-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylister.py
103 lines (82 loc) · 3.23 KB
/
playlister.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
import json
import os
from datetime import datetime
import requests
from network import client as spotify
from utilities import flatten
CACHE_FILE = os.path.join(os.path.dirname(__file__), "resources", "playlister.json")
UPDATE_BACKLOG = False
def internet():
connection = True
try:
r = requests.get("https://google.com")
r.raise_for_status()
except Exception:
connection = False
finally:
return connection
def load_cache():
if not os.path.isfile(CACHE_FILE):
with open(CACHE_FILE, 'w+') as cf:
json.dump({
"LIBRARY_METADATA": {
"LAST_UPDATE": datetime.min.isoformat(),
"ACTIVE_PLAYLIST": ""
},
"LIKED_METADATA": {
"LAST_UPDATE": datetime.min.isoformat(),
"ACTIVE_PLAYLIST": ""
},
}, cf, indent=2)
with open(CACHE_FILE, "r+") as cf:
return json.load(cf)
cache = load_cache()
def save_cache():
with open(CACHE_FILE, "w+") as cf:
json.dump(cache, cf, indent=2)
def update_library_playlist(playlist_id, last_update=None, limit=50):
time_now = datetime.utcnow().isoformat()
# in the interest of performance, only do last 50 played albums when updating;
# realistically, we won't have gotten backlogged past that point
albums = spotify.get_library_albums(last_update, limit=limit)
tracks = flatten([album.tracks for album in albums])
spotify.add_playlist_tracks(playlist_id, tracks)
cache["LIBRARY_METADATA"]["LAST_UPDATE"] = time_now
save_cache()
def update_liked_playlist(playlist_id, last_update=None):
time_now = datetime.utcnow().isoformat()
spotify.add_playlist_tracks(
playlist_id,
spotify.get_library_tracks(last_update)
)
cache["LIKED_METADATA"]["LAST_UPDATE"] = time_now
save_cache()
def update_backlog_playlist(playlist_id, backlog_id, last_update=None):
time_now = datetime.utcnow().isoformat()
backlog = spotify.get_playlist_tracks(last_update, playlist_id=backlog_id)
tracks = flatten([
spotify.get_album_tracks(album_id=track.album.id)
for track in backlog
])
spotify.add_playlist_tracks(playlist_id, tracks)
cache["BACKLOG_METADATA"]["LAST_UPDATE"] = time_now
save_cache()
if __name__ == "__main__":
if internet():
# Would prefer to disable liked playlist -> Zacksongs, since workflow doesn't really make sense since
# considering the removal of the like button from UI, but programmatically can add new tracks to top of
# playlist which is much preferred
update_liked_playlist(
cache["LIKED_METADATA"]["ACTIVE_PLAYLIST_ID"],
cache["LIKED_METADATA"]["LAST_UPDATE"],
)
update_library_playlist(
cache["LIBRARY_METADATA"]["ACTIVE_PLAYLIST_ID"],
cache["LIBRARY_METADATA"]["LAST_UPDATE"],
)
if UPDATE_BACKLOG:
update_backlog_playlist(
cache["BACKLOG_METADATA"]["ACTIVE_PLAYLIST_ID"],
cache["BACKLOG_METADATA"]["BACKLOG_PLAYLIST_ID"],
cache["BACKLOG_METADATA"]["LAST_UPDATE"],
)