summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJackson Taylor <jackson@jacksontaylor.xyz>2022-08-06 15:07:19 -0400
committerJackson Taylor <jackson@jacksontaylor.xyz>2022-08-06 15:07:19 -0400
commitffd09bf58a7e08e7f216257995435dbb6396b4cf (patch)
treecb056499fbada1d0e294618df58cfc6c5c179568
parent65f3c684e824378abc8cadc55804e09ff726b426 (diff)
Continuation fixes
Save urls to file Add try block around metadata value additions
-rwxr-xr-xjamos38
1 files 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)