I have some Python code to make a flask app that just auto saves all my discover weekly songs into a playlist, but it is unable to find the discover weekly. I have it list all my other playlists and they show up fine, and I have manually followed the discover weekly playlist in Spotify but it won't show.
EDIT: someone suggested checking the max limit, however for me at least this does not apply as the default limit is 50 and I have 41 playlists (including discover weekly)
def save_discover_weekly(token_info):
sp = spotipy.Spotify(auth=token_info['access_token'])
cur_playlists = sp.current_user_playlists()['items']
discover_weekly_id = None
saved_weekly_id = None
for playlist in cur_playlists:
print(playlist['name'])
if playlist['name'] == 'Discover Weekly':
discover_weekly_id = playlist['id']
print(discover_weekly_id)
break
if playlist['name'] == 'Saved Weekly':
saved_weekly_id = playlist['id']
print(saved_weekly_id)
if discover_weekly_id is None:
return "Discover Weekly not found"
if not saved_weekly_id:
print("Creating saved weekly playlist")
saved_weekly = sp.user_playlist_create(sp.current_user()['id'],
"Saved Weekly", True)
saved_weekly_id = saved_weekly['id']
discover_weekly_songs = sp.playlist_items(discover_weekly_id)
song_uris = [song['track']['uri'] for song in discover_weekly_songs['items']]
sp.user_playlist_add_tracks(sp.current_user()['id'], saved_weekly_id,
song_uris)
return "Discover Weekly songs saved to Saved Weekly playlist!"
Pagination
A lot of
Spotifyendpoints use pagination and take a pair of parameters:offsetandlimit. If skipped the default values are taken, which are usually documented in the corresponding endpoint reference. For example, the current user’s playlist request has a default limit of 20.spotipyfollows this way, sometimes using different default values. For example,current_user_playlisthas default limit of 50:So for the user with lots of playlists there is a chance not to retrieve enough data to get the desired playlist. You need to request these playlists page by page:
Search
Considering boundary cases there could be a situation that browsing playlists is more inferior than a simple search. It seems that there are not so many public playlists in search results by query “Discover Weekly”. And also Spotify ranks it high. It could be actually the very first playlist in the result collection. Still all the said above about paging applies here: