-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backup.py
67 lines (53 loc) · 2.03 KB
/
Backup.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
from pprint import pformat, pprint
from util import gist, playlist
from util.spotify import get_spotify_client
msg = ""
def print_exceptions(exepts):
print(f"Exceptions ({len(exepts)}):\n" + pformat(exepts))
def backup_playlist(pl: dict):
Get = [playlist.getAsync(_spotify, x, publicOnly=True)["items"] for x in pl["get"]][0]
Set = playlist.getAsync(_spotify, pl["set"], publicOnly=True)["items"]
print(f" Exporting: {pl['set']}")
try:
Get = [track for track in Get if track['track']]
Set = [track for track in Set if track['track']]
except Exception as e:
print(e)
exceptions.append(e)
Tracks = playlist.deduplify_list(main_list=Get, base_list=Set, ignore=ignore)
if len(Tracks) > 0:
ToAdd = [z['track']['uri'] for z in Tracks]
try:
playlist.addAsync(_spotify, ToAdd, pl['set'])
print(f"Added: {len(ToAdd)}")
pprint(ToAdd, depth=2)
msg = "get:\n " + "\n ".join([x for x in pl["get"]])
msg += "\nset\n " + pl["set"]
print(f"Backed:\n{msg}")
except Exception as e:
print(e)
exceptions.append(e)
else:
print(f"Already up to date: {pl['set']}", end="\n")
def main():
print("loading...")
for pl in data["backup"]:
if data["backup"][pl]["set"].strip() == "":
print(f"Empty set: {pl}")
continue
msg = pl + "\nget:\n " + "\n ".join([x for x in data["backup"][pl]["get"]])
msg += "\nset:\n " + data["backup"][pl]["set"]
print(f"Backing up: {msg}")
backup_playlist(data["backup"][pl])
print_exceptions(exceptions)
print("Done")
if __name__ == "__main__":
_spotify = get_spotify_client()
print("Loading autofy.json...")
data = gist.load("autofy.json")
print("Loading autofy.json completed.")
print("Loading Loading ignore list...")
ignore = playlist.getAsync(_spotify, data["ignore"])["items"]
print("Loading ignore list completed.")
exceptions = []
main()