From e90b5a4b4e277329a43cd4b84f079d5481ef372a Mon Sep 17 00:00:00 2001 From: Ritiek Malhotra Date: Mon, 26 Jun 2017 19:18:22 +0530 Subject: [PATCH 1/2] Fetches all user playlists now (was limited to 50) --- spotdl.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/spotdl.py b/spotdl.py index ee2d067..2fa38e9 100755 --- a/spotdl.py +++ b/spotdl.py @@ -119,10 +119,15 @@ 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) @@ -285,4 +290,4 @@ if __name__ == '__main__': elif args.list: grab_list(file=args.list) elif args.username: - feed_playlist(username=args.username) \ No newline at end of file + feed_playlist(username=args.username) From 8af24793891aab737107b8ea4f870442e7814391 Mon Sep 17 00:00:00 2001 From: Ritiek Date: Mon, 26 Jun 2017 19:44:02 +0530 Subject: [PATCH 2/2] Update mechanism for fetching playlist tracks --- core/misc.py | 11 ----------- spotdl.py | 24 +++++++++++++++++------- test/test_username.py | 21 ++++++++++++++++----- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/core/misc.py b/core/misc.py index f3da00f..3fdea63 100755 --- a/core/misc.py +++ b/core/misc.py @@ -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 diff --git a/spotdl.py b/spotdl.py index 2fa38e9..68b1430 100755 --- a/spotdl.py +++ b/spotdl.py @@ -128,6 +128,7 @@ def feed_playlist(username): playlists = spotify.next(playlists) else: break + print('') # let user select playlist playlist = misc.input_link(links) @@ -137,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': diff --git a/test/test_username.py b/test/test_username.py index e94e9c7..eb996fb 100644 --- a/test/test_username.py +++ b/test/test_username.py @@ -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]