def getSpotifyUserPlaylists(sp: spotipy.client, userId: str) -> []: playlists = sp.user_playlists(userId) spotifyPlaylists = [] while playlists: playlistItems = playlists['items'] for i, playlist in enumerate(playlistItems): if playlist['owner']['id'] == userId: spotifyPlaylists.append(getSpotifyPlaylist(sp, userId, playlist['id'])) if playlists['next']: playlists = sp.next(playlists) else: playlists = None return spotifyPlaylists
def getSpotifyLoggedUserPlaylists(sp: spotipy.client) -> []: playlists = sp.current_user_playlists() spotifyPlaylists = [] while playlists: playlistItems = playlists['items'] # logging.info(playlists['items']) for i, playlist in enumerate(playlistItems): spotifyPlaylists.append(getSpotifyPlaylist(sp, playlist['owner']['id'], playlist['id'])) if playlists['next']: playlists = sp.next(playlists) else: playlists = None return spotifyPlaylists
def getSpotifyTracks(sp: spotipy.client, playlist: []) -> []: spotifyTracks = [] tracks = playlist['tracks'] spotifyTracks.extend(tracks['items']) while tracks['next']: tracks = sp.next(tracks) spotifyTracks.extend(tracks['items']) return spotifyTracks
def get_tracks(sp: spotipy.client, playlist_uri_dict: dict, user: str, n_tracks: int = 5) -> dict: """Returns the information of `n_tracks` out of a particular playlist""" uri = playlist_uri_dict.get(user) playlist = sp.playlist(uri) tracks = playlist.get("tracks").get("items") tracks = [x.get("track").get("preview_url") for x in tracks] tracks = [x for x in tracks if x is not None] n_tracks = min(len(tracks), n_tracks) tracks = random.sample(tracks, n_tracks) return tracks
def getSpotifyPlaylist(sp: spotipy.client, userId: str, playlistId: str) -> []: playlist = sp.user_playlist(userId, playlistId) return playlist
def getSpotifyPlaylist(sp: spotipy.client, playlistId: str) -> []: playlist = sp.playlist(playlistId) return playlist