-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
169 lines (145 loc) · 6.53 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
from constant import *
from Wecom import *
import requests, re, json, copy, traceback
from lxml import etree
def ncov_report(data, headers, is_useold):
print('登录北邮 nCoV 上报网站')
login_res = session.post(
url=LOGIN_API, headers=headers, data=data
)
if login_res.status_code != 200:
raise RuntimeError('login_res 状态码不是 200')
get_res = session.get(
GET_API,
headers={**COMMON_HEADERS, 'Accept': HEADERS.ACCEPT_HTML},
)
if get_res.status_code != 200:
raise RuntimeError('get_res 状态码不是 200')
try:
old_data = json.loads('{' + re.search(r'(?<=oldInfo: {).+(?=})', get_res.text)[0] + '}')
except:
raise RuntimeError('未获取到昨日打卡数据,请今日手动打卡明日再执行脚本或使用固定打卡数据')
post_data = json.loads(copy.deepcopy(INFO).replace("\n", "").replace(" ", ""))
if is_useold:
try:
for k, v in old_data.items():
if k in post_data:
post_data[k] = v
geo = json.loads(old_data['geo_api_info'])
province = geo['addressComponent']['province']
city = geo['addressComponent']['city']
if geo['addressComponent']['city'].strip() == "" and len(re.findall(r'北京市|上海市|重庆市|天津市', province)) != 0:
city = geo['addressComponent']['province']
area = province + " " + city + " " + geo['addressComponent']['district']
address = geo['formattedAddress']
post_data['province'] = province
post_data['city'] = city
post_data['area'] = area
post_data['address'] = address
# 强行覆盖一些字段
post_data['ismoved'] = 0 # 是否移动了位置?否
post_data['bztcyy'] = '' # 不在同城原因?空
post_data['sfsfbh'] = 0 # 是否省份不合?否
except:
print("加载昨日数据错误,采用固定数据")
post_data = json.loads(copy.deepcopy(INFO).replace("\n", "").replace(" ", ""))
report_res = session.post(
REPORT_API,
data=post_data,
headers=headers,
)
if report_res.status_code != 200:
raise RuntimeError('report_res 状态码不是 200')
return post_data,report_res.text
def ncov_even_report(data,headers, is_useold):
print('登录北邮 nCoV 上报网站')
login_res = session.post(
url=LOGIN_API, headers=headers, data=data
)
if login_res.status_code != 200:
raise RuntimeError('login_res 状态码不是 200')
get_res = session.get(
GET_API,
headers={**COMMON_HEADERS, 'Accept': HEADERS.ACCEPT_HTML},
)
if get_res.status_code != 200:
raise RuntimeError('get_res 状态码不是 200')
try:
old_data = json.loads('{' + re.search(r'(?<=oldInfo: {).+(?=})', get_res.text)[0] + '}')
except:
raise RuntimeError('未获取到昨日打卡数据,请今日手动打卡明日再执行脚本或使用固定打卡数据')
get_res = session.get(
GETEven_API,
headers={**COMMON_HEADERS, 'Accept': HEADERS.ACCEPT_HTML},
)
if get_res.status_code != 200:
raise RuntimeError('get_res 状态码不是 200')
post_data = json.loads(copy.deepcopy(INFO_E))
if is_useold:
try:
for k, v in old_data.items():
if k in post_data:
post_data[k] = v
geo = old_data
info=geo['geo_api_info']
geo=json.loads(info)
province = geo['addressComponent']['province']
city = geo['addressComponent']['city']
district=geo['addressComponent']['district']
if geo['addressComponent']['city'].strip() == "" and len(re.findall(r'北京市|上海市|重庆市|天津市', province)) != 0:
city = geo['addressComponent']['province']
#area = province + " " + city + " " + geo['addressComponent']['district']
area = province+city+district
address = geo['formattedAddress']
post_data['geo_api_info']=info
post_data['province'] = province
post_data['city'] = city
post_data['area'] = area
post_data['address'] = address
# 强行覆盖一些字段
except:
print("加载上次晨午晚检数据错误,采用固定数据")
post_data = json.loads(copy.deepcopy(INFO_E).replace("\n", "").replace(" ", ""))
report_res = session.post(
POSTEven_API,
data=post_data,
headers=headers,
)
if report_res.status_code != 200:
raise RuntimeError('report_res 状态码不是 200')
return post_data,report_res.text
successs,ress,usernames,names,datas = [],[],[],[],[]
for user in USERS:
# 设置连接
session = requests.Session()
wecom_cid,wecom_aid,wecom_secret=WECOM
# 发送请求,设置cookies
headers = { "User-Agent": USER_AGENT}
params = { "service": SERVICE }
responce = session.get(url=LOGIN_API, headers=headers, params=params)
# 获取execution
html = etree.HTML(responce.content)
execution = html.xpath(EXECUTION_XPATH)[0]
username,password,name,useold=user
# 构造表单数据
data = {
'username': username,
'password': password,
'submit': "登录",
'type': 'username_password',
'execution': execution,
'_eventId': "submit"
}
success=True
try:
data,res = ncov_report(data,headers,is_useold=(useold==0))
data_even,res_even = ncov_even_report(data,headers,is_useold=(useold==0))
except:
success = False
data,res = '',traceback.format_exc()
msg1=f' {datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} {name}《每日填报》填报成功!服务器返回数据:\n{res}\n\n每日填报填报数据:\n{data}\n' if success else f'{name}《每日填报》填报失败!发生如下异常:\n{res}'
print(msg1)
send_to_wecom(msg1,wecom_cid, wecom_aid, wecom_secret)
msg2=f' {datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} {name}《晨午晚检》填报成功!服务器返回数据:\n{res_even}\n\n晨午晚检填报数据:\n{data_even}\n' if success else f'{name}《晨午晚检》填报失败!发生如下异常:\n{res}'
print(msg2)
send_to_wecom(msg2,wecom_cid, wecom_aid, wecom_secret)