forked from potatoshred/daily_fudan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailyFudan.py
208 lines (173 loc) · 6.11 KB
/
dailyFudan.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
import time
from json import loads as json_loads
from os import path as os_path
from sys import exit as sys_exit
from sys import argv as sys_argv
from lxml import etree
from requests import session
import logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
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'):
"""
初始化一个session,及登录信息
:param uid: 学号
:param psw: 密码
:param url_login: 登录页,默认服务为空
"""
self.session = session()
self.session.headers['User-Agent'] = self.UA
self.url_login = url_login
self.uid = uid
self.psw = psw
def _page_init(self):
"""
检查是否能打开登录页面
:return: 登录页page source
"""
logging.debug("Initiating——")
page_login = self.session.get(self.url_login)
logging.debug("return status code " + str(page_login.status_code))
if page_login.status_code == 200:
logging.debug("Initiated——")
return page_login.text
else:
logging.debug("Fail to open Login Page, Check your Internet connection\n")
self.close()
def login(self):
"""
执行登录
"""
page_login = self._page_init()
logging.debug("parsing Login page——")
html = etree.HTML(page_login, etree.HTMLParser())
logging.debug("getting tokens")
data = {
"username": self.uid,
"password": self.psw,
"service" : "https://zlapp.fudan.edu.cn/site/ncov/fudanDaily"
}
# 获取登录页上的令牌
data.update(
zip(
html.xpath("/html/body/form/input/@name"),
html.xpath("/html/body/form/input/@value")
)
)
headers = {
"Host" : "uis.fudan.edu.cn",
"Origin" : "https://uis.fudan.edu.cn",
"Referer" : self.url_login,
"User-Agent": self.UA
}
logging.debug("Login ing——")
post = self.session.post(
self.url_login,
data=data,
headers=headers,
allow_redirects=False)
logging.debug("return status code %d" % post.status_code)
if post.status_code == 302:
logging.debug("登录成功")
else:
logging.debug("登录失败,请检查账号信息")
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')
if '01-Jan-1970' in expire:
logging.debug("登出完毕")
else:
logging.debug("登出异常")
def close(self):
"""
执行登出并关闭会话
"""
self.logout()
self.session.close()
logging.debug("关闭会话")
sys_exit()
class Zlapp(Fudan):
last_info = ''
def check(self):
"""
检查
"""
logging.debug("检测是否已提交")
get_info = self.session.get(
'https://zlapp.fudan.edu.cn/ncov/wap/fudan/get-info')
last_info = get_info.json()
logging.info("上一次提交日期为: %s " % last_info["d"]["info"]["date"])
position = last_info["d"]["info"]['geo_api_info']
position = json_loads(position)
logging.info("上一次提交地址为: %s" % position['formattedAddress'])
# logging.debug("上一次提交GPS为", position["position"])
today = time.strftime("%Y%m%d", time.localtime())
if last_info["d"]["info"]["date"] == today:
logging.info("今日已提交")
self.close()
else:
logging.info("未提交")
self.last_info = last_info["d"]["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
}
logging.debug("提交中")
geo_api_info = json_loads(self.last_info["geo_api_info"])
province = geo_api_info["addressComponent"].get("province", "")
city = geo_api_info["addressComponent"].get("city", "") or province
district = geo_api_info["addressComponent"].get("district", "")
self.last_info.update(
{
"tw" : "13",
"province": province,
"city" : city,
"area" : " ".join(set((province, city, district))),
"ismoved" : 0
}
)
# logging.debug(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"]
logging.info(save_msg)
def get_account():
"""
获取账号信息
"""
uid, psw = sys_argv[1].strip().split(' ')
return uid, psw
if __name__ == '__main__':
uid, psw = get_account()
# logging.debug("ACCOUNT:" + uid + psw)
zlapp_login = 'https://uis.fudan.edu.cn/authserver/login?' \
'service=https://zlapp.fudan.edu.cn/site/ncov/fudanDaily'
daily_fudan = Zlapp(uid, psw, url_login=zlapp_login)
daily_fudan.login()
daily_fudan.check()
daily_fudan.checkin()
# 再检查一遍
daily_fudan.check()
daily_fudan.close()