-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
104 lines (93 loc) · 2.92 KB
/
storage.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
#!/usr/bin/env python
# encoding: UTF-8
"""
Class to stores everything into a json file.
"""
import json
from .const import Constant
from .singleton import Singleton
from .utils import utf8_data_to_file
class Storage(Singleton):
def __init__(self):
"""
Database stores every info.
version int
# if value in file is unequal to value defined in this class.
# An database update will be applied.
user dict:
username str
key str
collections list:
collection_info(dict):
collection_name str
collection_type str
collection_describe str
collection_songs list:
song_id(int)
songs dict:
song_id(int) dict:
song_id int
artist str
song_name str
mp3_url str
album_name str
album_id str
quality str
lyric str
tlyric str
player_info dict:
player_list list[dict]
playing_order list[int]
playing_mode int
playing_offset int
:return:
"""
if hasattr(self, "_init"):
return
self._init = True
self.database = {
"user": {"username": "", "password": "", "user_id": "", "nickname": ""},
"collections": [],
"songs": {},
"player_info": {
"player_list": [],
"player_list_type": "",
"player_list_title": "",
"playing_order": [],
"playing_mode": 0,
"idx": 0,
"ridx": 0,
"playing_volume": 60,
},
}
self.storage_path = Constant.storage_path
self.cookie_path = Constant.cookie_path
def login(self, username, password, userid, nickname):
self.database["user"] = dict(
username=username,
password=password,
user_id=userid,
nickname=nickname,
)
def logout(self):
self.database["user"] = {
"username": "",
"password": "",
"user_id": "",
"nickname": "",
}
def load(self):
try:
with open(self.storage_path, "r") as f:
for k, v in json.load(f).items():
if isinstance(self.database[k], dict):
self.database[k].update(v)
else:
self.database[k] = v
except (OSError, KeyError, ValueError):
pass
self.save()
def save(self):
with open(self.storage_path, "w") as f:
data = json.dumps(self.database)
utf8_data_to_file(f, data)