Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

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

4 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?

Suggested posts