From eda8f31425b6c4dd034cab0312a3a42e74dcb375 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sat, 13 Dec 2025 21:31:26 +0100 Subject: work --- .gitignore | 4 +- dl-artist.py | 183 +++++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 123 insertions(+), 64 deletions(-) diff --git a/.gitignore b/.gitignore index c59f32b..ea898a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ my-venv/ output/ yt-dlp_linux -~ \ No newline at end of file +queue.txt +download/ +*.txt \ No newline at end of file diff --git a/dl-artist.py b/dl-artist.py index 78fd3fc..1aec5c4 100644 --- a/dl-artist.py +++ b/dl-artist.py @@ -10,73 +10,130 @@ except ImportError: import os DEVNULL = open(os.devnull, 'wb') +output_dir = 'download' +do_download = True ytmusic = YTMusic() -artist_result = ytmusic.get_artist(sys.argv[1]) -artist_name = artist_result['name'] +while True: + ## Pop item from top of list and write back file. + with open('queue.txt', 'r') as file: + lines = file.readlines() -#print(artist_result['albums']['browseId']) + lines = [line.strip() for line in lines if line.strip()] + artist_to_download = lines.pop(0) -albums = [] -singles = [] + with open('queue.txt', 'w') as file: + for line in lines: + file.write(line + '\n') -if 'albums' in artist_result: - albums = artist_result['albums']['results'] - if artist_result['albums']['browseId'] != None: - albums = ytmusic.get_artist_albums(artist_result["albums"]["browseId"], artist_result["albums"]["params"]) + ## Artist exists already or other instance is downloading it. + directory_path = output_dir+'/'+artist_to_download + if os.path.exists(directory_path) and os.path.isdir(directory_path): + print(f'Artist exists: {artist_to_download}, skipping.') + continue -if 'singles' in artist_result: - singles = artist_result['singles']['results'] - if artist_result['singles']['browseId'] != None: - singles = ytmusic.get_artist_albums(artist_result["singles"]["browseId"], artist_result["singles"]["params"]) - -print('Artist "' + artist_name + '" has ' + str(len(albums)) + ' albums...') -for album in albums: - albumdata = ytmusic.get_album(album['browseId']) - album_name = albumdata['title'] - - print('Downloading "' + album_name + '"') - - # Construct the command as a list - command = [ - './yt-dlp_linux', - f"https://music.youtube.com/playlist?list={albumdata['audioPlaylistId']}", - '-o', - 'output/'+artist_name+'/'+album_name+'/%(title)s.%(ext)s', # Adjust the path as needed - '-x', - '--audio-format', - 'mp3', - '--embed-thumbnail', - '--add-metadata' - ] - - p = subprocess.Popen(command, stdin=PIPE, stdout=DEVNULL, stderr=STDOUT) - (output, err) = p.communicate() - p_status = p.wait() - print('done') - - -print('Artist "' + artist_name + '" has ' + str(len(singles)) + ' singles...') -for single in singles: - singledata = ytmusic.get_album(single['browseId']) - single_name = singledata['title'] + artist_id_to_download = 0 - print('Downloading "' + single_name + '"') - - # Construct the command as a list - command = [ - './yt-dlp_linux', - f"https://music.youtube.com/playlist?list={singledata['audioPlaylistId']}", - '-o', - 'output/'+artist_name+'/Singles/%(title)s.%(ext)s', # Adjust the path as needed - '-x', - '--audio-format', - 'mp3', - '--embed-thumbnail', - '--add-metadata' - ] - - p = subprocess.Popen(command, stdin=PIPE, stdout=DEVNULL, stderr=STDOUT) - (output, err) = p.communicate() - p_status = p.wait() - print('done') + ## Find artist id by name. + search_results = ytmusic.search(artist_to_download, filter='artists') + if search_results: + for artist in search_results: + artist_id_to_download = artist['browseId'] + print(f"Found artist: {artist['artist']}, ID: {artist['browseId']}") + break + else: + print(f"No artists found for {artist_to_download}") + continue + + + artist_result = ytmusic.get_artist(artist_id_to_download) + artist_name = artist_result['name'] + + ## Collect albums and singles to download. + albums = [] + singles = [] + if 'albums' in artist_result: + albums = artist_result['albums']['results'] + if artist_result['albums']['browseId'] != None: + albums = ytmusic.get_artist_albums(artist_result["albums"]["browseId"], artist_result["albums"]["params"]) + + if 'singles' in artist_result: + singles = artist_result['singles']['results'] + if artist_result['singles']['browseId'] != None: + singles = ytmusic.get_artist_albums(artist_result["singles"]["browseId"], artist_result["singles"]["params"]) + + ##################### + #### 1. Download albums + ##################### + print('Artist "' + artist_name + '" has ' + str(len(albums)) + ' albums...') + for index, album in enumerate(albums): + albumdata = ytmusic.get_album(album['browseId']) + album_name = albumdata['title'] + + print(f'Downloading "{album_name}"', end='\r') + + # Construct the command as a list + command = [ + './yt-dlp_linux', + f"https://music.youtube.com/playlist?list={albumdata['audioPlaylistId']}", + '-o', + output_dir+'/'+artist_name+'/'+album_name+'/%(title)s.%(ext)s', # Adjust the path as needed + '-x', + '--audio-format', + 'mp3', + '--embed-thumbnail', + '--add-metadata', + '--no-overwrites' + ] + + if not do_download: + continue + + p = subprocess.Popen(command, stdin=PIPE, stdout=DEVNULL, stderr=STDOUT) + (output, err) = p.communicate() + p_status = p.wait() + + directory_path = output_dir+'/'+artist_name+'/'+album_name + if os.path.exists(directory_path) and os.path.isdir(directory_path): + print(f'Downloaded "{album_name} ({index+1}/{len(albums)})"') + else: + print('Download failed') + exit() + + ##################### + #### 2. Download singles + ##################### + print('Artist "' + artist_name + '" has ' + str(len(singles)) + ' singles...') + for index, single in enumerate(singles): + singledata = ytmusic.get_album(single['browseId']) + single_name = singledata['title'] + + print(f'Downloading "{single_name}"', end='\r') + + # Construct the command as a list + command = [ + './yt-dlp_linux', + f"https://music.youtube.com/playlist?list={singledata['audioPlaylistId']}", + '-o', + output_dir+'/'+artist_name+'/Singles/%(title)s.%(ext)s', # Adjust the path as needed + '-x', + '--audio-format', + 'mp3', + '--embed-thumbnail', + '--add-metadata', + '--no-overwrites' + ] + + if not do_download: + continue + + p = subprocess.Popen(command, stdin=PIPE, stdout=DEVNULL, stderr=STDOUT) + (output, err) = p.communicate() + p_status = p.wait() + + directory_path = output_dir+'/'+artist_name+'/Singles' + if os.path.exists(directory_path) and os.path.isdir(directory_path): + print(f'Downloaded "{single_name} ({index+1}/{len(singles)})"') + else: + print('Download failed') + exit() \ No newline at end of file -- cgit v1.2.3-70-g09d2