-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_client.py
47 lines (34 loc) · 1.26 KB
/
spotify_client.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
import requests
import urllib.parse
class SpotifyClient(object):
def __init__(self, api_token):
self.api_token = api_token
def search_song(self, artist, track):
query = urllib.parse.quote(f'{artist} {track}')
url = f"https://api.spotify.com/v1/search?q={query}&type=track"
response = requests.get(
url,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_token}"
}
)
response_json = response.json()
results = response_json['track']['item']
if results:
# Predicting the first track in the list is the song we want
return results[0]['id']
else:
raise Exception(f"No song found for {artist} = {track}")
def add_song_to_spotify(self, song_id):
url = "https://api.spotify.com/v1/me/tracks"
response = requests.put(
json={
"ids": [song_id]
},
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_token}"
}
)
return response.ok