This repository was archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
233 lines (201 loc) · 10.4 KB
/
Copy pathmain.py
File metadata and controls
233 lines (201 loc) · 10.4 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
import sys
import json
import re
from datetime import datetime, timedelta
import getopt
from getpass import getpass
import requests
sys.path.insert(1, sys.path[0] + '/pyrsa')
from PyRsa.pyrsa import RsaKey
from PyRsa.pyb64 import Base64
class Jwxt(object):
def __init__(self, domain, student_number, pwd):
self.__ua = 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2'
self.__domain = domain
self.__student_number = student_number
self.session = requests.session()
self.__csrf_token, self.__cookies_route = self.__get_csrf_token()
self.__modulus, self.__exponent = self.__get_public_key()
self.__jsessionid = self.__get_jsessionid(pwd)
def __get_public_key(self):
url = f'https://{self.__domain}/xtgl/login_getPublicKey.html'
headers = {
'Host': self.__domain,
'User-Agent': self.__ua,
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate, br',
'X-Requested-With': 'XMLHttpRequest',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': f'https://{self.__domain}/xtgl/login_slogin.html',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin'
}
res = self.session.get(url, headers=headers)
res_json = res.json()
return res_json.get('modulus'), res_json.get('exponent')
def __get_csrf_token(self):
url = f'https://{self.__domain}/xtgl/login_slogin.html'
headers = {
'Host': self.__domain,
'User-Agent': self.__ua,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate, br',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-User': '?1',
}
res = self.session.get(url, headers=headers)
route = requests.utils.dict_from_cookiejar(res.cookies)
csrf_line = re.findall(r"value=\"[0-9a-z\-,]+\"", res.text)
csrf = re.findall(r"[0-9a-z\-,]+", csrf_line[0])
return csrf[1], route
def __get_jsessionid(self, pwd):
rsakey = RsaKey()
rsakey.set_public(Base64().b64tohex(self.__modulus), Base64().b64tohex(self.__exponent))
rr = rsakey.rsa_encrypt(pwd)
enpsw = Base64().hex2b64(rr)
data = {
'csrftoken': self.__csrf_token,
'yhm': self.__student_number,
'mm': enpsw
}
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;'
'q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Content-Length': '470',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': self.__domain,
'Origin': f'https://{self.__domain}',
'Referer': f'https://{self.__domain}/xtgl/login_slogin.html',
'Upgrade-Insecure-Requests': '1',
'User-Agent': self.__ua
}
url = f'https://{self.__domain}/xtgl/login_slogin.html'
res = self.session.post(url, headers=headers, data=data)
if len(res.history) and res.history[0].status_code == 302:
cookies = requests.utils.dict_from_cookiejar(res.history[0].cookies)
return cookies
def dump_class_json(self, academic_year, term):
term_map = {'1': '3', '2': '12', '3': '16'}
url = f'https://{self.__domain}/kbcx/xskbcx_cxXsKb.html?gnmkdm=N2151&su={self.__student_number}'
headers = {
'Host': f'{self.__domain}',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0',
'Accept': '*/*',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'X-Requested-With': 'XMLHttpRequest',
'Origin': f'https://{self.__domain}',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': f'https://{self.__domain}/kbcx/xskbcx_cxXskbcxIndex.html?gnmkdm=N2151&layout=default&su={self.__student_number}',
'Cookie': f"JSESSIONID={self.__jsessionid['JSESSIONID']}; route={self.__cookies_route['route']}",
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Cache-Control': 'no-cache',
}
data = {
'xnm': academic_year,
'xqm': term_map[term],
'kzlx': 'ck'
}
res = self.session.post(url=url, headers=headers, data=data)
return json.loads(res.text)
class ClassTable(object):
def __init__(self, start_date):
self.__start_date = start_date
self.class_table = []
def read_class(self, json_obj):
for i in json_obj['kbList']:
weeks_group = i.get('zcd').split(',')
weeks_group = list(map(lambda s: re.findall(r"\d+", s), weeks_group))
weeks_group = list(map(lambda s: s + s if len(s) == 1 else s, weeks_group)) # 单周补齐list最后一个元素
weeks_group = list(map(lambda s: list(map(lambda y: int(y), s)), weeks_group)) # str to int
# 因为套了2层list,最外层map的参数是[[]](给lambda的参数是[]]),内层map参数是[](给lambda就是单个元素)
self.class_table.append(dict(name=i.get('kcmc'), start_end_time=i.get('jcs').split('-'),
weekday=int(i.get('xqj')),
week_start_end=weeks_group,
location=i.get('cdmc'), teacher=i.get('xm')))
return self.class_table
def cal_gen(self, file_name):
begin_time = {'1': timedelta(hours=8, minutes=0), '2': timedelta(hours=8, minutes=50),
'3': timedelta(hours=10, minutes=0), '4': timedelta(hours=10, minutes=50),
'5': timedelta(hours=13, minutes=30), '6': timedelta(hours=14, minutes=20),
'7': timedelta(hours=15, minutes=15), '8': timedelta(hours=16, minutes=5),
'9': timedelta(hours=18, minutes=0), '10': timedelta(hours=18, minutes=50),
'11': timedelta(hours=19, minutes=40)}
end_time = {'1': timedelta(hours=8, minutes=45), '2': timedelta(hours=9, minutes=35),
'3': timedelta(hours=10, minutes=45), '4': timedelta(hours=11, minutes=35),
'5': timedelta(hours=14, minutes=15), '6': timedelta(hours=15, minutes=5),
'7': timedelta(hours=16, minutes=00), '8': timedelta(hours=16, minutes=50),
'9': timedelta(hours=18, minutes=45), '10': timedelta(hours=19, minutes=35),
'11': timedelta(hours=20, minutes=25)}
payload = 'BEGIN:VCALENDAR\nPRODID:-//wirano@github//CJLU ClassTable ' \
'Gen//EN\nVERSION:2.0\nCALSCALE:GREGORIAN\nBEGIN:VTIMEZONE\nTZID:Asia/Shanghai\nTZURL:http' \
'://tzurl.org/zoneinfo-outlook/Asia/Shanghai\nX-LIC-LOCATION:Asia/Shanghai\nBEGIN:STANDARD' \
'\nTZOFFSETFROM:+0800\nTZOFFSETTO:+0800\nTZNAME:CST\nDTSTART:19700101T000000\nEND:STANDARD\nEND' \
':VTIMEZONE'
for i in self.class_table:
for j in i['week_start_end']:
payload += '\nBEGIN:VEVENT\nDTSTAMP:' + datetime.now().strftime("%Y%m%dT%H%M%SZ") + \
'\nDTSTART;TZID=Asia/Shanghai:' + \
(self.__start_date + timedelta(weeks=j[0] - 1, days=i['weekday'] - 1) + begin_time[
i['start_end_time'][0]]).strftime("%Y%m%dT%H%M%S") + \
'\nDTEND;TZID=Asia/Shanghai:' + \
(self.__start_date + timedelta(weeks=j[0] - 1, days=i['weekday'] - 1) + end_time[
i['start_end_time'][1]]).strftime("%Y%m%dT%H%M%S") + \
'\nRRULE:FREQ=WEEKLY;COUNT=' + str(j[1] - j[0] + 1) + \
'\nSUMMARY:' + i['name'] + \
'\nLOCATION:' + i['location'] + \
'\nDESCRIPTION:' + i['teacher'] + \
'\nBEGIN:VALARM' + '\nTRIGGER:-PT15M' + '\nREPEAT:1' + '\nDURATION:PT1M' + '\nEND:VALARM' + '\nEND:VEVENT'
payload += '\nEND:VCALENDAR'
f = open(file_name, 'w')
f.write(payload)
f.close()
def main(argv):
doc = f"Usage: {os.path.basename(sys.argv[0])} <options...>\n" + " -s, --student_number <student number> Your student number\n" + " -d, --start_date <date> Term start date(ISO format e.g. 2021-09-06)\n" + " -o, --output <file name> Output file name\n"
file_name = 'timetable.ics'
try:
opts, args = getopt.getopt(argv[1:], "hs:d:o", ["help", "student_number=", "start_date=", "output"])
except getopt.GetoptError:
print(doc)
sys.exit(2)
for opt, arg in opts:
if opt in ('-s', '--student_number'):
s_num = arg
elif opt in ('-d', '--start_date'):
start_date = datetime.fromisoformat(arg)
if start_date.month > 6:
term = '1'
academic_year = str(start_date.year)
else:
term = '2'
academic_year = str(start_date.year - 1)
elif opt in ('-o', '--output'):
file_name = arg
else:
print(doc)
sys.exit(0)
jw = Jwxt('jwxt.cjlu.edu.cn', s_num, getpass('Enter your password:'))
table = jw.dump_class_json(academic_year, term)
ct = ClassTable(start_date)
ct.read_class(table)
ct.cal_gen(file_name)
if __name__ == '__main__':
main(sys.argv)