Merge pull request #90 from ritiek/develop

Fetch all user playlists (not just limit to 50 max)
This commit is contained in:
Ritiek Malhotra
2017-06-26 19:58:30 +05:30
committed by GitHub
3 changed files with 43 additions and 28 deletions

View File

@@ -72,17 +72,6 @@ def is_spotify(raw_song):
else:
return False
# write tracks into list file
def feed_tracks(file, tracks):
with open(file, 'a') as fout:
for item in tracks['items']:
track = item['track']
try:
fout.write(track['external_urls']['spotify'] + '\n')
except KeyError:
title = track['name'] + ' by '+ track['artists'][0]['name']
print('Skipping track ' + title + ' (local only?)')
# generate filename of the song to be downloaded
def generate_filename(title):
# IMO python2 sucks dealing with unicode

View File

@@ -119,10 +119,16 @@ def feed_playlist(username):
links = []
check = 1
# iterate over user playlists
for playlist in playlists['items']:
print(str(check) + '. ' + misc.fix_encoding(playlist['name']) + ' (' + str(playlist['tracks']['total']) + ' tracks)')
links.append(playlist)
check += 1
while True:
for playlist in playlists['items']:
print(str(check) + '. ' + misc.fix_encoding(playlist['name']) + ' (' + str(playlist['tracks']['total']) + ' tracks)')
links.append(playlist)
check += 1
if playlists['next']:
playlists = spotify.next(playlists)
else:
break
print('')
# let user select playlist
playlist = misc.input_link(links)
@@ -132,14 +138,23 @@ def feed_playlist(username):
# slugify removes any special characters
file = slugify(playlist['name'], ok='-_()[]{}') + '.txt'
print('Feeding ' + str(playlist['tracks']['total']) + ' tracks to ' + file)
tracks = results['tracks']
# write tracks to file
misc.feed_tracks(file, tracks)
# check if there are more pages
# 1 page = 50 results
while tracks['next']:
tracks = spotify.next(tracks)
misc.feed_tracks(file, tracks)
with open(file, 'a') as fout:
while True:
for item in tracks['items']:
track = item['track']
try:
fout.write(track['external_urls']['spotify'] + '\n')
except KeyError:
title = track['name'] + ' by '+ track['artists'][0]['name']
print('Skipping track ' + title + ' (local only?)')
# 1 page = 50 results
# check if there are more pages
if tracks['next']:
tracks = spotify.next(tracks)
else:
break
def download_song(content):
if args.input_ext == '.webm':
@@ -285,4 +300,4 @@ if __name__ == '__main__':
elif args.list:
grab_list(file=args.list)
elif args.username:
feed_playlist(username=args.username)
feed_playlist(username=args.username)

View File

@@ -16,16 +16,27 @@ def test_playlist():
tracks = playlist['tracks']['total']
assert tracks == expect_tracks
def test_list():
def test_tracks():
playlist = spotdl.spotify.user_playlists(username)['items'][0]
expect_lines = playlist['tracks']['total']
result = spotdl.spotify.user_playlist(playlist['owner']['id'], playlist['id'], fields='tracks,next')
tracks = result['tracks']
spotdl.misc.feed_tracks('list.txt', tracks)
while tracks['next']:
tracks = spotdl.spotify.next(tracks)
spotdl.misc.feed_tracks('list.txt', tracks)
with open('list.txt', 'a') as fout:
while True:
for item in tracks['items']:
track = item['track']
try:
fout.write(track['external_urls']['spotify'] + '\n')
except KeyError:
title = track['name'] + ' by '+ track['artists'][0]['name']
print('Skipping track ' + title + ' (local only?)')
# 1 page = 50 results
# check if there are more pages
if tracks['next']:
tracks = spotify.next(tracks)
else:
break
with open('list.txt', 'r') as listed:
expect_song = (listed.read()).splitlines()[0]