Announcements

Help Wizard

Step 1

NEXT STEP

retrieving genre of track in metadata

Solved!

retrieving genre of track in metadata

My Question or Issue

I'm writing a scritpt to try to retrieve the genre from a track using the metadata of that song. I am not well versed in the spotify API, so i do need some information 😅.
btw i am writing it in Python and I'm using spotipy to access the API.

My question would be:
Is it even possible to retrieven the genre of a certian track? (bc it doesn't seem to work for me lol). If yes, how?

 

Thanks!

Reply

Accepted Solutions
Marked as solution

At Spotify, tracks don't have genres, but artists do have them.

Here is code to get genre information of a single track:

 

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

CLIENT_ID = "your_client_id"
CLIENT_SECRET = "your_client_secret"

track_id = "TRACK_ID"

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=CLIENT_ID,client_secret=CLIENT_SECRET))

track_data = sp.track(track_id)

artist_ids = []

for artist in track_data["artists"]:
    artist_ids.append(artist["id"])

artists_data = sp.artists(artist_ids)

genres = []

for artist in artists_data["artists"]:
    genres += artist["genres"]

genres = set(genres) # removes duplicates

print(genres)

View solution in original post

5 Replies
Marked as solution

At Spotify, tracks don't have genres, but artists do have them.

Here is code to get genre information of a single track:

 

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

CLIENT_ID = "your_client_id"
CLIENT_SECRET = "your_client_secret"

track_id = "TRACK_ID"

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=CLIENT_ID,client_secret=CLIENT_SECRET))

track_data = sp.track(track_id)

artist_ids = []

for artist in track_data["artists"]:
    artist_ids.append(artist["id"])

artists_data = sp.artists(artist_ids)

genres = []

for artist in artists_data["artists"]:
    genres += artist["genres"]

genres = set(genres) # removes duplicates

print(genres)

Thanks for your help!

Hello, I have implemented this into my code and  I don't know why, it always gets stuck in the line: 

'artists_data = sp.artists(artist_ids)'
Can someone help me please? thanks for your time.

Hi TionEye,

Do you get any errors?

This is extremely frustrating because this issue has existed since atleast 2015 and the docs still show in their examples that you can get genres from tracks and albums. But that's a flat out lie. After dozens of github issue requests and dozens more on this newer platform for issue tracking, nobody has gave a single care to address this in almost 10 years. All that has to be done is to remove 1 line from the example requests for tracks and albums and no more questions about this topic will come up.

Suggested posts