-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
102 lines (80 loc) · 3.14 KB
/
api.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
import requests
class API:
def __init__(self, client_id, client_secret):
self.client_id = client_id
self.client_secret = client_secret
self.access_token = self._get_access_token()
def _get_access_token(self):
"""Posts a request to get the access token."""
# Authorization Data
auth_url = "https://accounts.spotify.com/api/token"
data = {
"grant_type": "client_credentials",
"client_id": self.client_id,
"client_secret": self.client_secret,
}
# Getting an Access Token
auth_response = requests.post(auth_url, data=data)
status_code = auth_response.status_code
auth_response_data = auth_response.json()
if status_code == 200:
access_token = auth_response_data.get("access_token")
return access_token
else:
print(auth_response_data["error_description"])
return 0
def _get_headers(self):
"""The headers for the GET request"""
access_token = self.access_token
headers = {
"Authorization": "Bearer {token}".format(token=access_token),
"Content-Type" : "application/json"
}
return headers
def _build_params(self, artist, track):
"""Builds the parameters as described in https://developer.spotify.com/documentation/web-api/reference/search.
Args:
artist (str): The name of the artist.
track (str): The name of the track.
Returns:
dict: A dictionary
"""
params = {
"q" : artist + " " + track,
"artist": artist,
"track" : track,
"type" : "track", # Allowed values: "album", "artist", "playlist", "track", "show", "episode", "audiobook"
"limit" : 1,
"offset": 0
}
return params
def search_for_track(self, search_url, artist, track):
# Setting up headers and params for a GET request
headers = self._get_headers()
params = self._build_params(artist, track)
result = requests.get(search_url,
headers=headers,
params=params)
data = result.json()
items = data
return items
def map_results(self, search):
"""_summary_
Args:
search (_type_): _description_
Returns:
_type_: _description_
"""
results = {}
track = search["tracks"]["items"][0]
album = track["album"]
artist = album["artists"][0]
images = album["images"]
results["track_name"] = track["name"]
results["artist_name"] = artist["name"]
results["track_number"] = track["track_number"]
results["album_name"] = album["name"]
results["album_image_640"] = images[0]["url"]
results["album_image_300"] = images[1]["url"]
results["album_image_64"] = images[2]["url"]
return results