I am creating a user dashboard using plotly and flask to display the top tracks and top artists preferred by a user. My code has been working fine but suddenly I got this error. Also, in the same application, I was able to create a new playlist on spotify by extracting user's past playlists through recommendations by using the spotify API directly but wasn't able to extract the user top tracks. Does it have to do with an expired access token? I may have to read more on this but it would be great if you can help me on that.
These are the code blocks
def get_user_top_tracks_artists(self, entity_type="artists", limit=50,time_range="medium_term", offset=0):
"""
:param entity_type: artists or tracks
:param limit: The number of entities to return. Limit is 50
:param time_range: Over what time frame the affinities are computed. Valid values: long_term
(calculated from several years of data and including all new data as it becomes available),
medium_term (approximately last 6 months), short_term (approximately last 4 weeks)
:param offset: The index of the first entity to return. Default: 0 (i.e., the first track).
Use with limit to get the next set of entities
:return: json text
"""
endpoint = f"https://api.spotify.com/v1/me/top/{entity_type}?time_range={time_range}&limit={limit}&offset={offset}"
track_artists = json.loads(requests.get(endpoint, headers=self.authorization_header).text)
return track_artists`
```
```
def df_get_user_top_track_artists(self, entity_type="artists", time_range="medium-term"):
"""
:return a pandas DataFrame
"""
total_top_entity = self.get_user_top_tracks_artists(entity_type=entity_type, limit=1,time_range=time_range, offset=0)["total"]
print(total_top_entity)
user_top_entity_data = pd.DataFrame()
for i in range(int(total_top_entity/50)+1):
temp_json = self.get_user_top_tracks_artists(entity_type=entity_type, limit=50,
time_range=time_range, offset=i*50)
if entity_type == "artists":
temp = us.df_user_top_artists(temp_json)
else:
temp = us.df_user_top_tracks(temp_json)
user_top_entity_data = pd.concat([user_top_entity_data, temp])
return user_top_entity_data
```
user_data.py code function
```
def df_user_top_artists(data):
"""
:param data: in json format
:return pandas dataframe
"""
name, popularity, image_url, external_url, followers, genres = [], [], [], [], [], []
for artist in data['items']:
name.append(artist['name'])
popularity.append(artist['popularity'])
try:
image_url.append(artist['images'][1]['url'])
except:
try:
image_url.append(artist['images'][0]['url'])
except IndexError:
image_url.append(np.nan)
external_url.append(artist['external_urls']['spotify'])
followers.append(artist['followers']['total'])
genres.append(artist['genres'])
user_top_artist_data = pd.DataFrame(columns=['name','followers','popularity','genres','image_url', 'external_url'])
user_top_artist_data['name'] = name
user_top_artist_data['followers'] = followers
user_top_artist_data['popularity'] = popularity
user_top_artist_data['genres'] = genres
user_top_artist_data['image_url'] = image_url
user_top_artist_data['external_url'] = external_url
return user_top_artist_data