-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.py
79 lines (67 loc) · 2.01 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
import os
import time
import random
import requests
import yaml
from Youth import Youth
def main(remote_config=""):
youth = Youth()
course = youth.course
print("[INFO] Read config from config.yaml")
if remote_config:
print("[INFO] Read remote config")
try:
r = requests.get(remote_config, timeout=5)
r.status_code
print("[INFO] Get remote config succeed")
try:
config_dict = yaml.safe_load(r.text)
except:
print("[ERROR] Please check your remote config")
return 0
except:
print("[ERROR] Get remote config failed")
return 0
else:
with open("config.yaml", "r") as f:
config_dict = yaml.safe_load(f)
user_i = 0
for single_config in config_dict["youth"]:
print("[INFO] User ", user_i, " Start")
user_i += 1
youth.read_config(single_config)
if not youth.get_cookie():
continue
if course.need_update:
if not course.update(youth.headers):
continue
course.need_update = False
if not youth.study():
continue
sleep_time = 2 + random.random()
print(f"[INFO] Sleep {sleep_time} s")
time.sleep(sleep_time)
return 1
def main_cli(args):
print("[INFO] Read config from command line parameters")
print("[INFO] Start")
youth = Youth()
youth.username = args["USERNAME"]
youth.password = args["PASSWORD"]
youth.org_id = args["ORG_ID"]
if not youth.get_cookie():
return 0
if not youth.course.update(youth.headers):
return 0
if not youth.study():
return 0
if __name__ == "__main__":
ENV = {
_i: os.getenv(_i) for _i in ["PASSWORD", "USERNAME", "ORG_ID", "REMOTE_CONFIG"]
}
if ENV["REMOTE_CONFIG"]:
main(ENV["REMOTE_CONFIG"])
elif ENV["USERNAME"] and ENV["PASSWORD"] and ENV["ORG_ID"]:
main_cli(ENV)
else:
main()