-
Notifications
You must be signed in to change notification settings - Fork 3
/
transfer.py
166 lines (121 loc) · 5.05 KB
/
transfer.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
### Get tokens read/modify tokens from, e.g.,
### https://developer.spotify.com/web-api/console/put-following/#complete
READ_AUTH_TOKEN="[]" # For account locked to facebook
MODIFY_AUTH_TOKEN="[]" # For new account tied to an email address
import time
import json
import numpy as np
import urllib2
def run_transfer():
## Albums
album_list = fetch_album_list(limit=50)
put_album_list(album_list)
## Followed artists
artist_list = fetch_artist_list()
put_artist_list(artist_list)
class MethodRequest(urllib2.Request):
"""
PUT request with urllib2
From GitHub @logic: https://gist.github.com/logic/2715756
"""
def __init__(self, *args, **kwargs):
if 'method' in kwargs:
self._method = kwargs['method']
del kwargs['method']
else:
self._method = None
return urllib2.Request.__init__(self, *args, **kwargs)
def get_method(self, *args, **kwargs):
if self._method is not None:
return self._method
return urllib2.Request.get_method(self, *args, **kwargs)
def fetch_album_list(limit=50, output='my_spotify_albums.json'):
total = 10000
offset = 0
albums = []
first = True
while offset < total:
request = urllib2.Request("https://api.spotify.com/v1/me/albums?limit=%d&offset=%d" %(limit, offset))
request.add_header("Authorization", "Bearer %s" %(READ_AUTH_TOKEN))
request.add_header("Accept", "application/json")
response = urllib2.urlopen(request)
data = json.load(response)
if first:
total = data['total']
first = False
albums.extend(data['items'])
print 'Fetch albums %d - %d of %d' %(offset, offset+limit, total)
offset += limit
out = {'items': albums}
fp = open(output,'w')
json.dump(out, fp)
fp.close()
return out
def fetch_artist_list(limit=50, output='my_spotify_artists.json'):
total = 10000
offset = 0
artists = []
first = True
url = "https://api.spotify.com/v1/me/following?type=artist&limit=%d" %(limit)
while offset < total:
request = urllib2.Request(url)
request.add_header("Authorization", "Bearer %s" %(READ_AUTH_TOKEN))
request.add_header("Accept", "application/json")
response = urllib2.urlopen(request)
data = json.load(response)
if first:
total = data['artists']['total']
first = False
artists.extend(data['artists']['items'])
print 'Fetch artists %d - %d of %d' %(offset, offset+limit, total)
offset += limit
url = data['artists']['next']
out = {'artists': {'items': artists}}
fp = open(output,'w')
json.dump(out, fp)
fp.close()
return out
def show_albums(album_list):
for i, item in enumerate(album_list['items']):
album = item['album']
artists = [artist['name'] for artist in album['artists']]
print '%4d: %s / %s' %(i, album['name'], ','.join(artists))
def show_artists(artist_list):
for i, item in enumerate(artist_list['artists']['items']):
print '%4d: %s / %s' %(i, item['name'], item['id'])
def put_artist_list(artist_list):
#artist_list = json.loads(open('my_spotify_artists.json','r').read())
artist_names = [artist['name'] for artist in artist_list['artists']['items']]
limit=20
N = len(artist_list['artists']['items'])
offset = 0
while offset < N:
if offset+limit > N:
list_i = artist_list['artists']['items'][offset:]
else:
list_i = artist_list['artists']['items'][offset:offset+limit]
print 'Send artists %d - %d' %(offset, offset+limit)
offset += limit
artist_ids = [str(artist['id']) for artist in list_i]
artist_str = ','.join(artist_ids)
url = "https://api.spotify.com/v1/me/following?type=artist&ids=%s" %(artist_str)
request = MethodRequest(url, method='PUT') #, data=artist_str)
request.add_header("Authorization", "Bearer %s" %(MODIFY_AUTH_TOKEN))
request.add_header("Accept", "application/json")
response = urllib2.urlopen(request)
def put_album_list(album_list):
### add them in order of "added_at" time
dates = [item['added_at'] for item in album_list['items']]
so = np.argsort(dates)
for ix, i in enumerate(so):
album_ids = [str(album_list['items'][i]['album']['id'])]
album_name = album_list['items'][i]['album']['name']
#album_ids = [str(id) for id in album_list[0:10]]
album_str = ("%s" %(album_ids)).replace('\'', '\"')
url = "https://api.spotify.com/v1/me/albums"
request = MethodRequest(url, method='PUT', data=album_str)
request.add_header("Authorization", "Bearer %s" %(MODIFY_AUTH_TOKEN))
request.add_header("Content-Type", "application/json")
print '%d/%d: %s' %(ix, len(so), album_name)
time.sleep(1.1)
response = urllib2.urlopen(request)