-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotifyclient.py
252 lines (207 loc) · 9.28 KB
/
spotifyclient.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import requests
import json
import base64
"""
@todo:
Merge this class with merge spotify.py
Some functions are in both, but the important functions in this file pertain
to creating and modifying Spotify playlists for a user
"""
RECENT_TRACKS_ENDPOINT = 'https://api.spotify.com/v1/me/player/recently-played?limit=5'
PLAYLISTS_ENDPOINT = 'https://api.spotify.com/v1/me/playlists'
CREATE_PLAYLISTS_ENDPOINT = 'https://api.spotify.com/v1/users/{}/playlists'
MODIFY_TRACKS_PLAYLISTS_ENDPOINT = 'https://api.spotify.com/v1/playlists/{}/tracks'
CHANGE_PLAYLIST_COVER_ENDPOINT = 'https://api.spotify.com/v1/playlists/{}/images'
DEFAULT_PLAYLIST_COVER = 'tempoture-logo.jpeg'
DEFAULT_PLAYLIST_NAME = 'Your Tempoture Playlist'
DEFAULT_PLAYLIST_DESC = 'A custom-made playlist made by Tempoture for you!'
class SpotifyClient:
def __init__(self, access_token, user_id):
"""
:param access_token (str): needed to fetch or push data from Spotify API
:param user_id (str): needed for playlist updates on user's account
"""
self.access_token = access_token
self.user_id = user_id
# https://developer.spotify.com/web-api/web-api-personalization-endpoints/get-recently-played
# Access Token requires scope: user-read-recently-played
def get_recent_tracks(self, after=None):
"""
:param after (int): optional timestamp in milliseconds
Output:
trackURIs = list of Spotify track URIs from user's recently played
"""
url = RECENT_TRACKS_ENDPOINT
if after is not None:
url += ('&after=' + str(after))
response = self.get_api_request(url)
trackNames = [track["track"]["name"] for track in response.json()["items"]]
trackURIs = [track["track"]["uri"] for track in response.json()["items"]]
return trackURIs
def get_current_user_playlists(self):
url = PLAYLISTS_ENDPOINT
response = self.get_api_request(self, url)
return response.json()
# https://developer.spotify.com/documentation/web-api/reference/#endpoint-create-playlist
# Scopes needed: playlist-modify-public & playlist-modify-private
def create_playlist(self, name=None, desc=None):
"""
:param name (str): = optional argument for custom name of new playlist, will default to hardcoded string if none
:param desc (str): = optional argument for custom description of new playlist, will default to hardcoded string if none
Output:
playlist_id (int): id of the newly created playlist
"""
url = CREATE_PLAYLISTS_ENDPOINT.format(self.user_id)
if name is None:
name = "Custom Tempoture Playlist"
# Allow empty descriptions?
data = json.dumps({
"name": name,
"public": True,
"collaborative": False,
"description": desc
})
response = self.post_api_request(url, data)
resp_json = response.json()
playlist_id = -1
try:
playlist_id = resp_json["id"]
except Exception:
print("Status " + str(resp_json["error"]["status"]) + ": " + resp_json["error"]["message"])
return playlist_id
def change_playlist_cover(self, playlist_id, cover=None):
"""
Input:
:param playlist_id (int): Spotify playlist id
:param cover (str): optional argument for custom album cover photo, will default to hardcoded encoded string if none
@todo:
Output:
a bool which determines if cover change went through successfully
"""
url = CHANGE_PLAYLIST_COVER_ENDPOINT.format(playlist_id)
if (cover is None):
cover = DEFAULT_PLAYLIST_COVER
encoded_string = ""
with open(cover, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
file = encoded_string
response = requests.put(
url,
headers={
"Content-Type": "image/jpeg",
"Authorization": f"Bearer {self.access_token}"
},
data = encoded_string
)
return True
def add_playlist_tracks(self, playlist_id, track_uri_data):
"""
Input:
:param playlist_id (int): id of Spotify playlist being modified
:param track_uri_data (list): a list of Spotify track URI's , ex. ['spotify:track:xxxxxx', 'spotify:track:yyyyyyy']
Output:
snapshot_id = updated snapshot_id of the modified playlist (-1 if error occurred)
"""
data = json.dumps(track_uri_data)
url = MODIFY_TRACKS_PLAYLISTS_ENDPOINT.format(playlist_id)
response = self.post_api_request(url, data)
resp_json = response.json()
snapshot_id = -1
try:
snapshot_id = resp_json["snapshot_id"]
except Exception:
print("Status " + str(resp_json["error"]["status"]) + ": " + resp_json["error"]["message"])
return snapshot_id
def remove_playlist_tracks(self, playlist_id, track_uri_data, snapshot_id):
"""
Input:
:param playlist_id (int): id of Spotify playlist being modified
:param track_uri_data (list): a list of Spotify track URIs to remove. Formatted ex: { "tracks": [{ "uri": "spotify:track:4iV5W9uYEdYUVa79Axb7Rh" },{ "uri": "spotify:track:1301WleyT98MSxVHPZCA6M" }] }
:param snapshot_id (int): (optional) playlist’s snapshot ID against which you want to make the changes
@todo:
Output:
snapshot_id = updated snapshot_id of the modified playlist (-1 if error occurred)
"""
data = json.dumps(track_uri_data)
url = MODIFY_TRACKS_PLAYLISTS_ENDPOINT.format(playlist_id)
response = self.delete_api_request(url, data)
resp_json = response.json()
snapshot_id = -1
try:
snapshot_id = resp_json["snapshot_id"]
except Exception:
print("Status " + str(resp_json["error"]["status"]) + ": " + resp_json["error"]["message"])
print(resp_json)
return snapshot_id
def get_api_request(self, url):
response = requests.get(
url,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {self.access_token}"
}
)
return response
def post_api_request(self, url, data):
response = requests.post(
url,
data=data,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {self.access_token}"
}
)
return response
def delete_api_request(self, url, data):
response = requests.delete(
url,
data=data,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {self.access_token}"
}
)
return response
# Create a new demo playlist and populate with your 5 most recent songs
def test_create_and_populate(self, name=None, desc=None):
"""
:param name (str): = optional argument for custom name of new playlist, will default to hardcoded string if none
:param desc (str): = optional argument for custom description of new playlist, will default to hardcoded string if none
Output:
snapshot_id (int): a Spotify snapshot id which represents the specific playlist version
"""
playlist_id = self.create_playlist(name, desc)
self.change_playlist_cover(playlist_id)
url = MODIFY_TRACKS_PLAYLISTS_ENDPOINT.format(playlist_id)
track_uri_data = self.get_recent_tracks()
data = json.dumps(track_uri_data)
response = self.post_api_request(url, data)
resp_json = response.json()
snapshot_id = resp_json["snapshot_id"]
return snapshot_id
# Create a new demo playlist and populate with your 5 most recent songs
def create_tempoture_playlist(self, track_uri_data, name=None, desc=None, cover=None):
"""
Input:
:param track_uri_data (list): a list of Spotify track URI's , ex. ['spotify:track:xxxxxx', 'spotify:track:yyyyyyy']
:param name (str): optional argument for custom name of new playlist, will default to hardcoded string if none
:param desc (str): optional argument for custom description of new playlist, will default to hardcoded string if none
:param cover (str): optional argument for custom album cover photo, will default to hardcoded encoded string if none
Output:
playlist_id (int): id of newly created custom Spotify playlist
"""
if name is None:
name = DEFAULT_PLAYLIST_NAME
if desc is None:
desc = DEFAULT_PLAYLIST_DESC
if cover is None:
cover = DEFAULT_PLAYLIST_COVER
playlist_id = self.create_playlist(name, desc)
if playlist_id == -1:
raise Exception("Could not create playlist.")
if self.change_playlist_cover(playlist_id) == False:
raise Exception("Could not change playlist cover.")
snapshot_id = self.add_playlist_tracks(playlist_id, track_uri_data)
if snapshot_id == -1:
raise Exception("Could not add tracks to playlist.")
return playlist_id