-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.py
103 lines (78 loc) · 2.83 KB
/
cron.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
import requests
import json
from datetime import datetime
import os
import get
import settings
headers = {"x-api-key": settings.OTLPLUS_API_KEY}
os.makedirs("logs", exist_ok=True)
result = requests.get(
f"{settings.OTLPLUS_BASE_URL}/defaultSemester", headers=headers
).json()
semesters = [(result["year"], result["semester"])]
def put_previous_semester(semesters, count):
if count == 0:
return
year, semester = semesters[-1]
if semester == 1:
year -= 1
semester = 3
elif semester == 3:
semester = 1
else:
raise ValueError("Invalid semester: " + semesters[0])
semesters.append((year, semester))
put_previous_semester(semesters, count - 1)
put_previous_semester(semesters, 1)
def save_log(filetype, data):
date_str = datetime.now().strftime("%Y-%m-%d")
num = 0
while True:
filename = f"logs/{date_str}_{filetype}_{num}.log"
if not os.path.exists(filename):
break
num += 1
# Write data to file
with open(filename, "w", encoding='utf8') as file:
json.dump(data, file, indent=4, ensure_ascii=False)
for year, semester in semesters:
lectures = get.get_lecture_type(lecture_year=year, lecture_term=semester)
charges = get.get_charge_type(lecture_year=year, lecture_term=semester)
result = requests.post(
f"{settings.OTLPLUS_BASE_URL}/scholarDB",
json={
"year": year,
"semester": semester,
"lectures": lectures,
"charges": charges,
},
headers=headers,
).json()
save_log(f"{year}-{semester}_scholarDB", result)
classtimes = get.get_time_type(lecture_year=year, lecture_term=semester)
for classtime in classtimes:
teaching = classtime["room_k_name"]
classtime["room_k_name"] = classtime["room_e_name"]
classtime["room_e_name"] = classtime["teaching"]
classtime["teaching"] = teaching
result = requests.post(
f"{settings.OTLPLUS_BASE_URL}/classtime",
json={"year": year, "semester": semester, "classtimes": classtimes},
headers=headers,
).json()
save_log(f"{year}-{semester}_classtime", result)
examtimes = get.get_exam_time_type(lecture_year=year, lecture_term=semester)
result = requests.post(
f"{settings.OTLPLUS_BASE_URL}/examtime",
json={"year": year, "semester": semester, "examtimes": examtimes},
headers=headers,
).json()
save_log(f"{year}-{semester}_examtime", result)
for year, semester in semesters:
attend = get.get_attend_type(lecture_year=year, lecture_term=semester)
result = requests.post(
f"{settings.OTLPLUS_BASE_URL}/takenLecture",
json={"year": year, "semester": semester, "attend": attend},
headers=headers,
).json()
save_log(f"{year}-{semester}_takenLecture", result)