From 1ae7a915d2b753a016d6c202516fd1d470f5c108 Mon Sep 17 00:00:00 2001 From: amit177 <8685831+amit177@users.noreply.github.com> Date: Sat, 24 Aug 2024 16:40:53 +0300 Subject: [PATCH 1/3] pass just the items to write_tracks --- spotify_to_mp3.py | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/spotify_to_mp3.py b/spotify_to_mp3.py index 2026153..1b2bbce 100644 --- a/spotify_to_mp3.py +++ b/spotify_to_mp3.py @@ -15,31 +15,25 @@ def write_tracks(text_file: str, tracks: dict): # This includins the name, artist, and spotify URL. Each is delimited by a comma. with open(text_file, 'w+', encoding='utf-8') as file_out: - while True: - for item in tracks['items']: - if 'track' in item: - track = item['track'] - else: - track = item - try: - track_url = track['external_urls']['spotify'] - track_name = track['name'] - track_artist = track['artists'][0]['name'] - album_art_url = track['album']['images'][0]['url'] - csv_line = track_name + "," + track_artist + "," + track_url + "," + album_art_url + "\n" - try: - file_out.write(csv_line) - except UnicodeEncodeError: # Most likely caused by non-English song names - print("Track named {} failed due to an encoding error. This is \ - most likely due to this song having a non-English name.".format(track_name)) - except KeyError: - print(u'Skipping track {0} by {1} (local only?)'.format( - track['name'], track['artists'][0]['name'])) - # 1 page = 50 results, check if there are more pages - if tracks['next']: - tracks = spotify.next(tracks) + for item in tracks: + if 'track' in item: + track = item['track'] else: - break + track = item + try: + track_url = track['external_urls']['spotify'] + track_name = track['name'] + track_artist = track['artists'][0]['name'] + album_art_url = track['album']['images'][0]['url'] + csv_line = track_name + "," + track_artist + "," + track_url + "," + album_art_url + "\n" + try: + file_out.write(csv_line) + except UnicodeEncodeError: # Most likely caused by non-English song names + print("Track named {} failed due to an encoding error. This is \ + most likely due to this song having a non-English name.".format(track_name)) + except KeyError: + print(u'Skipping track {0} by {1} (local only?)'.format( + track['name'], track['artists'][0]['name'])) def write_playlist(username: str, playlist_id: str): @@ -47,7 +41,7 @@ def write_playlist(username: str, playlist_id: str): playlist_name = results['name'] text_file = u'{0}.txt'.format(playlist_name, ok='-_()[]{}') print(u'Writing {0} tracks to {1}.'.format(results['tracks']['total'], text_file)) - tracks = results['tracks'] + tracks = results['tracks']['items'] write_tracks(text_file, tracks) imgURLs = []; From 214ab8e1a9bd4d76775eb8136c32ee34ebb7edde Mon Sep 17 00:00:00 2001 From: amit177 <8685831+amit177@users.noreply.github.com> Date: Sat, 24 Aug 2024 17:35:00 +0300 Subject: [PATCH 2/3] separate playlist and playlist_tracks --- spotify_to_mp3.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/spotify_to_mp3.py b/spotify_to_mp3.py index 1b2bbce..e45be29 100644 --- a/spotify_to_mp3.py +++ b/spotify_to_mp3.py @@ -37,16 +37,21 @@ def write_tracks(text_file: str, tracks: dict): def write_playlist(username: str, playlist_id: str): - results = spotify.user_playlist(username, playlist_id, fields='tracks,next,name') - playlist_name = results['name'] + playlist_name = spotify.playlist(playlist_id, fields='name')['name'] text_file = u'{0}.txt'.format(playlist_name, ok='-_()[]{}') - print(u'Writing {0} tracks to {1}.'.format(results['tracks']['total'], text_file)) - tracks = results['tracks']['items'] + + results = spotify.playlist_tracks(playlist_id) + tracks = results["items"] + while results["next"]: + results = spotify.next(results) + tracks.extend(results["items"]) + + print(u'Writing {0} tracks to {1}.'.format(len(tracks), text_file)) write_tracks(text_file, tracks) - imgURLs = []; + imgURLs = [] for item in tracks['items']: - imgURLs.append(item['track']['album']['images'][0]['url']); + imgURLs.append(item['track']['album']['images'][0]['url']) return playlist_name, imgURLs def find_and_download_songs(reference_file: str): From a6614687baf243ea0fb007553ead814c9692ddc0 Mon Sep 17 00:00:00 2001 From: amit177 <8685831+amit177@users.noreply.github.com> Date: Sat, 24 Aug 2024 17:35:51 +0300 Subject: [PATCH 3/3] remove username requirement --- config.ini | 3 +-- spotify_to_mp3.py | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/config.ini b/config.ini index 1f7dacf..5e59bc2 100644 --- a/config.ini +++ b/config.ini @@ -1,4 +1,3 @@ [Settings] client_id =yourclientid -client_secret =yourclientsecret -username =yourusername \ No newline at end of file +client_secret =yourclientsecret \ No newline at end of file diff --git a/spotify_to_mp3.py b/spotify_to_mp3.py index e45be29..8581863 100644 --- a/spotify_to_mp3.py +++ b/spotify_to_mp3.py @@ -36,7 +36,7 @@ def write_tracks(text_file: str, tracks: dict): track['name'], track['artists'][0]['name'])) -def write_playlist(username: str, playlist_id: str): +def write_playlist(playlist_id: str): playlist_name = spotify.playlist(playlist_id, fields='name')['name'] text_file = u'{0}.txt'.format(playlist_name, ok='-_()[]{}') @@ -233,18 +233,16 @@ def enable_multicore(autoenable=False, maxcores=None, buffercores=1): config.read("config.ini") client_id = config["Settings"]["client_id"] client_secret = config["Settings"]["client_secret"] - username = config["Settings"]["username"] else: client_id = input("Client ID: ") client_secret = input("Client secret: ") - username = input("Spotify username: ") playlist_uri = input("Playlist URI/Link: ") if playlist_uri.find("https://open.spotify.com/playlist/") != -1: playlist_uri = playlist_uri.replace("https://open.spotify.com/playlist/", "") multicore_support = enable_multicore(autoenable=False, maxcores=None, buffercores=1) auth_manager = oauth2.SpotifyClientCredentials(client_id=client_id, client_secret=client_secret) spotify = spotipy.Spotify(auth_manager=auth_manager) - playlist_name, albumArtUrls = write_playlist(username, playlist_uri) + playlist_name, albumArtUrls = write_playlist(playlist_uri) reference_file = "{}.txt".format(playlist_name) # Create the playlist folder if not os.path.exists(playlist_name):