From ffd09bf58a7e08e7f216257995435dbb6396b4cf Mon Sep 17 00:00:00 2001 From: Jackson Taylor Date: Sat, 6 Aug 2022 15:07:19 -0400 Subject: Continuation fixes Save urls to file Add try block around metadata value additions --- jamos | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/jamos b/jamos index dc86158..587577c 100755 --- a/jamos +++ b/jamos @@ -16,13 +16,6 @@ def cleanup_metadata_files(music_directory): os.remove(file) -def download_song(song_url, ytdl): - """ - Download a song using youtube url and song title - """ - return ytdl.extract_info(song_url, download=True) - - def format_youtube_date(date): default = "Unknown Year" try: @@ -120,7 +113,7 @@ def write_metadata_to_song_file(file, metadata): if ('artist' in metadata.keys()) and (metadata['artist'] is not None): if len(metadata['artist'].split(',')) > 1: # If there are multiple artists, pick the first one - # NOTE: This may break if the artist has a comma in their name + # NOTE: This will break if the artist has a comma in their name artist = metadata['artist'].split(',')[0].replace(' ', '_').lower() else: artist = metadata['artist'].replace(' ', '_').lower() @@ -162,6 +155,15 @@ def create_downloader(music_directory, cookies): return youtube_dl.YoutubeDL(audio_options) +def save_urls_from_playlist_to_file(filename, urls): + try: + f = open(filename, "w") + for url in urls: + f.writelines(url) + f.close() + except Exception as e: + print(e) + raise e if __name__ == "__main__": args = get_command_line_options() @@ -172,15 +174,19 @@ if __name__ == "__main__": music_directory = args.output or "~/Music" cookies = args.cookies or "~/cookies.txt" + # From some testing, if your playlist is public, you don't have to use a 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 urls = get_video_urls_in_playlist(playlist_url, ytdl) + save_urls_from_playlist_to_file("~/jamos_urls.txt", urls) + for url in urls: try: - download_song(url, ytdl) + ytdl.extract_info(url, download=True) except Exception as ex: # TODO: Handle this better print(ex) @@ -188,11 +194,15 @@ if __name__ == "__main__": files = get_all_files(music_directory) for f in files: - json_data = None - with open(f.replace('.mp3', '.info.json')) as json_file: - json_data = json.load(json_file) - write_metadata_to_song_file(f, json_data) - move_file(f, json_data, music_directory) + try: + json_data = None + with open(f.replace('.mp3', '.info.json')) as json_file: + json_data = json.load(json_file) + write_metadata_to_song_file(f, json_data) + move_file(f, json_data, music_directory) + except Exception as e: + # just gonna print this and move on to the next file. + print(e) cleanup_metadata_files(music_directory) -- cgit v1.2.3