From c3dc00811296a2724ddcdb229ab58a5312af130c Mon Sep 17 00:00:00 2001 From: Jackson Taylor Date: Thu, 31 Mar 2022 01:00:21 -0400 Subject: Use music_output and cookies arguments --- jamos | 63 +++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 30 deletions(-) (limited to 'jamos') diff --git a/jamos b/jamos index a6cec06..b4b382b 100755 --- a/jamos +++ b/jamos @@ -10,29 +10,8 @@ import sys import youtube_dl -# MUSIC_DIRECTORY = "~/Music/" -MUSIC_DIRECTORY = "./music/" - -AUDIO_OPTIONS = { - 'format': 'mp3/bestaudio/best', - 'cookiefile': 'cookies.txt', - 'outtmpl': MUSIC_DIRECTORY + '%(title)s.%(ext)s', - 'postprocessors': [ - { - 'key': 'FFmpegExtractAudio', - 'preferredcodec': 'mp3', - 'preferredquality': '192', - }, - {'key': 'FFmpegMetadata'}, - ], - 'writeinfojson': True - } - -# TODO: pass this instead, to make testing easier -ytdl = youtube_dl.YoutubeDL(AUDIO_OPTIONS) - - -def download_song(song_url): + +def download_song(song_url, ytdl): """ Download a song using youtube url and song title """ @@ -77,7 +56,7 @@ def get_playlist_url(): return sys.argv[1] -def get_video_urls_in_playlist(playlist_url): +def get_video_urls_in_playlist(playlist_url, ytdl): videos = ytdl.extract_info(playlist_url, download=False) urls = [] @@ -88,7 +67,7 @@ def get_video_urls_in_playlist(playlist_url): return urls -def move_file(file, metadata): +def move_file(file, metadata, output_directory): # TODO: Pass a jamos specific metadata object/dict # Then we can do this validation all at once @@ -111,7 +90,7 @@ def move_file(file, metadata): title = metadata['title'].replace(' ', '_').replace('/', '').lower() final_directory = os.path.join( - MUSIC_DIRECTORY, + output_directory, artist, album) @@ -160,29 +139,53 @@ def write_metadata_to_song_file(file, metadata): f.save() +def create_downloader(music_directory, cookies): + audio_options = { + 'format': 'mp3/bestaudio/best', + 'cookiefile': cookies, + 'outtmpl': music_directory + '%(title)s.%(ext)s', + 'postprocessors': [ + { + 'key': 'FFmpegExtractAudio', + 'preferredcodec': 'mp3', + 'preferredquality': '192', + }, + {'key': 'FFmpegMetadata'}, + ], + 'writeinfojson': True + } + + return youtube_dl.YoutubeDL(audio_options) + + if __name__ == "__main__": args = get_command_line_options() # Get the playlist url from the command line playlist_url = args.url + music_directory = args.output or "~/Music" + cookies = args.cookies or "~/cookies.txt" + + 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) + urls = get_video_urls_in_playlist(playlist_url, ytdl) for url in urls: try: - download_song(url) + download_song(url, ytdl) except Exception as ex: # TODO: Handle this better print(ex) - files = get_all_files(MUSIC_DIRECTORY) + 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) + move_file(f, json_data, music_directory) -- cgit v1.2.3