Skip to content

Commit

Permalink
Workaround genre from artist profile
Browse files Browse the repository at this point in the history
  • Loading branch information
casualsnek committed Apr 27, 2023
1 parent 245a391 commit 5f7453a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions utils/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,18 @@ def check_premium(session):

def get_song_info(session, song_id):
token = session.tokens().get("user-read-email")
print(token, song_id)
uri = 'https://api.spotify.com/v1/tracks?ids=' + song_id + '&market=from_token'
info = json.loads(requests.get(uri, headers={"Authorization": "Bearer %s" % token}).text)
album_url = info['tracks'][0]['album']['href']
artist_url = info['tracks'][0]['artists'][0]['href']
if album_url not in rt_cache['REQurl']:
rt_cache['REQurl'][album_url] = json.loads(
requests.get(album_url, headers={"Authorization": "Bearer %s" % token}).text
)
if artist_url not in rt_cache['REQurl']:
rt_cache['REQurl'][artist_url] = json.loads(
requests.get(artist_url, headers={"Authorization": "Bearer %s" % token}).text
)
artists = []
for data in info['tracks'][0]['artists']:
artists.append(sanitize_data(data['name']))
Expand All @@ -268,9 +272,10 @@ def get_song_info(session, song_id):
'is_playable': info['tracks'][0]['is_playable'],
'popularity': info['tracks'][0]['popularity'],
'isrc': info['tracks'][0]['external_ids'].get('isrc', ''),
'genre': info['tracks'][0].get('genres', []),
'genre': rt_cache['REQurl'][artist_url]['genres'],
# https://developer.spotify.com/documentation/web-api/reference/get-track
# List of genre is supposed to be here, genre from album API is deprecated
# List of genre is supposed to be here, genre from album API is deprecated and it always seems to be unavailable
# Use artist endpoint to get artist's genre instead
'label': rt_cache['REQurl'][album_url]['label'],
'copyrights': [ holder['text'] for holder in rt_cache['REQurl'][album_url]['copyrights'] ],
'explicit': info['tracks'][0]['explicit']
Expand Down

0 comments on commit 5f7453a

Please sign in to comment.