Reputation: 11
I am trying to get the spotify audiofeatures from the songs in a playlist using spotipy. However, I only get the data of the last song in the playlist. My code looks like this:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import time
import numpy as np
import pandas
client_id = 'ID'
client_secret = "Secret"
#Authentication - without user
client_credentials_manager = SpotifyClientCredentials(client_id=client_id,
client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager = client_credentials_manager)
playlist_link = "https://open.spotify.com/playlist/3VJlwgnV4IaxGK8uIEZMjV?
si=ca8c506dd5d04663"
playlist_URI = playlist_link.split("/")[-1].split("?")[0]
track_uris = [x["track"]["uri"] for x in sp.playlist_tracks(playlist_URI)["items"]]
for track in sp.playlist_tracks(playlist_URI)["items"]:
#URI
track_uri = track["track"]["uri"]
#Track name
track_name = track["track"]["name"]
#Main Artist
artist_uri = track["track"]["artists"][0]["uri"]
artist_info = sp.artist(artist_uri)
#Name, popularity, genre
artist_name = track["track"]["artists"][0]["name"]
artist_pop = artist_info["popularity"]
artist_genres = artist_info["genres"]
#Album
album = track["track"]["album"]["name"]
#Popularity of the track
track_pop = track["track"]["popularity"]
result = track_name, sp.audio_features(track_uri)
result
I have filled in the ID's but I removed them here for privacy.
Upvotes: 1
Views: 2332
Reputation: 415
Your question didn't give me the necessary information to run the code, so I have answered this blind.
You are not returning at the end of the for loop and you probably want to add the results to a structure and return that.
The below has (guessed) what you want. Few other comments:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
CLIENT_ID = 'ID'
CLIENT_SECRET = "Secret"
PLAYLIST_LINK = "https://open.spotify.com/playlist/3VJlwgnV4IaxGK8uIEZMjV?si=ca8c506dd5d04663"
CLIENT_CREDENTIALS_MANAGER = SpotifyClientCredentials(
client_id=CLIENT_ID, client_secret=CLIENT_SECRET
)
SP = spotipy.Spotify(client_credentials_manager=CLIENT_CREDENTIALS_MANAGER)
def get_playlist_uri(playlist_link):
return playlist_link.split("/")[-1].split("?")[0]
def get_tracks():
tracks = []
playlist_uri = get_playlist_uri(PLAYLIST_LINK)
for track in SP.playlist_tracks(playlist_uri)["items"]:
track_uri = track["track"]["uri"]
track_name = track["track"]["name"]
result = track_name, SP.audio_features(track_uri)
tracks.append(result)
return tracks
the_stuff = get_tracks()
This popped up as a notification as someone thought it was helpful. Here is a more updated version of how I would do it.
from spotipy import Spotify
from spotipy.oauth2 import SpotifyClientCredentials
from typing import NamedTuple, List
class Track(NamedTuple):
uri: str
name: str
features: List[str]
@classmethod
def from_spotify(cls, spotify: Spotify, track: Dict) -> 'Track':
uri: str = track["track"]["uri"]
return cls(
uri=uri,
name=track["track"]["name"],
features=spotify.audio_features(uri)
)
def get_playlist_uri(playlist_link: str):
# this should use url_parse or something...
return playlist_link.split("/")[-1].split("?")[0]
def get_tracks_from_playlist(client: Spotify, playlist_name: str) -> Iterable[Track]:
for track in client.playlist_tracks(playlist_name)["items"]:
yield Track.from_spotify(client, track)
if __name__ == "__main__":
CLIENT_ID = 'ID'
CLIENT_SECRET = "Secret"
PLAYLIST_LINK = "https://open.spotify.com/playlist/3VJlwgnV4IaxGK8uIEZMjV?si=ca8c506dd5d04663"
client = SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
spotify = Spotify(client_credentials_manager=client)
playlist = get_playlist_uri(PLAYLIST_LINK)
tracks = tuple(get_tracks_from_playlist(spotify, playlist))
Upvotes: 2