-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs.py
80 lines (65 loc) · 2.65 KB
/
funcs.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
import requests
class Spotify:
def __init__(self, token):
self.token = token
def create_playlist(self, playlist_name, description):
headers = {"Accept": "application/json", "Content-Type": "application/json", "Authorization": "Bearer " + self.token}
data = {
"name": playlist_name,
"description": description,
"public": False
}
response = requests.post(f"https://api.spotify.com/v1/users/piggyawesomeyt/playlists", json=data, headers=headers)
return response
def convert_tracks(self, tracks):
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {self.token}',
}
spotify_tracks = []
errors = []
for track in tracks:
name = track[0].replace('\'', '')
artist = track[1].replace('\'', '')
params = {
'q': f"track:{name} artist:{artist}",
'type': 'track,artist',
'market': 'ES',
'limit': '1',
'offset': '0',
}
response = requests.get('https://api.spotify.com/v1/search', params=params, headers=headers)
try:
print("[SPOTIFY] Found: " + response.json()["tracks"]["items"][0]["name"] + " | " + str(response.status_code))
spotify_tracks.append(response.json()["tracks"]["items"][0]["id"])
except IndexError:
print("ERROR: " + name + " | " + artist + " | " + str(response.status_code))
errors.append([name, artist])
pass
except KeyError:
print("ERROR: " + name + " | " + artist + " | " + str(response.status_code))
errors.append([name, artist])
pass
return spotify_tracks, errors
def add_tracks(self, tracks, playlist):
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {self.token}',
}
uris = []
for track in tracks:
uris.append(f'spotify:track:{track}')
params = {
'uris': ','.join(uris),
}
print(playlist, params, headers)
response = requests.post(playlist + "/tracks", params=params, headers=headers)
return response
class Deezer:
def __init__(self, playlist):
self.playlist = playlist
def get_tracks(self, data):
response = requests.get(f"https://api.deezer.com/playlist/{self.playlist}/tracks?{data}")
return response