diff --git a/plexist/modules/spotify.py b/plexist/modules/spotify.py index 80bae6b..8485a99 100644 --- a/plexist/modules/spotify.py +++ b/plexist/modules/spotify.py @@ -11,31 +11,26 @@ def _get_sp_user_playlists( sp: spotipy.Spotify, user_id: str, suffix: str = " - Spotify" ) -> List[Playlist]: - """Get metadata for playlists in the given user_id. - - Args: - sp (spotipy.Spotify): Spotify configured instance - userId (str): UserId of the spotify account (get it from open.spotify.com/account) - suffix (str): Identifier for source - Returns: - List[Playlist]: list of Playlist objects with playlist metadata fields - """ playlists = [] try: sp_playlists = sp.user_playlists(user_id) - for playlist in sp_playlists["items"]: - playlists.append( - Playlist( - id=playlist["uri"], - name=playlist["name"], - description=playlist.get("description", ""), - # playlists may not have a poster in such cases return "" - poster="" - if len(playlist["images"]) == 0 - else playlist["images"][0].get("url", ""), + while sp_playlists: + for playlist in sp_playlists["items"]: + playlists.append( + Playlist( + id=playlist["uri"], + name=playlist["name"], + description=playlist.get("description", ""), + poster="" + if len(playlist["images"]) == 0 + else playlist["images"][0].get("url", ""), + ) ) - ) + if sp_playlists["next"]: + sp_playlists = sp.next(sp_playlists) + else: + sp_playlists = None except: logging.error("Spotify User ID Error") return playlists @@ -44,27 +39,15 @@ def _get_sp_user_playlists( def _get_sp_tracks_from_playlist( sp: spotipy.Spotify, user_id: str, playlist: Playlist ) -> List[Track]: - """Return list of tracks with metadata. - - Args: - sp (spotipy.Spotify): Spotify configured instance - user_id (str): spotify user id - playlist (Playlist): Playlist object - Returns: - List[Track]: list of Track objects with track metadata fields - """ - def extract_sp_track_metadata(track) -> Track: title = track["track"]["name"] artist = track["track"]["artists"][0]["name"] album = track["track"]["album"]["name"] - # Tracks may no longer be on spotify in such cases return "" url = track["track"]["external_urls"].get("spotify", "") return Track(title, artist, album, url) sp_playlist_tracks = sp.user_playlist_tracks(user_id, playlist.id) - # Only processes first 100 tracks tracks = list( map( extract_sp_track_metadata, @@ -72,7 +55,6 @@ def extract_sp_track_metadata(track) -> Track: ) ) - # If playlist contains more than 100 tracks this loop is useful while sp_playlist_tracks["next"]: sp_playlist_tracks = sp.next(sp_playlist_tracks) tracks.extend( @@ -89,13 +71,6 @@ def extract_sp_track_metadata(track) -> Track: def spotify_playlist_sync( sp: spotipy.Spotify, plex: PlexServer, userInputs: UserInputs ) -> None: - """Create/Update plex playlists with playlists from spotify. - - Args: - sp (spotipy.Spotify): Spotify configured instance - user_id (str): spotify user id - plex (PlexServer): A configured PlexServer instance - """ playlists = _get_sp_user_playlists(sp, userInputs.spotify_user_id) if playlists: for playlist in playlists: @@ -104,4 +79,4 @@ def spotify_playlist_sync( ) update_or_create_plex_playlist(plex, playlist, tracks, userInputs) else: - logging.error("No spotify playlists found for given user") \ No newline at end of file + logging.error("No spotify playlists found for user provided")