-
Notifications
You must be signed in to change notification settings - Fork 89
/
main.py
319 lines (274 loc) · 10.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import json
import time
import os
from json import loads as json_loads
from os import path as os_path, getenv
from sys import exit as sys_exit
from getpass import getpass
import re
import base64
import easyocr
import random
import io
import numpy
from PIL import Image
from PIL import ImageEnhance
from requests import session, post, adapters
import urllib3, ssl
adapters.DEFAULT_RETRIES = 5
class CustomHttpAdapter (adapters.HTTPAdapter):
# "Transport adapter" that allows us to use custom ssl_context.
def __init__(self, ssl_context=None, **kwargs):
self.ssl_context = ssl_context
super().__init__(**kwargs)
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = urllib3.poolmanager.PoolManager(
num_pools=connections, maxsize=maxsize,
block=block, ssl_context=self.ssl_context)
def get_legacy_session():
ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
ctx.options |= 0x4 # OP_LEGACY_SERVER_CONNECT
mySession = session()
mySession.mount('https://', CustomHttpAdapter(ctx))
return mySession
class Fudan:
"""
建立与复旦服务器的会话,执行登录/登出操作
"""
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"
# 初始化会话
def __init__(self,
uid, psw,
url_login='https://uis.fudan.edu.cn/authserver/login',
url_code="https://zlapp.fudan.edu.cn/backend/default/code"):
"""
初始化一个session,及登录信息
:param uid: 学号
:param psw: 密码
:param url_login: 登录页,默认服务为空
"""
self.session = get_legacy_session()
self.session.keep_alive = False
self.session.headers['User-Agent'] = self.UA
self.url_login = url_login
self.url_code = url_code
self.uid = uid
self.psw = psw
def _page_init(self):
"""
检查是否能打开登录页面
:return: 登录页page source
"""
print("◉Initiating——", end='')
page_login = self.session.get(self.url_login)
print("return status code",
page_login.status_code)
if page_login.status_code == 200:
print("◉Initiated——", end="")
return page_login.text
else:
print("◉Fail to open Login Page, Check your Internet connection\n")
self.close()
def login(self):
"""
执行登录
"""
page_login = self._page_init()
print("getting tokens")
data = {
"username": self.uid,
"password": self.psw,
"service": "https://zlapp.fudan.edu.cn/site/ncov/fudanDaily"
}
# 获取登录页上的令牌
result = re.findall(
'<input type="hidden" name="([a-zA-Z0-9\-_]+)" value="([a-zA-Z0-9\-_]+)"/?>', page_login)
# print(result)
# result 是一个列表,列表中的每一项是包含 name 和 value 的 tuple,例如
# [('lt', 'LT-6711210-Ia3WttcMvLBWNBygRNHdNzHzB49jlQ1602983174755-7xmC-cas'), ('dllt', 'userNamePasswordLogin'), ('execution', 'e1s1'), ('_eventId', 'submit'), ('rmShown', '1')]
data.update(
result
)
headers = {
"Host": "uis.fudan.edu.cn",
"Origin": "https://uis.fudan.edu.cn",
"Referer": self.url_login,
"User-Agent": self.UA
}
print("◉Login ing——", end="")
post = self.session.post(
self.url_login,
data=data,
headers=headers,
allow_redirects=False)
print("return status code", post.status_code)
if post.status_code == 302:
print("\n***********************"
"\n◉登录成功"
"\n***********************\n")
else:
print("◉登录失败,请检查账号信息")
self.close()
def logout(self):
"""
执行登出
"""
exit_url = 'https://uis.fudan.edu.cn/authserver/logout?service=/authserver/login'
expire = self.session.get(exit_url).headers.get('Set-Cookie')
# print(expire)
if '01-Jan-1970' in expire:
print("◉登出完毕")
else:
print("◉登出异常")
def close(self, exit_code=0):
"""
执行登出并关闭会话
"""
self.logout()
self.session.close()
print("◉关闭会话")
print("************************")
sys_exit(exit_code)
class Zlapp(Fudan):
last_info = ''
def check(self):
"""
检查
"""
print("◉检测是否已提交")
get_info = self.session.get(
'https://zlapp.fudan.edu.cn/ncov/wap/fudan/get-info')
last_info = get_info.json()
print("◉上一次提交日期为:", last_info["d"]["info"]["date"])
position = last_info["d"]["info"]['geo_api_info']
position = json_loads(position)
print("◉上一次提交地址为:", position['formattedAddress'])
# print("◉上一次提交GPS为", position["position"])
# print(last_info)
# 改为上海时区
os.environ['TZ'] = 'Asia/Shanghai'
time.tzset()
today = time.strftime("%Y%m%d", time.localtime())
print("◉今日日期为:", today)
if last_info["d"]["info"]["date"] == today:
print("\n*******今日已提交*******")
self.close()
else:
print("\n\n*******未提交*******")
self.last_info = last_info["d"]["oldInfo"]
def read_captcha(self, img_byte):
img = Image.open(io.BytesIO(img_byte)).convert('L')
enh_bri = ImageEnhance.Brightness(img)
new_img = enh_bri.enhance(factor=1.5)
image = numpy.array(new_img)
reader = easyocr.Reader(['en'])
horizontal_list, free_list = reader.detect(image, optimal_num_chars=4)
character = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
allow_list = list(character)
allow_list.extend(list(character.lower()))
result = reader.recognize(image,
allowlist=allow_list,
horizontal_list=horizontal_list[0],
free_list=free_list[0],
detail = 0)
return result[0]
def validate_code(self):
img = self.session.get(self.url_code).content
return self.read_captcha(img)
def disturb_info(self, meter = 1):
geo_api_info = json.loads(self.last_info['geo_api_info'])
position = geo_api_info['position']
Q, R = float(position["Q"]), float(position["R"])
Q += meter * 0.00001 * (2 * random.random() - 1) #【GPS1度约为111km,这里是±meter米扰动】
R += meter * 0.00001 * (2 * random.random() - 1)
position["Q"] = str(Q)
position["R"] = str(R)
position["lng"] = str(round(R, 5))
position["lat"] = str(round(Q, 5))
self.last_info['geo_api_info'] = json.dumps(geo_api_info, ensure_ascii=False)
return geo_api_info
def checkin(self):
"""
提交
"""
headers = {
"Host": "zlapp.fudan.edu.cn",
"Referer": "https://zlapp.fudan.edu.cn/site/ncov/fudanDaily?from=history",
"DNT": "1",
"TE": "Trailers",
"User-Agent": self.UA
}
print("\n\n◉◉提交中")
geo_api_info = self.disturb_info(meter = 1)
province = self.last_info["province"]
city = self.last_info["city"]
district = geo_api_info["addressComponent"].get("district", "")
while(True):
print("◉正在识别验证码......")
code = self.validate_code()
print("◉验证码为:", code)
self.last_info.update(
{
"tw": "13",
"province": province,
"city": city,
"area": " ".join((province, city, district)),
#"sfzx": "1", # 是否在校
#"fxyy": "", # 返校原因
"code": code,
}
)
# print(self.last_info)
save = self.session.post(
'https://zlapp.fudan.edu.cn/ncov/wap/fudan/save',
data=self.last_info,
headers=headers,
allow_redirects=False)
save_msg = json_loads(save.text)["m"]
print(save_msg, '\n\n')
time.sleep(0.1)
if(json_loads(save.text)["e"] != 1):
break
def get_account():
"""
获取账号信息
"""
uid = getenv("STD_ID")
psw = getenv("PASSWORD")
if uid != None and psw != None:
print("从环境变量中获取了用户名和密码!")
return uid, psw
print("\n\n请仔细阅读以下日志!!\n请仔细阅读以下日志!!!!\n请仔细阅读以下日志!!!!!!\n\n")
if os_path.exists("account.txt"):
print("读取账号中……")
with open("account.txt", "r") as old:
raw = old.readlines()
if (raw[0][:3] != "uid") or (len(raw[0]) < 10):
print("account.txt 内容无效, 请手动修改内容")
sys_exit()
uid = (raw[0].split(":"))[1].strip()
psw = (raw[1].split(":"))[1].strip()
else:
print("未找到account.txt, 判断为首次运行, 请接下来依次输入学号密码")
uid = input("学号:")
psw = getpass("密码:")
with open("account.txt", "w") as new:
tmp = "uid:" + uid + "\npsw:" + psw +\
"\n\n\n以上两行冒号后分别写上学号密码,不要加空格/换行,谢谢\n\n请注意文件安全,不要放在明显位置\n\n可以从dailyFudan.exe创建快捷方式到桌面"
new.write(tmp)
print("账号已保存在目录下account.txt,请注意文件安全,不要放在明显位置\n\n建议拉个快捷方式到桌面")
return uid, psw
if __name__ == '__main__':
uid, psw = get_account()
# print(uid, psw)
zlapp_login = 'https://uis.fudan.edu.cn/authserver/login?' \
'service=https://zlapp.fudan.edu.cn/site/ncov/fudanDaily'
code_url = "https://zlapp.fudan.edu.cn/backend/default/code"
daily_fudan = Zlapp(uid, psw,
url_login=zlapp_login, url_code=code_url)
daily_fudan.login()
daily_fudan.check()
daily_fudan.checkin()
# 再检查一遍
daily_fudan.check()
daily_fudan.close(1)