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
This commit is contained in:
fanlp3
2017-11-29 23:19:38 -05:00
committed by Ritiek Malhotra
parent 7d7f4b75f7
commit 6ac60000e6
2 changed files with 10 additions and 25 deletions

View File

@@ -160,7 +160,7 @@ https://open.spotify.com/track/0fbspWuEdaaT9vfmbAZr1C
#### Download playlists #### 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 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`) - 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 <your_username>`, it will show all your public playlists (which excludes collaborative and private playlists). - Try running `python3 spotdl.py -u <your_username>`, 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 `<playlist_name>.txt`. - Once you select the one you want to download, the script will load all the tracks from the playlist into `<playlist_name>.txt`.

View File

@@ -217,12 +217,12 @@ def write_tracks(text_file, tracks):
break break
def write_playlist(playlist): def write_playlist(username, playlist_id):
results = spotify.user_playlist( results = spotify.user_playlist(
playlist['owner']['id'], playlist['id'], fields='tracks,next') username, playlist_id, fields='tracks,next,name')
text_file = u'{0}.txt'.format(slugify(playlist['name'], ok='-_()[]{}')) text_file = u'{0}.txt'.format(slugify(results['name'], ok='-_()[]{}'))
print(u'Feeding {0} tracks to {1}'.format(playlist['tracks']['total'], text_file))
print(u'Feeding {0} tracks to {1}'.format(results['tracks']['total'], text_file))
tracks = results['tracks'] tracks = results['tracks']
write_tracks(text_file, tracks) write_tracks(text_file, tracks)
@@ -340,24 +340,9 @@ def grab_playlist(playlist):
username = splits[-3] username = splits[-3]
playlist_id = splits[-1] playlist_id = splits[-1]
playlists = spotify.user_playlists(username) try:
found = False write_playlist(username, playlist_id)
except spotipy.client.SpotifyException:
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:
print('Unable to find playlist') print('Unable to find playlist')
print('Make sure the playlist is set to publicly visible and then try again') print('Make sure the playlist is set to publicly visible and then try again')