-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
88 lines (74 loc) · 2.97 KB
/
main.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
from cpu_equipment_reserve import my_reserve
import os
import sys
import yaml
def main():
REQUIRED_PARAMS = ["user", "equipment_id", "start", "end"]
config_file = "config/config.yaml"
if len(sys.argv) >= 2:
config_file = sys.argv[1]
if not os.path.exists(config_file):
raise FileNotFoundError(f"{config_file} not found.")
with open(config_file, "r", encoding="utf-8") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
if config is None:
config = {}
for param in REQUIRED_PARAMS:
if param not in config:
raise ValueError(f'Please specify "{param}" in the config file.')
user = config["user"]
if not os.path.exists(f"config/users/{user}.yaml"):
raise FileNotFoundError(f"config/users/{user}.yaml not found.")
with open(f"config/users/{user}.yaml", "r", encoding="utf-8") as f:
user_info: dict = yaml.load(f, Loader=yaml.FullLoader)
if not isinstance(user_info, dict):
raise ValueError(f"config/users/{user}.yaml should be a dict.")
credential = {
"username": user_info["username"],
"password": user_info["password"],
}
if "login_method" in user_info:
credential["login_method"] = user_info["login_method"]
equipment_id = config["equipment_id"]
reserve_info = {}
if os.path.exists(f"config/forms/{equipment_id}.yaml"):
with open(f"config/forms/{equipment_id}.yaml", "r", encoding="utf-8") as f:
reserve_info = yaml.load(f, Loader=yaml.FullLoader)
if reserve_info is None:
reserve_info = {}
dtstart = my_reserve.get_timestamp(config["start"])
dtend = my_reserve.get_timestamp(config["end"])
hackstart = my_reserve.get_timestamp(config.get("hackstart", None))
hackend = my_reserve.get_timestamp(config.get("hackend", None))
if (hackstart is None) ^ (hackend is None):
raise ValueError("Please specify both hackstart and hackend or neither.")
component_id = config.get("component_id", None)
hackuser_id = config.get("hackuser_id", None)
schedule = bool(config.get("schedule", False))
if schedule:
days_in_advance = int(reserve_info.get("days_in_advance", 0))
delay_seconds = float(config.get("delay_seconds", 0))
intervene = my_reserve.schedule(
dtend=dtend,
days_in_advance=days_in_advance,
delay_seconds=delay_seconds,
ticket_alive_seconds=my_reserve.TICKET_ALIVE_SECONDS,
)
else:
intervene = None
print("Start.\n")
res = my_reserve.single_reserve(
credential=credential,
dtstart=dtstart,
dtend=dtend,
reserve_info=reserve_info,
equipment_id=equipment_id,
component_id=component_id,
intervene=intervene,
hackstart=hackstart,
hackend=hackend,
hackuser_id=hackuser_id,
)
print(f"Success. Target component_id: \n{res['component_id']}")
if __name__ == "__main__":
main()