def update_database(ui): """ Scans the audio book directory for changes and new files. Also removes entries from the db that are no longer existent. """ paths = [] for location in db.Storage.select(): if os.path.exists(location.path): paths.append(location.path) # clean artwork cache artwork_cache.delete_artwork_cache() # are UI buttons currently blocked? player_blocked, importer_blocked = ui.get_ui_buttons_blocked() i = 0 percent_counter = 0 file_count = 0 for path in paths: file_count += sum([len(files) for r, d, files in os.walk(path)]) percent_threshold = file_count / 1000 failed = "" tracks_to_import = [] start = time.time() for path in paths: for directory, subdirectories, files in os.walk(path): for file in files: if file.lower().endswith(('.mp3', '.ogg', '.flac', '.m4a')): path = os.path.join(directory, file) imported = True try: # Is the track already in the database? if db.Track.select().where( db.Track.file == path).count() < 1: imported, track_data = import_file( file, directory, path) if track_data is not None: tracks_to_import.append(track_data) # Has the track changed on disk? elif tools.get_glib_settings().get_boolean( "use-crc32"): crc = __crc32_from_file(path) # Is the value in the db already crc32 or is the crc changed? if (db.Track.select().where( db.Track.file == path).first().modified != crc or db.Track.select().where( db.Track.file == path).first().crc32 != True): imported, ignore = import_file( file, directory, path, True, crc) # Has the modified date changed or is the value still a crc? elif (db.Track.select().where( db.Track.file == path).first().modified < os.path.getmtime(path) or db.Track.select().where( db.Track.file == path).first().crc32 != False): imported, ignore = import_file(file, directory, path, update=True) if not imported: failed += path + "\n" except Exception as e: log.warning("Could not import file: " + path) log.warning(traceback.format_exc()) failed += path + "\n" i = i + 1 if len(tracks_to_import) > 100: write_tracks_to_db(tracks_to_import) tracks_to_import = [] # don't flood gui updates if percent_counter < percent_threshold: percent_counter = percent_counter + 1 else: percent_counter = 1 Gdk.threads_add_idle( GLib.PRIORITY_DEFAULT_IDLE, ui.titlebar.progress_bar.set_fraction, i / file_count) Gdk.threads_add_idle( GLib.PRIORITY_DEFAULT_IDLE, ui.titlebar.update_progress_bar.set_fraction, i / file_count) write_tracks_to_db(tracks_to_import) end = time.time() log.info("Total import time: " + str(end - start)) # remove entries from the db that are no longer existent db.remove_invalid_entries() artwork_cache.generate_artwork_cache() Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.refresh_content) Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.switch_to_playing) Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.check_for_tracks) if len(failed) > 0: Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.display_failed_imports, failed)
def update_database(ui): """ Scans the audio book directory for changes and new files. Also removes entries from the db that are no longer existent. """ if not os.path.exists(db.Settings.get().path): # TODO: Notify the user about this return # clean artwork cache artwork_cache.delete_artwork_cache() i = 0 percent_counter = 0 file_count = sum( [len(files) for r, d, files in os.walk(db.Settings.get().path)]) percent_threshold = file_count / 1000 failed = "" for directory, subdirectories, files in os.walk(db.Settings.get().path): for file in files: if file.lower().endswith(('.mp3', '.ogg', '.flac', '.m4a')): path = os.path.join(directory, file) imported = True # Is the track already in the database? if db.Track.select().where(db.Track.file == path).count() < 1: imported = import_file(file, directory, path) # Has the track changed on disk? elif db.Track.select().where(db.Track.file == path).first( ).modified < os.path.getmtime(path): imported = import_file(file, directory, path, update=True) if not imported: failed += path + "\n" i = i + 1 # don't flood gui updates if percent_counter < percent_threshold: percent_counter = percent_counter + 1 else: percent_counter = 1 Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.progress_bar.set_fraction, i / file_count) Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.update_progress_bar.set_fraction, i / file_count) # remove entries from the db that are no longer existent for track in db.Track.select(): if not os.path.isfile(track.file): track.delete_instance() # remove all books that have no tracks for book in db.Book.select(): if db.Track.select().where(db.Track.book == book).count() < 1: book.delete_instance() artwork_cache.generate_artwork_cache() Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.refresh_content) Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.switch_to_playing) Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.block_ui_buttons, False, True) Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.check_for_tracks) if len(failed) > 0: Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.display_failed_imports, failed)