spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
4.97k stars 956 forks source link

Song Popularity #207

Closed fhopp closed 7 years ago

fhopp commented 7 years ago

Has anyone found a way to get the popularity of a song, /not/ of an /artist/? The examples that give you info on tracks only deliver metadata such as duration and explicitness, but Spotify's API also provides the title popularity in their track object (https://developer.spotify.com/web-api/get-track/#disqus_thread) I know that not every song has a popularity rating, but my code below gives me NaNs for /all/ songs (and I am scraping quite a bunch). Any ideas? Here's the track-portion of the code, thanks!


tracks =[] 
results = sp.album_tracks(album['id'])
tracks.extend(results['items'])
            while results['next']:
                results = sp.next(results)
                tracks.extend(results['items'])
            track_dict = defaultdict(lambda: defaultdict())
            for track in tracks:
                name = track['name']
                track_dict[name]['duration'] = track['duration_ms']
                track_dict[name]['explicit'] = track['explicit']
                track_dict[name]['artist'] = track['artists'][0]['name']
                try:
                    track_dict[name]['song_popularity'] = track['popularity']
                except KeyError:
                    track_dict[name]['song_popularity'] = np.nan
                    pass
                track_dict[name]['album'] = album['name']
kotutuloro commented 7 years ago

( Not sure if you still need help on this one but: ) Although popularity is usually given in Spotify's track objects, when you're using the endpoint that gets an album's tracks, you're actually getting a simplified version of the track objects which doesn't include popularity.

You can check out what's included in the response at this endpoint. If you need the popularity, you may have to make separate requests for the full track information for each track using its id.

lyons7 commented 5 years ago

Hey @fhopp did you ever find a solution to this problem?