-
Notifications
You must be signed in to change notification settings - Fork 323
/
fstate_generator.py
206 lines (171 loc) · 7.38 KB
/
fstate_generator.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
import base64
import datetime as dt
import json
import re
from pathlib import Path
from bs4 import BeautifulSoup
from PIL import Image, ImageDraw, ImageFont
def _generate_fstate_base64(fstate):
fstate_json = json.dumps(fstate, ensure_ascii=False)
fstate_bytes = fstate_json.encode("utf-8")
return base64.b64encode(fstate_bytes).decode()
def generate_fstate_day(BaoSRQ, ShiFSH, JinXXQ, ShiFZX, XiaoQu,
ddlSheng, ddlShi, ddlXian, ddlJieDao, XiangXDZ, ShiFZJ,
XingCM):
with open(Path(__file__).resolve().parent.joinpath('fstate_day.json'), encoding='utf8') as f:
fstate = json.loads(f.read())
fstate['p1_BaoSRQ']['Text'] = BaoSRQ
fstate['p1_P_GuoNei_ShiFSH']['SelectedValue'] = ShiFSH
fstate['p1_P_GuoNei_JinXXQ']['SelectedValueArray'][0] = JinXXQ
fstate['p1_P_GuoNei_ShiFZX']['SelectedValue'] = ShiFZX
fstate['p1_P_GuoNei_XiaoQu']['SelectedValue'] = XiaoQu
fstate['p1_ddlSheng']['F_Items'] = [[ddlSheng, ddlSheng, 1, '', '']]
fstate['p1_ddlSheng']['SelectedValueArray'] = [ddlSheng]
fstate['p1_ddlShi']['F_Items'] = [[ddlShi, ddlShi, 1, '', '']]
fstate['p1_ddlShi']['SelectedValueArray'] = [ddlShi]
fstate['p1_ddlXian']['F_Items'] = [[ddlXian, ddlXian, 1, '', '']]
fstate['p1_ddlXian']['SelectedValueArray'] = [ddlXian]
fstate['p1_ddlJieDao']['F_Items'] = [[ddlJieDao, ddlJieDao, 1, '', '']]
fstate['p1_ddlJieDao']['SelectedValueArray'] = [ddlJieDao]
fstate['p1_XiangXDZ']['Text'] = XiangXDZ
fstate['p1_ShiFZJ']['SelectedValue'] = ShiFZJ
fstate['p1_P_GuoNei_pImages_HFimgXingCM']['Text'] = XingCM
fstate_base64 = _generate_fstate_base64(fstate)
t = len(fstate_base64) // 2
fstate_base64 = fstate_base64[:t] + 'F_STATE' + fstate_base64[t:]
return fstate_base64
def _html_to_json(html):
return json.loads(html[html.find('=') + 1:])
def get_ShouJHM(sess):
print('#正在获取个人信息...')
ShouJHM = '111111111'
r = sess.get(f'https://selfreport.shu.edu.cn/PersonInfo.aspx')
t = re.findall(r'^.*//\]', r.text, re.MULTILINE)[0]
htmls = t.split(';var ')
for i, h in enumerate(htmls):
try:
if 'ShouJHM' in h:
print('-ShouJHM-')
ShouJHM = _html_to_json(htmls[i - 1])['Text']
except:
print('获取个人信息有错误', htmls[i - 1])
return ShouJHM
def get_last_report(sess, t):
print('#正在获取前一天的填报信息...')
ShiFSH = '在上海(校内)'
JinXXQ = '宝山'
ShiFZX = '是'
XiaoQu = '宝山'
ddlSheng = '上海'
ddlShi = '上海市'
ddlXian = '宝山区'
ddlJieDao = '大场镇'
XiangXDZ = '上海大学'
ShiFZJ = '是'
t = t - dt.timedelta(days=1)
r = sess.get(f'https://selfreport.shu.edu.cn/ViewDayReport.aspx?day={t.year}-{t.month}-{t.day}')
t = re.findall(r'^.*//\]', r.text, re.MULTILINE)[0]
htmls = t.split(';var ')
for i, h in enumerate(htmls):
try:
if 'ShiFSH' in h:
print('-ShiFSH-')
ShiFSH = _html_to_json(htmls[i - 1])['Text']
print(ShiFSH)
if 'JinXXQ' in h:
print('-JinXXQ-')
JinXXQ = "" if "不在上海" in ShiFSH else _html_to_json(htmls[i - 1])['Text']
print(JinXXQ)
if 'ShiFZX' in h:
print('-ShiFZX-')
ShiFZX = _html_to_json(htmls[i - 1])['SelectedValue']
print(ShiFZX)
if 'XiaoQu' in h:
print('-XiaoQu-')
XiaoQu = "" if "不在上海" in ShiFSH or ShiFZX == "否" else _html_to_json(htmls[i - 1])['Text']
print(XiaoQu)
if 'ddlSheng' in h:
print('-ddlSheng-')
ddlSheng = _html_to_json(htmls[i - 1])['SelectedValueArray'][0]
print(ddlSheng)
if 'ddlShi' in h:
print('-ddlShi-')
ddlShi = _html_to_json(htmls[i - 1])['SelectedValueArray'][0]
print(ddlShi)
if 'ddlXian' in h:
print('-ddlXian-')
ddlXian = _html_to_json(htmls[i - 1])['SelectedValueArray'][0]
print(ddlXian)
if 'ddlJieDao' in h:
print('-ddlJieDao-')
if "不在上海" in ShiFSH:
ddlJieDao = ""
else:
ddlJieDao = _html_to_json(htmls[i - 1])['SelectedValueArray'][0]
if ddlJieDao == '-1':
ddlJieDao = '大场镇'
print(ddlJieDao)
if 'XiangXDZ' in h:
print('-XiangXDZ-')
XiangXDZ = _html_to_json(htmls[i - 1])['Text']
print(f'###{XiangXDZ[-2:]}')
if 'ShiFZJ' in h:
print('-ShiFZJ-')
ShiFZJ = _html_to_json(htmls[i - 1])['SelectedValue']
print(ShiFZJ)
except:
print('获取前一天日报有错误', htmls[i - 1], htmls[i])
return ShiFSH, JinXXQ, ShiFZX, XiaoQu, ddlSheng, ddlShi, ddlXian, ddlJieDao, XiangXDZ, ShiFZJ
def _draw_XingCM(ShouJHM: str, t):
work_path = Path(__file__).resolve().parent
image = Image.open(str(work_path / 'xingcm.jpg'))
font1 = ImageFont.truetype(str(work_path / 'yahei.ttf'), 30)
font2 = ImageFont.truetype(str(work_path / 'yahei.ttf'), 36)
draw = ImageDraw.Draw(image)
draw.text((414, 380), f'{ShouJHM[:3]}****{ShouJHM[-4:]}的动态行程卡', font=font1, fill=(39, 39, 39), anchor='mm')
draw.text((414, 460), '更新于:' + t.strftime('%Y-%m-%d %H:%M:%S'), font=font2, fill=(143, 142, 147), anchor='mm')
img_path = str(Path(__file__).resolve().parent.joinpath('xingcm_a.jpg'))
image.save(img_path, 'jpeg')
return img_path
def upload_img(sess, view_state, ShouJHM, t):
img_path = _draw_XingCM(ShouJHM, t)
target = 'p1$P_GuoNei$pImages$fileXingCM'
with open(img_path, 'rb') as f:
r = sess.post('https://selfreport.shu.edu.cn/DayReport.aspx', data={
'__EVENTTARGET': target,
'__EVENTARGUMENT': '',
'__VIEWSTATE': view_state,
'p1$BaoSRQ': t.strftime('%Y-%m-%d')
}, files={
target: f
}, headers={
'X-Requested-With': 'XMLHttpRequest',
'X-FineUI-Ajax': 'true'
}, allow_redirects=False)
ret = re.search(r'Text":"(.*?)"}\)', r.text)
if ret is None:
print(r.text)
return None
else:
return ret.group(1)
def get_img_value(sess, ShouJHM, t):
print('#正在获取随申码、行程码信息...')
XingCM = 'cYskH72v3ZA='
r = sess.get(f'https://selfreport.shu.edu.cn/DayReport.aspx')
soup = BeautifulSoup(r.text, 'html.parser')
view_state = soup.find('input', attrs={'name': '__VIEWSTATE'})
view_state = view_state['value']
ret = re.findall(r'^.*//\]', r.text, re.MULTILINE)[0]
htmls = ret.split(';var ')
for i, h in enumerate(htmls):
if 'p1_P_GuoNei_pImages_HFimgXingCM' in h:
try:
XingCM = _html_to_json(htmls[i - 1])['Text']
except:
print('没有获取到已提交行程码,开始自动上传')
code = upload_img(sess, view_state, ShouJHM, t)
if code is None:
print('上传行程码失败,使用默认行程码')
else:
XingCM = code
return XingCM