From 6ac60000e661ec07991ec736da73768c0e87347d Mon Sep 17 00:00:00 2001 From: fanlp3 Date: Wed, 29 Nov 2017 23:19:38 -0500 Subject: [PATCH] fixes: #159 Refactored grab_playlist (#160) * Refactored grab_playlist grab_playlist was looping through the user's playlist until it finds the playlist. In some cases a public playlist isn't in the user's playlists Using a more direct approach, querying for the tracks of a playlist * Remove old `def write_playlist()` * Playlist URL download now works on private playlists --- README.md | 4 ++-- spotdl.py | 31 ++++++++----------------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 1a824ec..a567777 100755 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ https://open.spotify.com/track/0fbspWuEdaaT9vfmbAZr1C #### Download playlists -- You can copy the Spotify URL of the playlist and pass it in `--playlist` option. Note: This method will not work with playlists set to private only or with collaborative playlists (since they cannot be set be publicly visible). +- You can copy the Spotify URL of the playlist and pass it in `--playlist` option. Note: This method works for public as well as private playlists. For example @@ -186,7 +186,7 @@ For example - You can also load songs using Spotify username if you don't have the playlist URL. (Open profile in Spotify, click on the three little dots below name, "Share", "Copy to clipboard", paste last numbers or text into command-line: `https://open.spotify.com/user/0123456790`) -- Try running `python3 spotdl.py -u `, it will show all your public playlists (which excludes collaborative and private playlists). +- Try running `python3 spotdl.py -u `, it will (only) show all your public playlists (which excludes collaborative and private playlists). - Once you select the one you want to download, the script will load all the tracks from the playlist into `.txt`. diff --git a/spotdl.py b/spotdl.py index f36fcc8..c1ec9eb 100755 --- a/spotdl.py +++ b/spotdl.py @@ -217,16 +217,16 @@ def write_tracks(text_file, tracks): break -def write_playlist(playlist): +def write_playlist(username, playlist_id): results = spotify.user_playlist( - playlist['owner']['id'], playlist['id'], fields='tracks,next') - text_file = u'{0}.txt'.format(slugify(playlist['name'], ok='-_()[]{}')) - print(u'Feeding {0} tracks to {1}'.format(playlist['tracks']['total'], text_file)) + username, playlist_id, fields='tracks,next,name') + text_file = u'{0}.txt'.format(slugify(results['name'], ok='-_()[]{}')) + print(u'Feeding {0} tracks to {1}'.format(results['tracks']['total'], text_file)) tracks = results['tracks'] write_tracks(text_file, tracks) - + def write_album(album): tracks = spotify.album_tracks(album['id']) text_file = u'{0}.txt'.format(slugify(album['name'], ok='-_()[]{}')) @@ -340,24 +340,9 @@ def grab_playlist(playlist): username = splits[-3] playlist_id = splits[-1] - playlists = spotify.user_playlists(username) - found = False - - while True: - for playlist in playlists['items']: - if not playlist['name'] == None: - if playlist['id'] == playlist_id: - playlists['next'] = None - found = True - break - if playlists['next']: - playlists = spotify.next(playlists) - else: - break - - if found: - write_playlist(playlist) - else: + try: + write_playlist(username, playlist_id) + except spotipy.client.SpotifyException: print('Unable to find playlist') print('Make sure the playlist is set to publicly visible and then try again')