forked from mackorone/spotify-playlist-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
556 lines (491 loc) · 17.8 KB
/
script.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#!/usr/bin/env python3
import argparse
import base64
import collections
import datetime
import json
import os
import re
import requests
import subprocess
Playlist = collections.namedtuple(
"Playlist",
[
"url",
"name",
"description",
"tracks",
]
)
Track = collections.namedtuple(
"Track",
[
"id",
"url",
"duration_ms",
"name",
"album",
"artists",
]
)
Album = collections.namedtuple("Album", ["url", "name"])
Artist = collections.namedtuple("Artist", ["url", "name"])
class InvalidAccessTokenError(Exception):
pass
class InvalidPlaylistError(Exception):
pass
class PrivatePlaylistError(Exception):
pass
class Spotify:
BASE_URL = "https://api.spotify.com/v1/playlists/"
def __init__(self, client_id, client_secret):
self._token = self._get_access_token(client_id, client_secret)
@classmethod
def _get_access_token(cls, client_id, client_secret):
joined = "{}:{}".format(client_id, client_secret)
encoded = base64.b64encode(joined.encode()).decode()
response = requests.post(
"https://accounts.spotify.com/api/token",
data={"grant_type": "client_credentials"},
headers={"Authorization": "Basic {}".format(encoded)},
).json()
error = response.get("error")
if error:
raise Exception("Failed to get access token: {}".format(error))
access_token = response.get("access_token")
if not access_token:
raise Exception("Invalid access token: {}".format(access_token))
token_type = response.get("token_type")
if token_type != "Bearer":
raise Exception("Invalid token type: {}".format(token_type))
return access_token
def get_playlist(self, playlist_id):
playlist_href = self._get_playlist_href(playlist_id)
response = self._make_request(playlist_href)
error = response.get("error")
if error:
if error.get("status") == 401:
raise InvalidAccessTokenError
elif error.get("status") == 403:
raise PrivatePlaylistError
elif error.get("status") == 404:
raise InvalidPlaylistError
else:
raise Exception("Failed to get playlist: {}".format(error))
url = self._get_url(response["external_urls"])
name = response["name"]
description = response["description"]
tracks = self._get_tracks(playlist_id)
return Playlist(url=url,name=name, description=description, tracks=tracks)
def _get_tracks(self, playlist_id):
tracks = []
tracks_href = self._get_tracks_href(playlist_id)
while tracks_href:
response = self._make_request(tracks_href)
error = response.get("error")
if error:
raise Exception("Failed to get tracks: {}".format(error))
for item in response["items"]:
track = item["track"]
if not track:
continue
id_ = track["id"]
url = self._get_url(track["external_urls"])
duration_ms = track["duration_ms"]
name = track["name"]
album = Album(
url=self._get_url(track["album"]["external_urls"]),
name=track["album"]["name"],
)
artists = []
for artist in track["artists"]:
artists.append(Artist(
url=self._get_url(artist["external_urls"]),
name=artist["name"],
))
tracks.append(Track(
id=id_,
url=url,
duration_ms=duration_ms,
name=name,
album=album,
artists=artists,
))
tracks_href = response["next"]
return tracks
@classmethod
def _get_url(cls, external_urls):
return (external_urls or {}).get("spotify")
@classmethod
def _get_playlist_href(cls, playlist_id):
rest = "{}?fields=external_urls,name,description"
template = cls.BASE_URL + rest
return template.format(playlist_id)
@classmethod
def _get_tracks_href(cls, playlist_id):
rest = (
"{}/tracks?fields=next,items.track(id,external_urls,"
"duration_ms,name,album(external_urls,name),artists)"
)
template = cls.BASE_URL + rest
return template.format(playlist_id)
def _make_request(self, href):
return requests.get(
href,
headers={"Authorization": "Bearer {}".format(self._token)},
).json()
class Formatter:
TRACK_NO = "No."
TITLE = "Title"
ARTISTS = "Artist(s)"
ALBUM = "Album"
LENGTH = "Length"
ADDED = "Added"
REMOVED = "Removed"
ARTIST_SEPARATOR = ", "
LINK_REGEX = r"\[(.+?)\]\(.+?\)"
@classmethod
def plain(cls, playlist_id, playlist):
lines = [cls._plain_line_from_track(track) for track in playlist.tracks]
# Sort alphabetically to minimize changes when tracks are reordered
sorted_lines = sorted(lines, key=lambda line: line.lower())
header = [playlist.name, playlist.description, ""]
return "\n".join(header + sorted_lines)
@classmethod
def pretty(cls, playlist_id, playlist):
columns = [
cls.TRACK_NO,
cls.TITLE,
cls.ARTISTS,
cls.ALBUM,
cls.LENGTH,
]
vertical_separators = ["|"] * (len(columns) + 1)
line_template = " {} ".join(vertical_separators)
divider_line = "---".join(vertical_separators)
lines = cls._markdown_header_lines(
playlist_name=playlist.name,
playlist_url=playlist.url,
playlist_id=playlist_id,
playlist_description=playlist.description,
is_cumulative=False,
)
lines += [
line_template.format(*columns),
divider_line,
]
for i, track in enumerate(playlist.tracks):
lines.append(line_template.format(
i + 1,
cls._link(track.name, track.url),
cls.ARTIST_SEPARATOR.join([
cls._link(artist.name, artist.url)
for artist in track.artists
]),
cls._link(track.album.name, track.album.url),
cls._format_duration(track.duration_ms),
))
return "\n".join(lines)
@classmethod
def cumulative(cls, now, prev_content, playlist_id, playlist):
today = now.strftime("%Y-%m-%d")
columns = [
cls.TITLE,
cls.ARTISTS,
cls.ALBUM,
cls.LENGTH,
cls.ADDED,
cls.REMOVED,
]
vertical_separators = ["|"] * (len(columns) + 1)
line_template = " {} ".join(vertical_separators)
divider_line = "---".join(vertical_separators)
header = cls._markdown_header_lines(
playlist_name=playlist.name,
playlist_url=playlist.url,
playlist_id=playlist_id,
playlist_description=playlist.description,
is_cumulative=True,
)
header += [
line_template.format(*columns),
divider_line,
]
# Retrieve existing rows, then add new rows
rows = cls._rows_from_prev_content(today, prev_content, divider_line)
for track in playlist.tracks:
# Get the row for the given track
key = cls._plain_line_from_track(track).lower()
row = rows.get(key, {column: None for column in columns})
rows[key] = row
# Update row values
row[cls.TITLE] = cls._link(track.name, track.url)
row[cls.ARTISTS] = cls.ARTIST_SEPARATOR.join([
cls._link(artist.name, artist.url)
for artist in track.artists
])
row[cls.ALBUM] = cls._link(track.album.name, track.album.url)
row[cls.LENGTH] = cls._format_duration(track.duration_ms)
if not row[cls.ADDED]:
row[cls.ADDED] = today
row[cls.REMOVED] = ""
lines = []
for key, row in sorted(rows.items()):
lines.append(line_template.format(
*[row[column] for column in columns]
))
return "\n".join(header + lines)
@classmethod
def _markdown_header_lines(
cls,
playlist_name,
playlist_url,
playlist_id,
playlist_description,
is_cumulative,
):
if is_cumulative:
pretty = cls._link("pretty", URL.pretty(playlist_name))
cumulative = "cumulative"
else:
pretty = "pretty"
cumulative = cls._link("cumulative", URL.cumulative(playlist_name))
return [
"{} - {} - {} ({})".format(
pretty,
cumulative,
cls._link("plain", URL.plain(playlist_id)),
cls._link("githistory", URL.plain_history(playlist_id)),
),
"",
"### {}".format(cls._link(playlist_name, playlist_url)),
"",
"> {}".format(playlist_description),
"",
]
@classmethod
def _rows_from_prev_content(cls, today, prev_content, divider_line):
rows = {}
if not prev_content:
return rows
prev_lines = prev_content.splitlines()
try:
index = prev_lines.index(divider_line)
except ValueError:
return rows
for i in range(index + 1, len(prev_lines)):
prev_line = prev_lines[i]
try:
title, artists, album, length, added, removed = (
# Slice [2:-2] to trim off "| " and " |"
prev_line[2:-2].split(" | ")
)
except Exception:
continue
key = cls._plain_line_from_names(
track_name=cls._unlink(title),
artist_names=[
artist for
artist in re.findall(cls.LINK_REGEX, artists)
],
album_name=cls._unlink(album),
).lower()
row = {
cls.TITLE: title,
cls.ARTISTS: artists,
cls.ALBUM: album,
cls.LENGTH: length,
cls.ADDED: added,
cls.REMOVED: removed
}
rows[key] = row
if not row[cls.REMOVED]:
row[cls.REMOVED] = today
return rows
@classmethod
def _plain_line_from_track(cls, track):
return cls._plain_line_from_names(
track_name=track.name,
artist_names=[artist.name for artist in track.artists],
album_name=track.album.name,
)
@classmethod
def _plain_line_from_names(cls, track_name, artist_names, album_name):
return "{} -- {} -- {}".format(
track_name,
cls.ARTIST_SEPARATOR.join(artist_names),
album_name,
)
@classmethod
def _link(cls, text, url):
if not url:
return text
return "[{}]({})".format(text, url)
@classmethod
def _unlink(cls, link):
return re.match(cls.LINK_REGEX, link).group(1)
@classmethod
def _format_duration(cls, duration_ms):
seconds = int(duration_ms // 1000)
timedelta = str(datetime.timedelta(seconds=seconds))
index = 0
while timedelta[index] in [":", "0"]:
index += 1
return timedelta[index:]
class URL:
BASE = (
"https://github.com/mackorone/spotify-playlist-archive/"
"blob/master/playlists/"
)
@classmethod
def plain_history(cls, playlist_id):
plain = cls.plain(playlist_id)
return plain.replace("github.com", "github.githistory.xyz")
@classmethod
def plain(cls, playlist_id):
return cls.BASE + "plain/{}".format(playlist_id)
@classmethod
def pretty(cls, playlist_name):
sanitized = playlist_name.replace(" ", "%20")
return cls.BASE + "pretty/{}.md".format(sanitized)
@classmethod
def cumulative(cls, playlist_name):
sanitized = playlist_name.replace(" ", "%20")
return cls.BASE + "cumulative/{}.md".format(sanitized)
def update_files(now):
spotify = Spotify(
client_id=os.getenv("SPOTIFY_CLIENT_ID"),
client_secret=os.getenv("SPOTIFY_CLIENT_SECRET"),
)
plain_dir = "playlists/plain"
pretty_dir = "playlists/pretty"
cumulative_dir = "playlists/cumulative"
# Determine which playlists to scrape from the files in playlists/plain.
# This makes it easy to add new a playlist: just touch an empty file like
# playlists/plain/<playlist_id> and this script will handle the rest.
playlist_ids = os.listdir(plain_dir)
readme_lines = []
for playlist_id in playlist_ids:
plain_path = "{}/{}".format(plain_dir, playlist_id)
try:
playlist = spotify.get_playlist(playlist_id)
except PrivatePlaylistError:
print("Removing private playlist: {}".format(playlist_id))
os.remove(plain_path)
except InvalidPlaylistError:
print("Removing invalid playlist: {}".format(playlist_id))
os.remove(plain_path)
else:
readme_lines.append("- [{}]({})".format(
playlist.name,
URL.pretty(playlist.name),
))
pretty_path = "{}/{}.md".format(pretty_dir, playlist.name)
cumulative_path = "{}/{}.md".format(cumulative_dir, playlist.name)
for path, func, flag in [
(plain_path, Formatter.plain, False),
(pretty_path, Formatter.pretty, False),
(cumulative_path, Formatter.cumulative, True),
]:
try:
prev_content = "".join(open(path).readlines())
except Exception:
prev_content = None
if flag:
args = [now, prev_content, playlist_id, playlist]
else:
args = [playlist_id, playlist]
content = func(*args)
if content == prev_content:
print("No changes to file: {}".format(path))
else:
print("Writing updates to file: {}".format(path))
with open(path, "w") as f:
f.write(content)
# Sanity check: ensure same number of files in playlists/plain and
# playlists/pretty - if not, some playlists have the same name and
# overwrote each other in playlists/pretty
num_plain_playlists = len(os.listdir(plain_dir))
num_pretty_playlists = len(os.listdir(pretty_dir))
if num_plain_playlists != num_pretty_playlists:
raise Exception("Unequal number of playlists: {} vs {}".format(
num_plain_playlists,
num_pretty_playlists,
))
# Lastly, update README.md
readme = open("README.md").read().splitlines()
index = readme.index("## Playlists")
lines = readme[:index + 1] + [""] + sorted(readme_lines)
with open("README.md", "w") as f:
f.write("\n".join(lines) + "\n")
def run(args):
print("- Running: {}".format(args))
result = subprocess.run(
args=args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
print("- Exited with: {}".format(result.returncode))
return result
def push_updates(now):
diff = run(["git", "status", "-s"])
has_changes = bool(diff.stdout)
if not has_changes:
print("No changes, not pushing")
return
print("Configuring git")
config = ["git", "config", "--global"]
config_name = run(config + ["user.name", "Mack Ward (Bot Account)"])
config_email = run(config + ["user.email", "[email protected]"])
if config_name.returncode != 0:
raise Exception("Failed to configure name")
if config_email.returncode != 0:
raise Exception("Failed to configure email")
print("Staging changes")
add = run(["git", "add", "-A"])
if add.returncode != 0:
raise Exception("Failed to stage changes")
print("Committing changes")
build = os.getenv("TRAVIS_BUILD_NUMBER")
now_str = now.strftime("%Y-%m-%d %H:%M:%S")
message = "[skip ci] Build #{} ({})".format(build, now_str)
commit = run(["git", "commit", "-m", message])
if commit.returncode != 0:
raise Exception("Failed to commit changes")
print("Rebasing onto master")
rebase = run(["git", "rebase", "HEAD", "master"])
if commit.returncode != 0:
raise Exception("Failed to rebase onto master")
print("Removing origin")
remote_rm = run(["git", "remote", "rm", "origin"])
if remote_rm.returncode != 0:
raise Excetion("Failed to remove origin")
print("Adding new origin")
# It's ok to print the token, Travis will hide it
token = os.getenv("GITHUB_ACCESS_TOKEN")
url = (
"https://mackorone-bot:{}@github.com/mackorone/"
"spotify-playlist-archive.git".format(token)
)
remote_add = run(["git", "remote", "add", "origin", url])
if remote_add.returncode != 0:
raise Exception("Failed to add new origin")
print("Pushing changes")
push = run(["git", "push", "origin", "master"])
if push.returncode != 0:
raise Exception("Failed to push changes")
def main():
parser = argparse.ArgumentParser("Snapshot Spotify playlists")
parser.add_argument(
"--push",
help="Commit and push updated playlists",
action="store_true",
)
args = parser.parse_args()
now = datetime.datetime.now()
update_files(now)
if args.push:
push_updates(now)
print("Done")
if __name__ == "__main__":
main()