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 ee2d067..68b1430 100755 --- a/spotdl.py +++ b/spotdl.py @@ -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) \ No newline at end of file + feed_playlist(username=args.username) 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]