From 4eac21b73c78c6bdfd1be28da4f23d2aae9dd798 Mon Sep 17 00:00:00 2001 From: Jackson Taylor Date: Sat, 6 Aug 2022 22:20:46 -0400 Subject: Handle more special characters Handle a unicode right quotation mark. Handle stupid /s in file names that cause you to debug for an hour. --- jamos | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) (limited to 'jamos') diff --git a/jamos b/jamos index 9c666e2..a7d8051 100755 --- a/jamos +++ b/jamos @@ -9,6 +9,8 @@ from pathlib import Path import sys import youtube_dl +DEBUG = False + def cleanup_metadata_files(music_directory): files = glob.glob(os.path.join(music_directory, '*.json')) @@ -80,14 +82,14 @@ def get_video_urls_in_playlist(playlist_url, ytdl): def move_file(file, metadata, output_directory): artist = metadata['artist'].replace( - '-', ' ').replace(' ', '_').replace("'", "").replace( - "&", "and").lower() + '-', ' ').replace("/", " ").replace(' ', '_').replace("'", "").replace( + "&", "and").replace(chr(8217), "").lower() album = metadata['album'].replace( - '-', ' ').replace(' ', '_').replace("'", "").replace( - "&", "and").lower() + '-', ' ').replace("/", " ").replace(' ', '_').replace("'", "").replace( + "&", "and").replace(chr(8217), "").lower() title = metadata['title'].replace( - '-', ' ').replace(' ', '_').replace("'", "").replace( - "&", "and").lower() + '-', ' ').replace("/", " ").replace(' ', '_').replace("'", "").replace( + "&", "and").replace(chr(8217), "").lower() final_directory = os.path.join( output_directory, @@ -167,7 +169,8 @@ def create_downloader(music_directory, cookies): }, {'key': 'FFmpegMetadata'}, ], - 'writeinfojson': True + 'writeinfojson': True, + 'quiet': not DEBUG } return youtube_dl.YoutubeDL(audio_options) @@ -197,33 +200,52 @@ if __name__ == "__main__": # cookie file. Youtube-dl doesn't break or throw if the file doesn't exist. ytdl = create_downloader(music_directory, cookies) - # TODO: Save urls to file so we can start in the - # middle of the playlist if needed + print("Downloading urls...", end="") urls = get_video_urls_in_playlist(playlist_url, ytdl) + print("Done.") - save_urls_from_playlist_to_file(os.path.join(music_directory, - "jamos_urls.txt"), urls) - + failed_urls = [] for url in urls: try: + print("Downloading: {}...".format(url), end='') ytdl.extract_info(url, download=True) + print("Done.") except Exception as ex: - # TODO: Handle this better print(ex) + print("Could not download: {}".format((url))) + failed_urls.append(url) + + try: + if len(failed_urls) > 1: + print("Saving failed urls to txt file.") + save_urls_from_playlist_to_file( + os.path.join(music_directory, "jamos_failed_urls.txt"), + failed_urls) + except Exception as ex: + print(ex) + print("Saving failed urls to file failed! Printing failed urls:") + for url in failed_urls: + print(url) files = get_all_files(music_directory) counter = 1 for f in files: try: + print("Adding metadata to {} ...".format(f), end="") with open(f.replace('.mp3', '.info.json')) as json_file: json_data = json.load(json_file) metadata = get_song_metadata_from_json(json_data, counter) write_metadata_to_song_file(f, metadata) + print("Done".format(f)) + print("Moving file...", end="") move_file(f, metadata, music_directory) + print("Done", end="") counter += 1 except Exception as e: # just gonna print this and move on to the next file. print(e) + print("Cleaning up JSON files...", end='') cleanup_metadata_files(music_directory) + print("Done.") -- cgit v1.2.3