-
Notifications
You must be signed in to change notification settings - Fork 73
/
using-spotify-track.py
91 lines (80 loc) · 3.17 KB
/
using-spotify-track.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
import requests
import base64
import datetime
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_secret == None:
raise Exception('You must set client_ID and client_secret')
client_creds = f'{client_id}:{client_secret}'
client_creds_base64 = base64.b64encode(client_creds.encode())
return client_creds_base64.decode()
def get_token_headers(self):
client_creds_base64 = self.get_client_credentials()
return {
'Authorization': f'Basic {client_creds_base64}'
}
def get_token_data(self):
return {
'grant_type': 'client_credentials'
}
def perfom_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.")
now = datetime.datetime.now()
data = r.json()
access_token = data['access_token']
expires_in = data['expires_in']
expires = now + datetime.timedelta(seconds=expires_in)
self.access_token_expires = expires
self.access_token_did_expire = expires < now
self.access_token = access_token
return True
def get_access_token(self):
token = self.access_token
expires = self.access_token_expires
now = datetime.datetime.now()
if expires < now:
self.perfom_auth()
return self.get_access_token()
elif token == None:
self.perfom_auth()
return self.get_access_token()
return token
def spotify_track(self, link):
access_token = self.get_access_token()
id = link[31:53]
headers = {
'Authorization': f'Bearer {access_token}'
}
endpoint = 'https://api.spotify.com/v1/tracks'
lookup_url = f"{endpoint}/{id}"
r = requests.get(lookup_url, headers=headers)
if r.status_code in range(200, 299):
data = r.json()
track_name = data['name']
artist_name = data['artists'][0]['name']
return [track_name,artist_name]
client_id = '9f31c31d0a4c40178302ff79ccfc59df'
client_secret = '87ed19e8b607442e9a871ec4f183a499'
spotify = SpotifyAPI(client_id, client_secret)
# print(''.join(x for x in str(spotify.spotify_track('https://open.spotify.com/track/21AUdfi6fLFDp9JuNcHsfS')[0]) if x not in [',']))
print(str(spotify.spotify_track('https://open.spotify.com/track/21AUdfi6fLFDp9JuNcHsfS')[0])+'!')