-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·434 lines (381 loc) · 17.9 KB
/
main.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/python3
from http.server import BaseHTTPRequestHandler, HTTPServer
import webbrowser
import urllib.parse as urlparse
from urllib.parse import urlencode
import requests
from base64 import urlsafe_b64encode
import secrets
from hashlib import sha256
from diff import diff
from threading import Thread
import os
from os import path, getcwd
from time import time
import sys
import argparse
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from getpass import getuser
import traceback
import json
from json.decoder import JSONDecodeError
from pygments import highlight
from pygments.lexers import JsonLexer
from pygments.formatters import TerminalFormatter
epilog = \
'''<https://www.github.com/martinmake/spotify-playlist-manager>'''
client_id = 'ccf29f57a6f049a08d83403ff98ce91b'
scope = 'playlist-modify-public playlist-modify-private'
state = '0123456789ABCDE' # has to be randomly generated
maximum_track_count_in_block = 100
tokens_filename = 'tokens.json'
code: str
cache_dir: str
tokens_filepath: str
server_port: int
redirect_uri: str
tracks_filepath_default='./tracks.tsv'
value_separator_default='\t'
value_separator=value_separator_default
project_name = 'spotify-playlist-manager'
closer_webpage = \
"""
<!DOCTYPE html>
<html>
<head>
<title>You may close this window.</title>
</head>
<body>
<h1>You may close this window.</h1>
<script>close()</script>
</body>
</html>
"""
class HTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
url = urlparse.urlparse(self.path)
qs = urlparse.parse_qs(url.query)
if 'code' in qs:
global code
code = qs['code'][0]
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes(closer_webpage, "utf-8"))
def log_message(self, format, *args):
return
def dump_as_json(str):
json_obj = json.loads(str)
json_str = json.dumps(json_obj, indent=4, sort_keys=True)
print(highlight(json_str, JsonLexer(), TerminalFormatter()))
def dump_json(json_obj):
json_str = json.dumps(json_obj, indent=4, sort_keys=True)
print(highlight(json_str, JsonLexer(), TerminalFormatter()))
def serve_closer():
server = HTTPServer(('localhost', server_port), HTTPRequestHandler)
try:
server.handle_request()
except KeyboardInterrupt:
server.server_close()
def request_code(code_verifier ):
code_challenge = urlsafe_b64encode(sha256(code_verifier.encode('ascii')).digest()).decode('ascii').replace('=','')
t = Thread(target=serve_closer)
t.start()
webbrowser.open( 'https://accounts.spotify.com/authorize' + '?'
+ urlencode({ 'response_type' : 'code'
, 'client_id' : client_id
, 'scope' : scope
, 'redirect_uri' : redirect_uri
, 'state' : state
, 'code_challenge' : code_challenge
, 'code_challenge_method' : 'S256' })
, new=2 )
t.join()
return code
def request_tokens():
code_verifier = secrets.token_urlsafe(43 + secrets.randbits(7) % 86)
code = request_code(code_verifier )
response = requests.post( 'https://accounts.spotify.com/api/token'
, headers={ 'content-type' : 'application/x-www-form-urlencoded' }
, data=urlencode({ 'client_id' : client_id
, 'grant_type' : 'authorization_code'
, 'code' : code
, 'redirect_uri' : redirect_uri
, 'code_verifier' : code_verifier
}).encode('ascii') )
return response.json()
def refresh_tokens(refresh_token):
response = requests.post( 'https://accounts.spotify.com/api/token'
, headers={ 'content-type' : 'application/x-www-form-urlencoded' }
, data=urlencode({ 'grant_type' : 'refresh_token'
, 'refresh_token' : refresh_token
, 'client_id' : client_id
}).encode('ascii') )
return response.json()
def get_access_token():
# check cache for valid access token
try:
with open(tokens_filepath, 'r') as tokens_file:
try:
tokens = json.load(tokens_file)
if tokens is None: raise TypeError
except ( JSONDecodeError
, TypeError ):
os.remove(tokens_filepath)
return get_access_token()
try:
if time() - os.path.getmtime(tokens_filepath) > tokens['expires_in']:
tokens = refresh_tokens(tokens['refresh_token'])
with open(tokens_filepath, 'w') as tokens_file:
json.dump(tokens, tokens_file)
return tokens['access_token']
except KeyError:
os.remove(tokens_filepath)
return get_access_token()
except FileNotFoundError:
try:
with open(tokens_filepath, 'w') as tokens_file:
tokens = request_tokens()
json.dump(tokens, tokens_file)
return tokens['access_token']
except FileNotFoundError:
os.makedirs(cache_dir)
return get_access_token()
except:
traceback.print_exc()
def push(args):
playlist_name = args.playlist_name
access_token = get_access_token()
if not 'playlist_id' in args:
# GET https://api.spotify.com/v1/me
response = requests.get( 'https://api.spotify.com/v1/me'
, headers={ 'Authorization': f"Bearer {access_token}" } )
user_id = response.json()['id']
# GET https://api.spotify.com/v1/users/{user_id}/playlists
response = requests.get( f"https://api.spotify.com/v1/users/{user_id}/playlists"
, headers={ 'Authorization': f"Bearer {access_token}" } )
playlists = response.json()['items']
playlist_id = None
for playlist in playlists:
if playlist['name'] == playlist_name:
playlist_id = playlist['id']
if not playlist_id:
if args.create_playlist_if_not_found:
response = requests.post( f"https://api.spotify.com/v1/users/{user_id}/playlists"
, headers={ 'Authorization': f"Bearer {access_token}"
, 'content-type' : 'application/json' }
, json={'name' : playlist_name} )
playlist_id = response.json()['id']
else:
print(f"ERROR: Playlist '{playlist_name}' not found!")
exit(2)
# GET https://api.spotify.com/v1/playlists/{playlist_id}
response = requests.get( f"https://api.spotify.com/v1/playlists/{playlist_id}"
, headers={ 'Authorization': f"Bearer {access_token}" }
, params={ 'fields' : 'tracks.total' } )
track_count = response.json()['tracks']['total']
old_track_ids = []
remaining_track_count = track_count
while remaining_track_count > 0:
track_count_in_current_block = 0
if remaining_track_count > maximum_track_count_in_block:
track_count_in_current_block = maximum_track_count_in_block
else:
track_count_in_current_block = remaining_track_count
# GET https://api.spotify.com/v1/playlists/{playlist_id}/tracks
response = requests.get( f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks'
, headers={ 'Authorization': f"Bearer {access_token}" }
, params={ 'fields' : 'items.track(id)'
, 'offset' : track_count - remaining_track_count
, 'limit' : track_count_in_current_block } )
for track in (item['track'] for item in response.json()['items']):
old_track_ids.append(track['id'])
remaining_track_count -= track_count_in_current_block
track_ids = []
tracks_filepath = args.tracks_filepath
value_separator = args.value_separator
with open(tracks_filepath, 'r') as tracks_file:
for track_line in tracks_file.readlines():
track_id = track_line.split(value_separator)[0].strip()
if len(track_id) != 22: continue
track_ids.append(track_id)
playlist_diff = diff('\n'.join(old_track_ids), '\n'.join(track_ids))
if not playlist_diff:
exit()
playlist_diff = playlist_diff.explain().split('\n')
# DELETE pass
tracks = []
position = 0
for track_diff in playlist_diff:
action = track_diff[0]
track_id = track_diff[2:]
if action == '-':
uri = f"spotify:track:{track_id}"
try:
track_index = [track['uri'] for track in tracks].index(uri)
track_positions = tracks[track_index]['positions']
track_positions.append(position)
except ValueError:
tracks.append({ 'uri' : uri
, 'positions' : [position] })
position += 1
elif action == '+':
continue
else:
position += 1
continue
if tracks:
# DELETE https://api.spotify.com/v1/playlists/{playlist_id}/tracks
response = requests.delete( f"https://api.spotify.com/v1/playlists/{playlist_id}/tracks"
, headers={ 'Authorization' : f"Bearer {access_token}"
, 'content-type' : 'application/json' }
, json={ 'tracks': tracks } )
# ADD pass
track_position = 0
track_diff_index = 0
while track_diff_index < len(playlist_diff):
operation = playlist_diff[track_diff_index][0]
if operation == '+':
old_track_diff_index = track_diff_index
uris = []
while playlist_diff[track_diff_index][0] == '+':
track_id = playlist_diff[track_diff_index][2:]
uri = f"spotify:track:{track_id}"
uris.append(uri)
track_diff_index += 1
tracks_to_add = track_diff_index - old_track_diff_index
# POST https://api.spotify.com/v1/playlists/{playlist_id}/tracks
response = requests.post( f"https://api.spotify.com/v1/playlists/{playlist_id}/tracks"
, headers={ 'Authorization' : f"Bearer {access_token}"
, 'content-type' : 'application/json' }
, json={ 'uris' : uris
, 'position' : track_position } )
track_position += tracks_to_add
elif operation == '-':
track_diff_index += 1
else:
track_position += 1
track_diff_index += 1
def pull(args):
playlist_name = args.playlist_name
access_token = get_access_token()
if not 'playlist_id' in args:
# GET https://api.spotify.com/v1/me
response = requests.get( 'https://api.spotify.com/v1/me'
, headers={ 'Authorization': f"Bearer {access_token}" } )
user_id = response.json()['id']
# GET https://api.spotify.com/v1/users/{user_id}/playlists
response = requests.get( f"https://api.spotify.com/v1/users/{user_id}/playlists"
, headers={ 'Authorization': f"Bearer {access_token}" } )
playlists = response.json()['items']
playlist_id = None
for playlist in playlists:
if playlist['name'] == playlist_name:
playlist_id = playlist['id']
if not playlist_id:
print(f"ERROR: Playlist '{playlist_name}' not found!")
exit(2)
# GET https://api.spotify.com/v1/playlists/{playlist_id}
response = requests.get( f"https://api.spotify.com/v1/playlists/{playlist_id}"
, headers={ 'Authorization': f"Bearer {access_token}" }
, params={ 'fields' : 'tracks.total' } )
track_count = response.json()['tracks']['total']
remaining_track_count = track_count
print(f"id{value_separator}artists{value_separator}album{value_separator}name")
while remaining_track_count > 0:
track_count_in_current_block = 0
if remaining_track_count > maximum_track_count_in_block:
track_count_in_current_block = maximum_track_count_in_block
else:
track_count_in_current_block = remaining_track_count
# GET https://api.spotify.com/v1/playlists/{playlist_id}/tracks
response = requests.get( f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks'
, headers={ 'Authorization': f"Bearer {access_token}" }
, params={ 'fields' : 'items.track(id,name,artists(name),album(name))'
, 'offset' : track_count - remaining_track_count
, 'limit' : track_count_in_current_block } )
for track in (item['track'] for item in response.json()['items']):
print(track['id'], end=value_separator)
print(track['artists'][0]['name'], end='')
for artist in track['artists'][1:]:
print(', ' + artist['name'], end='')
print('', end=value_separator)
print(track['album']['name'], end=value_separator)
print(track['name'])
remaining_track_count -= track_count_in_current_block
def main(argv):
parser = ArgumentParser( prog=argv[0]
, description='Push/pull spotify playlist from text file to cloud and vice versa.'
, epilog=epilog
, formatter_class=ArgumentDefaultsHelpFormatter
, allow_abbrev=False )
subparsers = parser.add_subparsers( dest='command'
, required=True
, title='commands'
, description='Select from different commands.'
, help='Idkkkkk.' )
pull_parser = subparsers.add_parser( 'pull'
, help ='Pull spotify playlist from the cloud into a file.'
, description='Pull spotify playlist from the cloud into a file.'
, epilog=epilog
, formatter_class=ArgumentDefaultsHelpFormatter
, allow_abbrev=False )
pull_parser.set_defaults(command=pull)
pull_parser.add_argument( '-u'
, '--username'
, type=str
, default=getuser()
, help='username of current client' )
pull_parser.add_argument( '-p', '--playlist-name'
, type=str
, default=path.basename(getcwd())
, help='name of your spotify playlist' )
pull_parser.add_argument( '--port'
, type=int
, default=8080
, choices=[8080, 8888, 123456, 666]
, help='local port for authentication' )
push_parser = subparsers.add_parser( 'push'
, help ='Push spotify playlist from a file into the cloud.'
, description='Push spotify playlist from a file into the cloud.'
, epilog=epilog
, formatter_class=ArgumentDefaultsHelpFormatter
, allow_abbrev=False )
push_parser.set_defaults(command=push)
push_parser.add_argument( '-u', '--username'
, type=str
, default=getuser()
, help='Username of current client.' )
push_parser.add_argument( '-f', '--tracks-filepath'
, type=str
, default=tracks_filepath_default
, help='Path to tracks file.' )
push_parser.add_argument( '-s', '--value-separator'
, type=str
, default='|'
, help='Value separator used in the tracks file.' )
push_parser.add_argument( '-p', '--playlist-name'
, type=str
, default=path.basename(getcwd())
, help='Name of your spotify playlist.' )
push_parser.add_argument( '-c', '--create-playlist-if-not-found'
, action='store_true'
, help="If the playlist isn't be found, new one will be created." )
push_parser.add_argument( '--port'
, type=int
, default=8080
, choices=[8080, 8888, 123456, 666]
, help='Local port for authentication.' )
args = parser.parse_args()
# retarded
global server_port
server_port = args.port
global redirect_uri
redirect_uri = f"http://localhost:{server_port}"
global cache_dir
cache_dir = os.path.expanduser(f"~/.cache/{project_name}/{args.username}/")
global tokens_filepath
tokens_filepath = os.path.join(cache_dir, tokens_filename)
args.command(args)
if __name__ == "__main__":
main(sys.argv)