This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.py
231 lines (207 loc) · 8.33 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
#! usr/bin/python
from bs4 import BeautifulSoup
import requests
from requests.packages import urllib3
import json
import os
import re
from notify import Notify
# import ssl
import time
from requests import exceptions as ex
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
urllib3.disable_warnings()
verify_path = False # 默认不开启SSL验证
# ssl._create_default_https_context = ssl._create_unverified_context
session = requests.session()
def read_json(json_file):
obj = []
try:
obj = json.load(open(json_file, 'r', encoding='utf-8'))
except FileNotFoundError:
print(f'{json_file} not found')
return obj
def get_users():
users = []
USERS = os.environ['UID']
PWD = os.environ['UPW']
SCKEY = os.environ['KEY']
ADDR = os.environ['ADDR']
user_list = USERS.split('&')
pwd_list = PWD.split('&')
sckey_list = SCKEY.split('&')
addr_list = ADDR.split('&')
# assert len(user_list) == len(pwd_list)
for u, p, k, a in zip(user_list,pwd_list,sckey_list,addr_list):
user = dict()
user['uid'] = u
user['upw'] = p
user['key'] = k
user['addr'] = a
users.append(user)
return users
def get_hh28():
url = 'https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/first0?fun2=&door='
header = {
'Host': 'jksb.v.zzu.edu.cn',
'Referer': 'https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/login',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
res = session.get(url=url, headers=header, verify=verify_path)
html = res.text
soup = BeautifulSoup(html, 'lxml')
hh28 = soup.find('input',{'name':'hh28'}).get('value')
return hh28
# 登录
def login(user_data, key):
url = "https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/login"
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
'Referer': 'https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/first0?fun2=&door=',
'Origin': 'https://jksb.v.zzu.edu.cn',
'Host': 'jksb.v.zzu.edu.cn'
}
data = {
'uid':user_data['uid'],
'upw':user_data['upw'],
'smbtn':'进入健康状况上报平台',
'hh28':key
}
res = session.post(url=url, data=data, headers=header, verify=verify_path)
if res.status_code != 200:
return '',''
res.encoding = 'utf-8'
html = res.text
soup = BeautifulSoup(html, 'lxml')
content = soup.find('script').text
url1 = re.search('"https.*?"', content)
if url1 is None:
return '',''
url1 = url1.group()[1:-1]
header2 = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
'Referer': 'https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/login',
'Host': 'jksb.v.zzu.edu.cn'
}
res = session.get(url=url1, headers=header2, verify=verify_path)
res.encoding = 'utf-8'
html = res.text
soup = BeautifulSoup(html, 'lxml')
url2 = soup.find('iframe', {'id':'zzj_top_6s'}).get('src')
return url1, url2
# 获取通行
def get_permit_data(refer, url):
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
'Referer': refer,
'Host': 'jksb.v.zzu.edu.cn'
}
res = session.get(url=url, headers=header, verify=verify_path)
res.encoding = 'utf-8'
html = res.text
soup = BeautifulSoup(html, 'lxml')
state = soup.select('#bak_0 > div:nth-child(5) > span')[0].get_text()
data = dict()
if state == "今日您已经填报过了":
pass
else:
input_list = soup.find_all('input')
for i in input_list:
if (data.__contains__(i.attrs.get('name'))):
l = list()
l.append(data[i.attrs.get('name')])
l.append(i.attrs.get('value'))
data[i.attrs.get('name')] = l
else:
data[i.attrs.get('name')] = i.attrs.get('value')
return data
# 开始填
def ready_submit(data, refer):
header = {
'Host': 'jksb.v.zzu.edu.cn',
'Origin': 'https://jksb.v.zzu.edu.cn',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
'Referer': refer
}
url = 'https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/jksb'
res = session.post(url=url, data=data, headers=header, verify=verify_path)
res.encoding = 'utf-8'
html = res.text
soup = BeautifulSoup(html, 'lxml')
input_ = soup.find_all('input', {'type':'hidden'})
submit_data = dict()
for i in input_:
submit_data[i.attrs.get('name')] = i.attrs.get('value')
return submit_data
# 逆地址解码
# addr:经纬度
# return:省 市 街道地址门牌号 ('41', '4101', '科学大道100号')
def geocoder(addr):
url = f'https://restapi.amap.com/v3/geocode/regeo?output=json&location={addr}&key=af3d0194d3e0b6d626bd708cb823ed97'
headers = {
'Host': 'restapi.amap.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
response.encoding = 'utf-8'
obj = json.loads(response.content)
formatted_address = obj['regeocode']['formatted_address']
adcode = obj['regeocode']['addressComponent']['adcode']
street = obj['regeocode']['addressComponent']['streetNumber']['street']
number = obj['regeocode']['addressComponent']['streetNumber']['number']
myvs_13a = adcode[0:2]
myvs_13b = adcode[0:4]
myvs_13c = street+number
return myvs_13a,myvs_13b,myvs_13c,formatted_address
# 默认打卡地点是 河南省郑州市科学大道100号
def parse_submit_data(data,json_file,myvs_13a="41",myvs_13b="4101",myvs_13c="科学大道100号",):
submit_data = read_json(json_file)
submit_data['myvs_13a'],submit_data['myvs_13b'],submit_data['myvs_13c'] = myvs_13a,myvs_13b,myvs_13c
submit_data.update(data)
return submit_data
# 提交
def submit(data):
url = 'https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/jksb'
header = {
'Host': 'jksb.v.zzu.edu.cn',
'Origin': 'https://jksb.v.zzu.edu.cn',
'Referer': 'https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/jksb',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
res = session.post(url=url, data=data, headers=header, verify=verify_path)
res.encoding = 'utf-8'
html = res.text
soup = BeautifulSoup(html, 'lxml')
r = soup.select('#bak_0 > div:nth-child(2) > div:nth-child(2) > div:nth-child(2) > div:nth-child(2)')[0]
result = r.get_text().replace(" ", "")
return result
# c = soup.select('#bak_0 > div:nth-child(2) > div:nth-child(2) > div:nth-child(4) > div:nth-child(2)')[0]
# url1 = re.search('\'https.*?\'', str(c))
# print(url1.group()[1:-1])
if __name__ == '__main__':
notify = Notify()
users = get_users()
retry = 5 # 打卡失败重试次数
for user in users:
try:
myvs_13a,myvs_13b,myvs_13c,formatted_address = geocoder(user['addr'])
hh28 = get_hh28()
refer, url = login(user, hh28)
permit_data = get_permit_data(refer, url)
if permit_data: # 如果未填报则开始填报
submit_data = ready_submit(data=permit_data, refer=url)
submit_data = parse_submit_data(submit_data, 'submit_data.json',myvs_13a,myvs_13b,myvs_13c)
result = submit(submit_data)
notify.send(user['key'], user['uid'], result, formatted_address)
else: # 已经打过卡也发送通知
result = "今日您已经填报过了"
notify.send(user['key'], user['uid'], result, formatted_address)
except ex.SSLError:
retry = retry - 1
if retry >= 0:
users.append(user) # 打卡失败重试
else:
result = "SSLError: 打卡失败,请手动打卡!"
notify.send(user['key'], user['uid'], result, formatted_address)
time.sleep(10) # 账号间打卡间隔10s