Skip to content

Commit

Permalink
fix playlist fetch bug for paginated playlists (SathyaBhat#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
SathyaBhat authored Nov 18, 2020
1 parent 56877bb commit 5519948
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ A clear and concise description of what you expected to happen.
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- OS: [e.g. Windows 10, Linux (which distro)]
- Python version

2 changes: 1 addition & 1 deletion spotify_dl/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__all__ = ['VERSION']

VERSION = '7.1.0'
VERSION = '7.2.0'
SAVE_PATH = '~/.spotifydl'
15 changes: 9 additions & 6 deletions spotify_dl/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,32 @@ def fetch_tracks(sp, item_type, url):
offset = 0

if item_type == 'playlist':
items = sp.playlist_items(playlist_id=url, fields='items.track.name,items.track.artists(name),items.track.album(name),total,next,offset', additional_types=['track'])
while True:
items = sp.playlist_items(playlist_id=url, fields='items.track.name,items.track.artists(name),items.track.album(name),total,next,offset', additional_types=['track'], offset=offset)
total_songs = items.get('total')
for item in items['items']:
track_name = item['track']['name']
log.debug("Artist: {}".format(item['track']['artists']))
track_artist = ", ".join([artist['name'] for artist in item['track']['artists']])
songs_dict.update({track_name: track_artist})
offset += 1

if items.get('next') is None:

log.info(f"Fetched {offset}/{total_songs} songs in the playlist")
if total_songs == offset:
log.info('All pages fetched, time to leave. Added %s songs in total', offset)
break

elif item_type == 'album':
items = sp.album_tracks(album_id=url)
while True:
items = sp.album_tracks(album_id=url)
total_songs = items.get('total')
for item in items['items']:
track_name = item['name']
track_artist = " ".join([artist['name'] for artist in item['artists']])
songs_dict.update({track_name: track_artist})
offset += 1

if items.get('next') is None:
log.info(f"Fetched {offset}/{total_songs} songs in the album")
if total_songs == offset:
log.info('All pages fetched, time to leave. Added %s songs in total', offset)
break

Expand Down
2 changes: 1 addition & 1 deletion spotify_dl/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def download_songs(songs, download_directory, format_string, skip_mp3):

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
try:
log.debug(ydl.download([query]))
ydl.download([query])
except Exception as e:
log.debug(e)
print('Failed to download: {}, please ensure YouTubeDL is up-to-date. '.format(query))
Expand Down

0 comments on commit 5519948

Please sign in to comment.