-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathSpotify.py
355 lines (334 loc) · 14.8 KB
/
Spotify.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# TODO
# limit number of tracks from same artist
# remove exact duplicates (mp3vec proximity 1)
import os
import numpy as np
import pickle
import argparse
import spotipy
import spotipy.util as util
import random
import requests
import webbrowser
# If you want to be able to load your playlists into Spotify
# you will need to get credentials from https://developer.spotify.com/dashboard/applications
scope = 'playlist-modify-public'
client_id = '194086cb37be48ebb45b9ba4ce4c5936'
client_secret = 'fb9fb4957a9841fcb5b2dbc7804e1e85'
redirect_uri = 'https://www.attentioncoach.es/'
epsilon_distance = 0.001
def download_file_from_google_drive(id, destination):
if os.path.isfile(destination):
return None
print(f'Downloading {destination}')
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params={'id': id}, stream=True)
token = get_confirm_token(response)
if token:
params = {'id': id, 'confirm': token}
response = session.get(URL, params=params, stream=True)
save_response_content(response, destination)
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
def add_track_to_playlist(sp, username, playlist_id, track_id, replace=False):
if sp is not None and username is not None and playlist_id is not None:
try:
if replace:
result = sp.user_playlist_replace_tracks(
username, playlist_id, [track_id])
else:
result = sp.user_playlist_add_tracks(username, playlist_id,
[track_id])
except spotipy.client.SpotifyException:
# token has probably gone stale
token = util.prompt_for_user_token(username, scope, client_id,
client_secret, redirect_uri)
sp = spotipy.Spotify(token)
if replace:
result = sp.user_playlist_replace_tracks(
username, playlist_id, [track_id])
else:
result = sp.user_playlist_add_tracks(username, playlist_id,
[track_id])
def most_similar(mp3tovecs, weights, positive=[], negative=[], noise=0):
if isinstance(positive, str):
positive = [positive] # broadcast to list
if isinstance(negative, str):
negative = [negative] # broadcast to list
similar = np.zeros((len(mp3tovecs[0]), 2, len(weights)), dtype=np.float64)
for k, mp3tovec in enumerate(mp3tovecs):
mp3_vec_i = np.sum([mp3tovec[i] for i in positive] +
[-mp3tovec[i] for i in negative],
axis=0)
mp3_vec_i += np.random.normal(0, noise, len(mp3_vec_i))
mp3_vec_i = mp3_vec_i / np.linalg.norm(mp3_vec_i)
for j, track_j in enumerate(mp3tovec):
if track_j in positive or track_j in negative:
continue
mp3_vec_j = mp3tovec[track_j]
similar[j, 0, k] = j
similar[j, 1, k] = np.dot(mp3_vec_i, mp3_vec_j)
return sorted(similar, key=lambda x: -np.dot(x[1], weights))
def most_similar_by_vec(mp3tovecs,
weights,
positives=None,
negatives=None,
noise=0):
similar = np.zeros((len(mp3tovecs[0]), 2, len(weights)), dtype=np.float64)
positive = negative = []
for k, mp3tovec in enumerate(mp3tovecs):
if positives is not None:
positive = positives[k]
if negatives is not None:
negative = negatives[k]
if isinstance(positive, str):
positive = [positive] # broadcast to list
if isinstance(negative, str):
negative = [negative] # broadcast to list
mp3_vec_i = np.sum([i for i in positive] + [-i for i in negative],
axis=0)
mp3_vec_i += np.random.normal(0, noise, len(mp3_vec_i))
mp3_vec_i = mp3_vec_i / np.linalg.norm(mp3_vec_i)
for j, track_j in enumerate(mp3tovec):
mp3_vec_j = mp3tovec[track_j]
similar[j, 0, k] = j
similar[j, 1, k] = np.dot(mp3_vec_i, mp3_vec_j)
return sorted(similar, key=lambda x: -np.dot(x[1], weights))
# create a musical journey between given track "waypoints"
def join_the_dots(sp, username, playlist_id, mp3tovecs, weights, ids, \
tracks, track_ids, n=5, noise=0, replace=True):
playlist = []
playlist_tracks = [tracks[_] for _ in ids]
end = start = ids[0]
start_vec = [mp3tovec[start] for k, mp3tovec in enumerate(mp3tovecs)]
for end in ids[1:]:
end_vec = [mp3tovec[end] for k, mp3tovec in enumerate(mp3tovecs)]
playlist.append(start)
add_track_to_playlist(sp, username, playlist_id, playlist[-1], replace
and len(playlist) == 1)
print(f'{len(playlist)}.* {tracks[playlist[-1]]}')
for i in range(n):
candidates = most_similar_by_vec(mp3tovecs,
weights,
[[(n - i + 1) / n * start_vec[k] +
(i + 1) / n * end_vec[k]]
for k in range(len(mp3tovecs))],
noise=noise)
for candidate in candidates:
track_id = track_ids[int(candidate[0][0])]
if track_id not in playlist + ids and tracks[
track_id] not in playlist_tracks:
break
playlist.append(track_id)
playlist_tracks.append(tracks[track_id])
add_track_to_playlist(sp, username, playlist_id, playlist[-1])
print(f'{len(playlist)}. {tracks[playlist[-1]]}')
start = end
start_vec = end_vec
playlist.append(end)
add_track_to_playlist(sp, username, playlist_id, playlist[-1])
print(f'{len(playlist)}.* {tracks[playlist[-1]]}')
return playlist
def make_playlist(sp, username, playlist_id, mp3tovecs, weights, seed_tracks, \
tracks, track_ids, size=10, lookback=3, noise=0, replace=True):
playlist = seed_tracks
playlist_tracks = [tracks[_] for _ in playlist]
for i in range(0, len(seed_tracks)):
add_track_to_playlist(sp, username, playlist_id, playlist[i], replace
and len(playlist) == 1)
print(f'{i+1}.* {tracks[seed_tracks[i]]}')
for i in range(len(seed_tracks), size):
candidates = most_similar(mp3tovecs,
weights,
positive=playlist[-lookback:],
noise=noise)
for candidate in candidates:
track_id = track_ids[int(candidate[0][0])]
if track_id not in playlist and tracks[
track_id] not in playlist_tracks:
break
playlist.append(track_id)
playlist_tracks.append(tracks[track_id])
add_track_to_playlist(sp, username, playlist_id, playlist[-1])
print(f'{i+1}. {tracks[playlist[-1]]}')
return playlist
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--user', type=str, help='Spotify username')
parser.add_argument('--playlist',
type=str,
help='Playlist name (must already exist')
parser.add_argument('--n',
type=int,
help='Size of playlist to generate (default 5)')
parser.add_argument('--creativity',
type=float,
help='Discover something new? (0-1, default 0.5)')
parser.add_argument(
'--lookback',
type=int,
help='Number of previous tracks to consider (default 3)')
parser.add_argument('--noise',
type=float,
help='Degree of randomness (0-1, default 0)')
parser.add_argument('--mp3',
type=str,
help='Start with sommething that sounds like this')
parser.add_argument('--mp3tovec',
type=str,
help='MP3ToVecs file (full path)')
parser.add_argument('--extend',
action='store_true',
help='Extend existing playlist')
args = parser.parse_args()
username = args.user
playlist_name = args.playlist
size = args.n
creativity = args.creativity
lookback = args.lookback
noise = args.noise
mp3_filename = args.mp3
user_mp3tovecs_filename = args.mp3tovec
if size is None:
size = 5
if creativity is None:
creativity = 0.5
if lookback is None:
lookback = 3
if noise is None:
noise = 0
if args.extend:
replace = False
else:
replace = True
sp = playlist_id = None
if username is not None and playlist_name is not None:
token = util.prompt_for_user_token(username, scope, client_id,
client_secret, redirect_uri)
if token is not None:
sp = spotipy.Spotify(token)
if sp is not None:
try:
playlists = sp.user_playlists(username)
if playlists is not None:
playlist_ids = [
playlist['id'] for playlist in playlists['items']
if playlist['name'] == playlist_name
]
if len(playlist_ids) > 0:
playlist_id = playlist_ids[0]
except:
pass
if playlist_id is None:
print(
f'Unable to access playlist {playlist_name} for user {username}'
)
download_file_from_google_drive('1Mg924qqF3iDgVW5w34m6Zaki5fNBdfSy',
'spotifytovec.p')
download_file_from_google_drive('1geEALPQTRBNUvkpI08B-oN4vsIiDTb5I',
'tracktovec.p')
download_file_from_google_drive('1Qre4Lkym1n5UTpAveNl5ffxlaAmH1ntS',
'spotify_tracks.p')
mp3tovecs = pickle.load(open('spotifytovec.p', 'rb'))
mp3tovecs = dict(
zip(mp3tovecs.keys(),
[mp3tovecs[_] / np.linalg.norm(mp3tovecs[_]) for _ in mp3tovecs]))
tracktovecs = pickle.load(open('tracktovec.p', 'rb'))
tracktovecs = dict(
zip(tracktovecs.keys(), [
tracktovecs[_] / np.linalg.norm(tracktovecs[_])
for _ in tracktovecs
]))
tracks = pickle.load(open('spotify_tracks.p', 'rb'))
track_ids = [_ for _ in mp3tovecs]
if mp3_filename is None or user_mp3tovecs_filename is None:
user_input = input('Search keywords: ')
input_tracks = []
while True:
if user_input == '':
break
ids = sorted([
track for track in mp3tovecs
if all(word in tracks[track].lower()
for word in user_input.lower().split())
],
key=lambda x: tracks[x])
for i, id in enumerate(ids):
print(f'{i+1}. {tracks[id]}')
while True:
user_input = input(
'Input track number, ENTER to finish, or search keywords: '
)
if user_input == '':
break
if user_input.isdigit() and len(ids) > 0:
if int(user_input) - 1 >= len(ids):
continue
id = ids[int(user_input) - 1]
input_tracks.append(id)
print(f'Added {tracks[id]} to playlist')
else:
break
print()
if len(input_tracks) == 0:
ids = [track for track in mp3tovecs]
input_tracks.append(ids[random.randint(0, len(ids))])
if len(input_tracks) > 1:
playlist = join_the_dots(sp,
username,
playlist_id, [mp3tovecs, tracktovecs],
[creativity, 1 - creativity],
input_tracks,
tracks,
track_ids,
n=size,
noise=noise,
replace=replace)
else:
playlist = make_playlist(sp,
username,
playlist_id, [mp3tovecs, tracktovecs],
[creativity, 1 - creativity],
input_tracks,
tracks,
track_ids,
size=size,
lookback=lookback,
noise=noise,
replace=replace)
else:
user_mp3tovecs = pickle.load(open(user_mp3tovecs_filename, 'rb'))
ids = most_similar_by_vec(mp3tovecs, [user_mp3tovecs[mp3_filename]],
topn=10)
for i, id in enumerate(ids):
print(f'{i+1}. {tracks[id[0]]} [{id[1]:.2f}]')
user_input = input('Input track number: ')
if user_input.isdigit(
) and int(user_input) > 0 and int(user_input) < len(ids):
print()
playlist = make_playlist(sp,
username,
playlist_id, [mp3tovecs, tracktovecs],
[creativity, 1 - creativity],
[ids[int(user_input) - 1][0]],
size=size,
lookback=lookback,
noise=noise,
replace=replace)
if sp is None or username is None or playlist_id is None:
with open("playlist.html", "w") as text_file:
for id in playlist:
text_file.write(
f'<iframe src="https://open.spotify.com/embed/track/{id}" width="100%" height="80" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>'
)
webbrowser.open('file://' + os.path.realpath('playlist.html'))