-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmood_api_call.py
133 lines (114 loc) · 4.47 KB
/
mood_api_call.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#Modified from codingforentrepreneurs' Reusable Spotify API Client
#Link for Reusable Client: https://github.com/codingforentrepreneurs/30-Days-of-Python/blob/master/tutorial-reference/Day%2019/client/spotify_client.py
import base64
import datetime
import time
from urllib.parse import urlencode
import requests
class SpotifyAPI(object):
access_token = None
access_token_expires = datetime.datetime.now()
access_token_did_expire = True
client_id = None
client_secret = None
token_url = "https://accounts.spotify.com/api/token"
def __init__(self, client_id, client_secret, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client_id = client_id
self.client_secret = client_secret
def get_client_credentials(self):
"""
Returns a base64 encoded string
"""
client_id = self.client_id
client_secret = self.client_secret
if client_secret == None or client_id == None:
raise Exception("You must set client_id and client_secret")
client_creds = f"{client_id}:{client_secret}"
client_creds_b64 = base64.b64encode(client_creds.encode())
return client_creds_b64.decode()
def get_token_headers(self):
client_creds_b64 = self.get_client_credentials()
return {
"Authorization": f"Basic {client_creds_b64}"
}
def get_token_data(self):
return {
"grant_type": "client_credentials"
}
def perform_auth(self):
token_url = self.token_url
token_data = self.get_token_data()
token_headers = self.get_token_headers()
r = requests.post(token_url, data=token_data, headers=token_headers)
if r.status_code not in range(200, 299):
raise Exception("Could not authenticate client.")
# return False
data = r.json()
now = datetime.datetime.now()
access_token = data['access_token']
expires_in = data['expires_in'] # seconds
expires = now + datetime.timedelta(seconds=expires_in)
self.access_token = access_token
self.access_token_expires = expires
self.access_token_did_expire = expires < now
return True
def get_access_token(self):
token = self.access_token
expires = self.access_token_expires
now = datetime.datetime.now()
if expires < now:
self.perform_auth()
return self.get_access_token()
elif token == None:
self.perform_auth()
return self.get_access_token()
return token
def get_resource_header(self):
access_token = self.get_access_token()
headers = {
"Authorization": f"Bearer {access_token}"
}
return headers
def get_resource(self, batch_id, resource_type='audio-features', version='v1'):
endpoint = f"https://api.spotify.com/{version}/{resource_type}/?ids={batch_id}"
headers = self.get_resource_header()
r = requests.get(endpoint, headers=headers)
if r.status_code not in range(200, 299):
return {}
return r.json()
#Fill in client_id and client_secret
client_id= ""
client_secret= ""
start2= time.time()
print("Clock started")
client= SpotifyAPI(client_id, client_secret)
client.get_client_credentials()
client.get_token_headers()
client.get_token_data()
client.perform_auth()
client.get_access_token()
client.get_resource_header()
audioresponses=list()
concatls_len= len(concatls)
for index in range(concatls_len):
audiojson= client.get_resource(batch_id=concatls[index])
audioresponses.append(audiojson)
valencels=list()
energyls=list()
idcheckls=list()
for index2 in range(len(audioresponses)):
#for index3 in range(len(audioresponses[index2])):
for index3 in range(len(audioresponses[index2]["audio_features"])):
valencels.append(audioresponses[index2]["audio_features"][index3]["valence"])
energyls.append(audioresponses[index2]["audio_features"][index3]["energy"])
idcheckls.append(audioresponses[index2]["audio_features"][index3]["id"])
valencedf= pd.DataFrame(valencels)
energydf= pd.DataFrame(energyls)
idcheckdf= pd.DataFrame(idcheckls)
combineddf= pd.concat([valencedf, energydf, idcheckdf], axis=1, sort=False)
combineddf.columns = ["Valence", "Energy", "ID Check"]
combineddf.to_csv("all_mood_features2.csv", index= False)
end2= time.time()
print("Seconds: %.2f"
% (end2-start2))