-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreferences.py
71 lines (58 loc) · 2.02 KB
/
preferences.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
import os
import json
prefs_file = os.path.join(os.path.dirname(__file__), "resources", "preferences.json")
group_file = os.path.join(os.path.dirname(__file__), "resources", "groups.json")
shortcuts_file = os.path.join(os.path.dirname(__file__), "resources", "shortcuts.json")
# TODO: Synced-style class for preferences/groups
def load_prefs():
if not os.path.isfile(group_file):
with open(group_file, 'w+') as gf: json.dump({}, gf)
if not os.path.isfile(prefs_file):
with open(prefs_file, 'w+') as pf: json.dump({
"PLAYLISTS": {
"DEFAULT": "",
"PRIMARY": "",
"BACKLOG": "",
"SHARED": ""
},
"ALIASES": {},
"LASTFM_USER": "",
"LASTFM_WATCH_USER": "Nathansbud"
}, pf)
if not os.path.isfile(shortcuts_file):
with open(shortcuts_file, 'w+') as sf:
json.dump(
{"albums": {}, "tracks": {}},
sf
)
with \
open(group_file, 'r') as gf, \
open(prefs_file, 'r') as pf, \
open(shortcuts_file, 'r') as sf:
try:
gs = json.load(gf)
except Exception:
gs = {}
try:
ps = json.load(pf)
except Exception:
ps = {}
try:
ss = json.load(sf)
except Exception:
ss = {}
return gs, ps, ss
def dump_groups():
with open(group_file, 'w+') as gf:
json.dump(groups, gf)
def dump_shortcuts():
with open(shortcuts_file, 'w+') as sf:
json.dump(shortcuts, sf)
def playlist_preference(identifier):
if identifier.startswith('http'):
return identifier.split("/")[-1].split("?")[0]
elif identifier.startswith("spotify:playlist:"):
return identifier.split(":")[-1]
else:
return prefs.get("PLAYLISTS", {}).get(identifier.upper())
groups, prefs, shortcuts = load_prefs()