-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.py
62 lines (56 loc) · 2.16 KB
/
add.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
import json
from pathlib import Path
from pytz import timezone
from sys import argv
from datetime import datetime
from typing import List
from models.code import CodeList, Code, Reward
data_path = Path("data")
custom_path = data_path / "custom.json"
def add(code: str, expire_str: str, rewards_str: List[str]) -> Code:
if ":" in expire_str:
expire = datetime.strptime(expire_str, "%Y-%m-%d:%H:%M:%S")
expire = expire.replace(microsecond=999999)
else:
expire = datetime.strptime(expire_str, "%Y-%m-%d")
expire = expire.replace(hour=23, minute=59, second=59, microsecond=999999)
expire = timezone("Asia/Shanghai").localize(expire)
rewards = []
for reward_str in rewards_str:
reward_list = reward_str.split(":")
rewards.append(
Reward(
name=reward_list[0],
cnt=int(reward_list[1])
)
)
return Code(
code=code,
expire=int(expire.timestamp() * 1000),
reward=rewards,
)
if __name__ == '__main__':
try:
add_type = argv[1]
if add_type not in ["main", "over"]:
raise IndexError
code = add(argv[2], argv[3], argv[4:])
with open(custom_path, "r", encoding="utf-8") as f:
custom: CodeList = CodeList.model_validate_json(f.read())
if add_type == "main":
main_codes = [i.code for i in custom.main]
if code.code in main_codes:
raise ValueError("Duplicate code")
custom.main.append(code)
else:
over_codes = [i.code for i in custom.over]
if code.code in over_codes:
raise ValueError("Duplicate code")
custom.over.append(code)
custom.main.sort(key=lambda x: x.expire, reverse=True)
custom.over.sort(key=lambda x: x.expire, reverse=True)
with open(custom_path, "w", encoding="utf-8") as f:
f.write(json.dumps(custom.model_dump(), indent=4, ensure_ascii=False))
except IndexError:
print("Usage: python add.py [main/over] [code] [expire] [rewards...]")
print("Example: python add.py main code 2023-11-1 星琼:1 信用点:1000")