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]