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

@@ -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')