Skip to content

Commit

Permalink
Merge pull request #30 from Gyarbij/dev
Browse files Browse the repository at this point in the history
Fixed
_get_sp_user_playlists: Updated the function to handle pagination and fetch all playlists, not just the first 50. The changes include:
Added a while loop to fetch all playlists by checking if there's a next value in the response.
Updated the assignment of sp_playlists inside the loop to fetch the next set of playlists if available, otherwise, set it to None.
  • Loading branch information
Gyarbij authored Jun 2, 2023
2 parents 72d0b05 + 399aac7 commit 8e6f1a5
Showing 1 changed file with 16 additions and 41 deletions.
57 changes: 16 additions & 41 deletions plexist/modules/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,35 +39,22 @@ 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,
[i for i in sp_playlist_tracks["items"] if i.get("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(
Expand All @@ -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:
Expand All @@ -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")
logging.error("No spotify playlists found for user provided")

0 comments on commit 8e6f1a5

Please sign in to comment.