7 PowerBI and python
I think that despite having all the data, there is still a lack of it that Spotify can give me about my listening habits. For this I will use the API to extract the audio features of the songs, the extracted data will be the following:
- Instrumentalness
- Acousticness
- Danceability
- Energy
- Liveness
- Speechiness
Documentation of these factors can be found here
To extract this data I use the following python script.
import pandas as pd
import requests
# Function to get the token
def get_token():
= 'https://accounts.spotify.com/api/token'
url = requests.post(url, {
auth_response 'grant_type': 'client_credentials',
'client_id': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'client_secret': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
})if auth_response.status_code != 200:
raise Exception('Error getting token')
else:
= auth_response.json()
auth_response_data return auth_response_data['access_token']
# Get token
= get_token()
access_token = {
header 'Authorization': 'Bearer {token}'.format(token=access_token),
'accept':'application/json'
}= 'https://api.spotify.com/v1/audio-features/'
base_url =pd.read_csv('endsong_9 copy.csv')
dataframe=0
index_startpoint#get the audio features for each song strating from the index_startpoint
for index, row in dataframe.iterrows():
if index >= index_startpoint:
= base_url + row['track_id']
url = requests.get(url, headers=header)
response if response.status_code != 200:
print('Error getting data')
else:
= response.json()
response_data 'acousticness'] = response_data['acousticness']
dataframe.loc[index, 'danceability'] = response_data['danceability']
dataframe.loc[index, 'energy'] = response_data['energy']
dataframe.loc[index, 'instrumentalness'] = response_data['instrumentalness']
dataframe.loc[index, 'liveness'] = response_data['liveness']
dataframe.loc[index, 'loudness'] = response_data['loudness']
dataframe.loc[index, 'speechiness'] = response_data['speechiness']
dataframe.loc[index, 'tempo'] = response_data['tempo']
dataframe.loc[index, 'valence'] = response_data['valence']
dataframe.loc[index, 'duration_ms'] = response_data['duration_ms']
dataframe.loc[index, 'time_signature'] = response_data['time_signature']
dataframe.loc[index, 'key'] = response_data['key']
dataframe.loc[index, 'mode'] = response_data['mode']
dataframe.loc[index, #save log in file
with open('log.log', 'a') as f:
'Song: ' + str(index) + ' - ' + dataframe['track_id'][index])
f.write(#save the dataframe in a csv file
'Audio_Features.csv', index=False) dataframe.to_csv(
This resulting dataframe must be imported into PowerBi and the relationships with the previous dataset must be created:
7.0.1 Radar Chart
With the help of some radar charts, I can show the variance of these values over the years. The final result is the following:
If you want to download the PowerBI file (pbix) you can do it from this link