-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
219 lines (175 loc) · 6 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
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
#!usr/bin/env python
# -*- coding:utf-8 -*-
""" PyCharm
@author: oGsLP
@file: main.py
@time: 2020/12/24 12:40
@function: automatically choose class for nju masters
"""
import time
import requests
import yaml
from util.des_encrypt import strEncSimple
from util.number_identifier import NumberIdentifier
WEU = ""
cookie = "_WEU=%s; _ga=GA1.3.624467535.1605355636; route=cd43b6d3bb4369a40cab748104f44526; JSESSIONID=F8ohqfxR9ePxSJIEp08Kc4G71JI_sXMvQJ1sm7R_CCvuyqHgxyjk!358279823; XK_TOKEN=ebca04d2-22a5-407a-944e-a4122a530dc0"
root_url = "https://yjsxk.nju.edu.cn/yjsxkapp/sys/xsxkapp/"
index_url = root_url + "login/4/vcode.do?timestamp=%d"
image_url = root_url + "login/vcode/image.do?vtoken=%s"
login_url = root_url + "login/check/login.do?timestrap=%d"
list_url = root_url + "xsxkHome/loadPublicInfo_course.do"
courses_url = root_url + "xsxkCourse/loadFanCourseInfo.do?_=%d"
choose_url = root_url + "xsxkCourse/choiceCourse.do?_=%d"
headers = {
"Accept": 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Connection': 'keep-alive',
'Content-Length': '86',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': "",
'Host': 'yjsxk.nju.edu.cn',
'Origin': 'https://yjsxk.nju.edu.cn',
'Referer': 'https://yjsxk.nju.edu.cn/yjsxkapp/sys/xsxkapp/course_nju.html',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.47',
'X-Requested-With': 'XMLHttpRequest'
}
refresh_headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Connection': 'keep-alive',
'Cookie': "",
'Host': 'yjsxk.nju.edu.cn',
'Referer': 'https://yjsxk.nju.edu.cn/yjsxkapp/sys/xsxkapp/course_nju.html',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.47'
}
courses = [
# "20202-032-085212C01-1604969834471",
# "20202-032-085212C02-1604969833474",
# "20202-032-085212D02-1604969834149",
# "20202-032-085212D14-1604969833901",
# "20202-032-085212D17-1604969834440",
# "20202-032-085212D13-1604969833733"
]
names = [
# "人机交互",
# "敏捷开发",
# "分布式系统",
# "大数据",
# "软件过程改进",
# "体系结构"
]
name = ""
passwd = ""
pic = 'number.jpeg'
csrftoken = ""
vtoken = ""
identifier = None
def configure_identifier():
with open('./configuration.yaml', 'r', encoding='utf-8') as f:
file_content = f.read()
configuration = yaml.load(file_content, yaml.FullLoader)
# configure number & passwd by the way
global name, passwd
name = configuration["yjsxk"]["number"]
passwd = configuration["yjsxk"]["password"]
return NumberIdentifier(configuration["chaojiying"]["username"], configuration["chaojiying"]["password"],
configuration["chaojiying"]["softid"])
def get_vtoken():
res = requests.get(index_url % get_timestamp())
data = res.json()
global vtoken
if int(data["code"]) == 1:
vtoken = data["data"]["token"]
res.close()
def get_image():
res = requests.get(image_url % vtoken)
with open(pic, 'wb') as f:
f.write(res.content)
def login():
global identifier, WEU, headers, refresh_headers
identifier = configure_identifier()
get_vtoken()
get_image()
verify_code = identifier.post_pic(open(pic, 'rb').read(), 1902)["pic_str"]
code = 0
res = None
while code != 1:
res = requests.post(login_url % get_timestamp(), data={
"loginName": name,
"loginPwd": strEncSimple(passwd),
"verifyCode": verify_code,
"vtoken": vtoken
})
data = res.json()
print(data)
code = int(data["code"])
for c in res.cookies:
if c.name == "_WEU":
WEU = c.value
break
headers["Cookie"] = cookie % WEU
refresh_headers["Cookie"] = cookie % WEU
res.close()
def refresh_csrftoken():
res = requests.get(list_url, headers=refresh_headers)
data = res.json()
global csrftoken
csrftoken = data["csrfToken"]
res.close()
def load_courses():
res = requests.post(courses_url % get_timestamp(), data={
"pageIndex": 1,
"pageSize": 20
}, headers=headers)
data = (res.json())["datas"]
for item in data:
names.append(f"{item['KCDM']} - {item['KCMC']} ({item['BJMC']})")
courses.append(item["BJDM"])
def crawl_course(i):
res = requests.post(
choose_url % get_timestamp(), data={
"bjdm": courses[i],
"lx": 2,
"csrfToken": csrftoken
}, headers=headers)
data = res.json()
print(" [%s]: %s" % (names[i], data["msg"]))
res.close()
return int(data['code']) == 1
def get_timestamp():
return int(time.time() * 1000)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
count = 0
start = time.time()
login()
refresh_csrftoken()
load_courses()
while True:
if time.time() - start > 2 * 60 * 60:
start = time.time()
login()
refresh_csrftoken()
print(
"(%s) <csrfToken: %s> COUNT:%d" % (
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), csrftoken, count + 1))
for i in range(len(courses)):
bl = crawl_course(i)
if bl:
del courses[i]
del names[i]
break
# time.sleep(0.05)
time.sleep(0.1)
count = count + 1
if count > 1:
refresh_csrftoken()
count = 0