Update spotify_client.py
This commit is contained in:
parent
61618c2fc7
commit
48b914a4be
1 changed files with 30 additions and 7 deletions
|
|
@ -314,23 +314,42 @@ class SpotifyClient:
|
|||
offset = 0
|
||||
total_fetched = 0
|
||||
|
||||
logger.info("Beginning fetch of user playlists...")
|
||||
|
||||
while True:
|
||||
results = self.sp.current_user_playlists(limit=limit, offset=offset)
|
||||
|
||||
if not results or 'items' not in results:
|
||||
break
|
||||
|
||||
# Log expected total on first page
|
||||
if offset == 0:
|
||||
expected_total = results.get('total', 'Unknown')
|
||||
logger.info(f"Spotify reports {expected_total} total playlists to fetch.")
|
||||
|
||||
batch_count = 0
|
||||
for playlist_data in results['items']:
|
||||
# Spotify API already returns all playlists the user has access to
|
||||
# (owned + followed), so no need to filter
|
||||
# Create playlist with empty tracks list for now
|
||||
playlist = Playlist.from_spotify_playlist(playlist_data, [])
|
||||
playlists.append(playlist)
|
||||
batch_count += 1
|
||||
try:
|
||||
# Spotify API already returns all playlists the user has access to
|
||||
# (owned + followed), so no need to filter
|
||||
|
||||
# Handle potential missing owner data safely
|
||||
if not playlist_data.get('owner'):
|
||||
playlist_data['owner'] = {'display_name': 'Unknown Owner', 'id': 'unknown'}
|
||||
elif not playlist_data['owner'].get('display_name'):
|
||||
playlist_data['owner']['display_name'] = 'Unknown'
|
||||
|
||||
# Create playlist with empty tracks list for now
|
||||
playlist = Playlist.from_spotify_playlist(playlist_data, [])
|
||||
playlists.append(playlist)
|
||||
batch_count += 1
|
||||
|
||||
except Exception as p_error:
|
||||
p_name = playlist_data.get('name', 'Unknown') if playlist_data else 'None'
|
||||
logger.warning(f"Skipping malformed playlist '{p_name}': {p_error}")
|
||||
|
||||
total_fetched += batch_count
|
||||
logger.info(f"Retrieved {batch_count} playlists in batch (offset {offset}), total: {total_fetched}")
|
||||
logger.info(f"Retrieved {batch_count} playlists in batch (offset {offset}), total so far: {total_fetched}")
|
||||
|
||||
# Check if we've fetched all playlists
|
||||
if len(results['items']) < limit or not results.get('next'):
|
||||
|
|
@ -343,6 +362,10 @@ class SpotifyClient:
|
|||
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching user playlists metadata: {e}")
|
||||
# Return partial results if we crashed mid-way but have some data
|
||||
if playlists:
|
||||
logger.info(f"Returning {len(playlists)} playlists fetched before error.")
|
||||
return playlists
|
||||
return []
|
||||
|
||||
@rate_limited
|
||||
|
|
|
|||
Loading…
Reference in a new issue