From 822b696dc431cbad278d6c508195e54201796144 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Thu, 21 Oct 2021 23:13:39 +0300 Subject: [PATCH 1/5] Added Artist String Id in regex_input_for_urls --- zspotify.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/zspotify.py b/zspotify.py index b31665a1..89d36b99 100755 --- a/zspotify.py +++ b/zspotify.py @@ -141,7 +141,7 @@ def client(): else: search_text = input("Enter search or URL: ") - track_id_str, album_id_str, playlist_id_str, episode_id_str = regex_input_for_urls( + track_id_str, album_id_str, playlist_id_str, episode_id_str, artist_id_str = regex_input_for_urls( search_text) if track_id_str is not None: @@ -191,6 +191,13 @@ def regex_input_for_urls(search_input): search_input, ) + artist_uri_search = re.search( + r"^spotify:artist:(?P[0-9a-zA-Z]{22})$", search_input) + artist_url_search = re.search( + r"^(https?://)?open\.spotify\.com/artist/(?P[0-9a-zA-Z]{22})(\?si=.+?)?$", + search_input, + ) + if track_uri_search is not None or track_url_search is not None: track_id_str = (track_uri_search if track_uri_search is not None else @@ -219,7 +226,14 @@ def regex_input_for_urls(search_input): else: episode_id_str = None - return track_id_str, album_id_str, playlist_id_str, episode_id_str + if artist_uri_search is not None or artist_url_search is not None: + artist_id_str = (artist_uri_search + if artist_uri_search is not None else + artist_url_search).group("ArtistID") + else: + artist_id_str = None + + return track_id_str, album_id_str, playlist_id_str, episode_id_str, artist_id_str def get_episode_info(episode_id_str): @@ -494,7 +508,6 @@ def get_album_name(access_token, album_id): f'https://api.spotify.com/v1/albums/{album_id}', headers=headers).json() return resp['artists'][0]['name'], sanitize_data(resp['name']) - # Extra functions directly related to our saved tracks def get_saved_tracks(access_token): """ Returns user's saved tracks """ From 92090512d05161c000d40b6f7f284aeba920ab06 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Thu, 21 Oct 2021 23:15:09 +0300 Subject: [PATCH 2/5] Added get_artist_albums Added the get_artist_albums(access_token, artist_id) function that returns a list of tuples that each contain the name and the id of an album. --- zspotify.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/zspotify.py b/zspotify.py index 89d36b99..820ec1ed 100755 --- a/zspotify.py +++ b/zspotify.py @@ -508,6 +508,16 @@ def get_album_name(access_token, album_id): f'https://api.spotify.com/v1/albums/{album_id}', headers=headers).json() return resp['artists'][0]['name'], sanitize_data(resp['name']) +# Extra functions directly related to spotify artists +def get_artist_albums(access_token, artist_id): + """ Returns artist's albums """ + headers = {'Authorization': f'Bearer {access_token}'} + resp = requests.get( + f'https://api.spotify.com/v1/artists/{artist_id}/albums', headers=headers).json() + # Return a list of tuples that contain each album's name and id + return [(resp['items'][i]['name'], resp['items'][i]['id']) + for i in range(len(resp['items']))] + # Extra functions directly related to our saved tracks def get_saved_tracks(access_token): """ Returns user's saved tracks """ From 126ebe2046de8ad2028bda6136176ed17ec84bb6 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Thu, 21 Oct 2021 23:27:02 +0300 Subject: [PATCH 3/5] Added download_artist_albums Added a function to be called in client if artist_id_str is not None; download_artist_albums(artist_id_str) that uses the get_artist_album function to get the artist's album id's and then downloads each one with the download_album function. Also modified get_artist_album function to return a list of album ids instead of a list of tuples of album ids and names. --- zspotify.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/zspotify.py b/zspotify.py index 820ec1ed..ed0b7aad 100755 --- a/zspotify.py +++ b/zspotify.py @@ -127,6 +127,8 @@ def client(): if track_id_str is not None: download_track(track_id_str) + elif artist_id_str is not None: + download_artist_albums(artist_id_str) elif album_id_str is not None: download_album(album_id_str) elif playlist_id_str is not None: @@ -146,6 +148,8 @@ def client(): if track_id_str is not None: download_track(track_id_str) + elif artist_id_str is not None: + download_artist_albums(artist_id_str) elif album_id_str is not None: download_album(album_id_str) elif playlist_id_str is not None: @@ -604,6 +608,12 @@ def download_album(album): download_track(track['id'], artist + " - " + album_name + "/") print("\n") +def download_artist_albums(artist): + """ Downloads albums of an artist """ + token = SESSION.tokens().get("user-read-email") + albums = get_artist_albums(token, artist) + for name, album_id in albums: + download_album(album_id) def download_from_user_playlist(): """ Downloads songs from users playlist """ From 1bd12be1dd33e33dc49e8985445c0fccc6f6abc0 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Thu, 21 Oct 2021 23:38:58 +0300 Subject: [PATCH 4/5] Modified get_artist_album --- zspotify.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/zspotify.py b/zspotify.py index ed0b7aad..1a64bccb 100755 --- a/zspotify.py +++ b/zspotify.py @@ -518,9 +518,8 @@ def get_artist_albums(access_token, artist_id): headers = {'Authorization': f'Bearer {access_token}'} resp = requests.get( f'https://api.spotify.com/v1/artists/{artist_id}/albums', headers=headers).json() - # Return a list of tuples that contain each album's name and id - return [(resp['items'][i]['name'], resp['items'][i]['id']) - for i in range(len(resp['items']))] + # Return a list each album's id + return [resp['items'][i]['id'] for i in range(len(resp['items']))] # Extra functions directly related to our saved tracks def get_saved_tracks(access_token): From 07c1a6a3df586dbc5014e744d5be6a2613d9d162 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Thu, 21 Oct 2021 23:40:40 +0300 Subject: [PATCH 5/5] Fixed minor error in download_artist_albums --- zspotify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zspotify.py b/zspotify.py index 1a64bccb..e3bc56ec 100755 --- a/zspotify.py +++ b/zspotify.py @@ -611,7 +611,7 @@ def download_artist_albums(artist): """ Downloads albums of an artist """ token = SESSION.tokens().get("user-read-email") albums = get_artist_albums(token, artist) - for name, album_id in albums: + for album_id in albums: download_album(album_id) def download_from_user_playlist():