diff --git a/app/config.py b/app/config.py index f52ec2f..8b1333d 100644 --- a/app/config.py +++ b/app/config.py @@ -49,6 +49,7 @@ class Config(object): MAIL_USERNAME = os.environ.get('MAIL_USERNAME') MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None MAX_INFO_TABS = 3 + MAX_MISSING_FILES_TO_REPORT = 10 MILLISECOND_SIGFIGS = 0 MYSQL_CONNECT = os.environ.get('MYSQL_CONNECT') or "mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_v2" # noqa E501 NORMALISE_ON_IMPORT = True diff --git a/app/utilities.py b/app/utilities.py index b797837..98df4fe 100755 --- a/app/utilities.py +++ b/app/utilities.py @@ -204,7 +204,9 @@ def update_db(session): Repopulate database """ - # Search for tracks in only one of directory and database + # Search for tracks that are in the music directory but not the datebase + # Check all paths in database exist + # If issues found, write to stdout but do not try to resolve them db_paths = set(Tracks.get_all_paths(session)) @@ -217,61 +219,74 @@ def update_db(session): os_paths_list.append(path) os_paths = set(os_paths_list) - # If a track is moved, only the path will have changed. - # For any files we have found whose paths are not in the database, - # check to see whether the filename (basename) is present in the - # database: + # Find any files in music directory that are not in database + files_not_in_db = list(os_paths - db_paths) - for path in list(os_paths - db_paths): - DEBUG(f"utilities.update_db: {path=} not in database") - # is filename in database with a different path? - track = Tracks.get_by_filename(session, os.path.basename(path)) - if not track: - messages.append(f"{path} missing from database: {path}") - else: - # Check track info matches found track - t = helpers.get_tags(path) - if t['artist'] == track.artist and t['title'] == track.title: - print(f">>> Update {path=} for {track.title=}") - track.update_path(session, path) - else: - create_track_from_file(session, path) - - # Refresh database paths - db_paths = set(Tracks.get_all_paths(session)) - # Remove any tracks from database whose paths don't exist + # Find paths in database missing in music directory + paths_not_found = [] + missing_file_count = 0 + more_files_to_report = False for path in list(db_paths - os_paths): - # Manage tracks listed in database but where path is invalid - DEBUG(f"Invalid {path=} in database", True) + if missing_file_count >= Config.MAX_MISSING_FILES_TO_REPORT: + more_files_to_report = True + break + + missing_file_count += 1 + track = Tracks.get_by_path(session, path) - messages.append(f"Remove from database: {path=} {track=}") + if not track: + ERROR(f"update_db: {path} not found in db") + continue - # Remove references from Playdates - Playdates.remove_track(session, track.id) - - # Replace playlist entries with a note - note_txt = ( - f"File removed: {track.title=}, {track.artist=}, " - f"{track.path=}" - ) - for playlist_track in track.playlists: - row = playlist_track.row - # Remove playlist entry - DEBUG(f"Remove {row=} from {playlist_track.playlist_id}", True) - playlist_track.playlist.remove_track(session, row) - # Create note - DEBUG(f"Add note at {row=} to {playlist_track.playlist_id=}", True) - Notes(session, playlist_track.playlist_id, row, note_txt) - - # Remove Track entry pointing to invalid path - Tracks.remove_by_path(session, path) + paths_not_found.append(track) # Output messages (so if running via cron, these will get sent to # user) - if messages: - print("Messages") - print("\n".join(messages)) + if files_not_in_db: + print("Files in music directory but not in database") + print("--------------------------------------------") + print("\n".join(files_not_in_db)) + print("\n") + if paths_not_found: + print("Invalid paths in database") + print("-------------------------") + for t in paths_not_found: + print(f""" + Track ID: {t.id} + Path: {t.path} + Title: {t.title} + Artist: {t.artist} + """) + if more_files_to_report: + print("There were more paths than listed that were not found") +# Spike +# +# # Manage tracks listed in database but where path is invalid +# DEBUG(f"Invalid {path=} in database", True) +# track = Tracks.get_by_path(session, path) +# messages.append(f"Remove from database: {path=} {track=}") +# +# # Remove references from Playdates +# Playdates.remove_track(session, track.id) +# +# # Replace playlist entries with a note +# note_txt = ( +# f"File removed: {track.title=}, {track.artist=}, " +# f"{track.path=}" +# ) +# for playlist_track in track.playlists: +# row = playlist_track.row +# # Remove playlist entry +# DEBUG(f"Remove {row=} from {playlist_track.playlist_id}", True) +# playlist_track.playlist.remove_track(session, row) +# # Create note +# DEBUG(f"Add note at {row=} to {playlist_track.playlist_id=}", True) +# Notes(session, playlist_track.playlist_id, row, note_txt) +# +# # Remove Track entry pointing to invalid path +# Tracks.remove_by_path(session, path) + if __name__ == '__main__' and '__file__' in globals(): main()