From 48b914a4be9ea7b13d5e850fcae8e8e2fc545d97 Mon Sep 17 00:00:00 2001 From: Broque Thomas Date: Sun, 25 Jan 2026 13:51:29 -0800 Subject: [PATCH] Update spotify_client.py --- core/spotify_client.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/core/spotify_client.py b/core/spotify_client.py index 9279db60..70a9ce34 100644 --- a/core/spotify_client.py +++ b/core/spotify_client.py @@ -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