-
Notifications
You must be signed in to change notification settings - Fork 2
/
Playlist.py
101 lines (91 loc) · 3.58 KB
/
Playlist.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
import logging
import AppContext
import requests
import sys
import json
import math
__all__ = ['Playlist']
class Playlist:
def __init__(self, item):
self.id = item["id"]
self.name = item["name"]
self.public = item["public"]
self.collaborative = item["collaborative"]
self.owner = item["owner"]["id"]
def follow_collaborative(self):
payload = {
'public': AppContext.olduser.playlists[self.id].public
}
headers = {
'Authorization': 'Bearer ' + AppContext.newuser.access_token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
url = 'https://api.spotify.com/v1/users/'+self.owner+'/playlists/'+self.id+'/followers'
response = requests.put(url, data=payload, headers=headers, verify=False)
print response
def copy_playlist(self):
try:
tracks = self._load_tracks()
uris = []
for item in tracks:
uris.append(item["track"]["uri"])
new_playlist_id = self._create_playlist()
self._add_tracks_to_playlist(uris, new_playlist_id)
except requests.exceptions.HTTPError:
msg = "Could not copy playlist \"%s\" id: %s" % (self.name, self.id)
logging.error(msg)
def _load_tracks(self):
tracks_json = self._fetch_tracks(0)
tracks = tracks_json["items"]
total = tracks_json["total"]
if total > 100:
pages = int(math.ceil(float(total)/100))
for i in range(1, pages + 1):
tracks_json = self._fetch_tracks(i * 100)
tracks += tracks_json["items"]
return tracks
def _fetch_tracks(self, offset):
headers = {
'Authorization': 'Bearer ' + AppContext.olduser.access_token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
payload = {
'fields': 'total,items(track.uri)',
'offset': offset
}
url = 'https://api.spotify.com/v1/users/'+AppContext.olduser.user_id+'/playlists/'+self.id+'/tracks'
response = requests.get(url, params=payload, headers=headers, verify=False)
response.raise_for_status()
response = response.json()
return response
def _create_playlist(self):
headers = {
'Authorization': 'Bearer ' + AppContext.newuser.access_token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
payload = {
'name': self.name,
'public': self.public
}
url = 'https://api.spotify.com/v1/users/'+AppContext.newuser.user_id+'/playlists'
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False)
response = response.json()
return response["id"]
def _add_tracks_to_playlist(self, tracks, playlist_id):
headers = {
'Authorization': 'Bearer ' + AppContext.newuser.access_token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
pages = int(math.ceil(float(len(tracks)) / 100))
for i in range(0, pages):
payload = {
'uris': tracks[i*100:(i*100)+100]
}
print str(len(payload['uris']))
url = 'https://api.spotify.com/v1/users/'+AppContext.newuser.user_id+'/playlists/'+playlist_id+'/tracks'
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False)
response = response.json()