forked from stephen-zeng/WHU_Class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_calendar.py
53 lines (48 loc) · 1.62 KB
/
get_calendar.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
import requests
import json
from datetime import datetime, timedelta
from curl_convert import curl_convert
# 函数用于生成两个日期之间的所有日期
def get_date_range(start_date, end_date):
start = datetime.strptime(start_date, "%Y-%m-%d")
end = datetime.strptime(end_date, "%Y-%m-%d")
delta = timedelta(days=7)
current = start
while current <= end:
yield current.strftime("%Y-%m-%d")
current += delta
def get_json():
headers, cookies = curl_convert()
start_date = input("请输入开始日期 (格式: YYYY-MM-DD): ")
end_date = input("请输入结束日期 (格式: YYYY-MM-DD): ")
date_range = list(get_date_range(start_date, end_date))
first_date = date_range[0]
first_date_obj = datetime.strptime(first_date, "%Y-%m-%d")
year = first_date_obj.year
month = first_date_obj.month
day = first_date_obj.day
data_structure = {
"year": year,
"month": month,
"day": day,
"data": []
}
week = 0
for date in get_date_range(start_date, end_date):
week += 1
params = {
'date': date,
}
response = requests.get(
'https://zhlj.whu.edu.cn/mobile/homepageapi/getCurriculumData',
params=params,
cookies=cookies,
headers=headers,
)
week_data = {
"week": week,
"data": [json.loads(response.text)]
}
data_structure["data"].append(week_data)
with open("calendar.json", "w", encoding="utf-8") as json_file:
json.dump(data_structure, json_file, ensure_ascii=False, indent=4)