This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathmain.py
344 lines (296 loc) · 10.9 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import base64
import json
import os
import random
import requests
import time
# 小北学生 账号密码
USERNAME = os.getenv("XB_USERNAME")
PASSWORD = os.getenv("XB_PASSWORD")
# 经纬度
LOCATION = os.getenv("XB_LOCATION")
# 位置,可选通过接口获取
COORD = os.getenv("XB_COORD")
#tgbot推送
TG_BOT_TOKEN = os.getenv("TG_BOT_TOKEN")
TG_CHATID = os.getenv("TG_CHATID")
TG_URL = os.getenv("TG_URL")
#server酱
SENDKEY = os.getenv("XB_SENDKEY")
# 企业微信应用
WX_APP = os.getenv("XB_WXAPP")
# 基本链接
BASE_URL = "https://xiaobei.yinghuaonline.com/xiaobei-api/"
# header
HEADERS = {
"user-agent": "iPhone10,3(iOS/14.4) Uninview(Uninview/1.0.0) Weex/0.26.0 1125x2436",
"accept": "*/*",
"accept-language": "zh-cn",
"accept-encoding": "gzip, deflate, br"
}
def is_open():
import platform
# 只在win系统下打开
if platform.system() == 'Windows':
reply = str(input("选择是否去获取经纬度,此操作会打开默认浏览器[Y/N]:"))
if reply == 'Y':
import webbrowser
webbrowser.open("https://api.xiaobaibk.com/api/map/")
else:
pass
else:
print("请在浏览器里打开链接获取经纬度:https://api.xiaobaibk.com/api/map/")
if SENDKEY is None:
SENDKEY =''
if WX_APP is None:
WX_APP = ''
if TG_BOT_TOKEN is None:
TG_BOT_TOKEN = ''
if TG_CHATID is None:
TG_CHATID = ''
# 判断环境变量里是否为空
if USERNAME is None or PASSWORD is None:
USERNAME = str(input("请输入小北学生账号:"))
PASSWORD = str(input("请输入小北学生密码:"))
is_open()
LOCATION = str(input("请将您所复制的经纬度粘贴到此处:"))
print("登录网站【https://sct.ftqq.com/】获取sendkey")
SENDKEY = input("[留空则不开启]server酱通知密钥:")
print("微信通知,开启需填写KEY,教程:https://ghurl.github.io/?130")
WX_APP = input("微信通知密钥,留空则不开启:")
PASSWORD = str(base64.b64encode(PASSWORD.encode()).decode())
else:
PASSWORD = str(base64.b64encode(PASSWORD.encode()).decode())
def get_location():
lc = LOCATION.split(',')
location = lc[1] + ',' + lc[0]
url = "https://api.xiaobaibk.com/api/location/?location=" + location
try:
result = requests.get(url).text
except:
print("获取地址失败!")
wxapp_notify('😂由于获取位置信息失败打卡不成功,估计接口服务器崩了吧', '小北打卡失败')
data = json.loads(result)
if data['status'] == 0:
province = data['result']['addressComponent']['province']
city = data['result']['addressComponent']['city']
district = data['result']['addressComponent']['district']
return '中国-' + province + '-' + city + '-' + district
else:
print("位置获取失败,程序终止")
os._exit(0)
def get_param(coord):
# 体温随机为35.7~36.7
temperature = str(random.randint(357, 367) / 10)
# 107.807008,26.245838
rand = random.randint(1111, 9999)
# 经度
location_x = LOCATION.split(',')[0].split('.')[0] + '.' + LOCATION.split(',')[0].split('.')[1][0:2] + str(rand)
# 纬度
location_y = LOCATION.split(',')[1].split('.')[0] + '.' + LOCATION.split(',')[1].split('.')[1][0:2] + str(rand)
location = location_x + ',' + location_y
return {
"temperature": temperature,
"coordinates": coord,
"location": location,
"healthState": "1",
"dangerousRegion": "2",
"dangerousRegionRemark": "",
"contactSituation": "2",
"goOut": "1",
"goOutRemark": "",
"remark": "无",
"familySituation": "1"
}
def sc_send(context):
baseUrl = 'https://sctapi.ftqq.com/'+SENDKEY+'.send'
resp = None
data = {
"text": context,
"desp": context
}
try:
resp = requests.post(baseUrl, data=data).text
# {"code":0,"message":"","data":{"pushid":"35319564","readkey":"SCT1c4Qpzp0F9u7","error":"SUCCESS","errno":0}}
except:
print("server酱通知失败了")
resp = json.loads(resp)
if resp['code'] != 0:
print(resp['message'])
def tg_send(context):
bot_token = TG_BOT_TOKEN
chat_id = TG_CHATID
if not bot_token or not chat_id:
print("未设置bot_token或chat_id")
return
if TG_URL :
url = f"{TG_URL}/bot{TG_BOT_TOKEN}/sendMessage"
else:
url = f"https://api.telegram.org/bot{TG_BOT_TOKEN}/sendMessage"
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'chat_id': str(TG_CHATID), 'text': f'{context}', 'disable_web_page_preview': 'true'}
try:
response = requests.post(url=url,headers=headers,params=payload)
except:
"TG推送失败"
else:
"TG推送完成"
# 一言
def yiyan():
try:
txt = requests.get("https://api.xiaobaibk.com/api/yiyan.php").text
except:
txt = '随言获取失败,不清楚什么问题,问问作者吧'
return txt
def wxapp_notify(content,title='小北成功打卡通知'):
app_params = WX_APP.split(',')
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
headers = {
'Content-Type': 'application/json',
}
payload = {
'corpid': app_params[0],
'corpsecret': app_params[1],
}
try:
response = requests.post(url=url, headers=headers, data=json.dumps(payload), timeout=15).json()
except:
print("微信通知发送不成功!")
os._exit(0)
accesstoken = response["access_token"]
content = "打卡情况:[" + content + "]\n打卡位置:[" + COORD + "]\n打卡日期:[" + time.strftime("%Y-%m-%d") + "]\n随言:["+yiyan()+"]"
html = content.replace("\n", "<br/>")
options = {
'msgtype': 'mpnews',
'mpnews': {
'articles': [
{
'title': title,
'thumb_media_id': f'{app_params[4]}',
'author': '小白',
'content_source_url': '',
'content': f'{html}',
'digest': f'{content}'
}
]
}
}
url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={accesstoken}"
data = {
'touser': f'{app_params[2]}',
'agentid': f'{app_params[3]}',
'safe': '0'
}
data.update(options)
headers = {
'Content-Type': 'application/json',
}
response = requests.post(url=url, headers=headers, data=json.dumps(data)).json()
if response['errcode'] == 0:
print('企业微信应用通知成功!')
else:
print('企业微信应用通知失败!')
if __name__ == '__main__':
# Url
# 滑动验证
captcha = BASE_URL + 'captchaImage'
# captcha = 'https://xiaobei.yinghuaonline.com/xiaobei-api/captchaImage'
# https://xiaobei.yinghuaonline.com/xiaobei-api/captchaImage
# 登录
login = BASE_URL + 'login'
# 打卡
health = BASE_URL + 'student/health'
# post method return 500 , So use the get method
# data: {"msg":"操作成功","img":"xxxxxx","code":200,"showCode":"NM6B","uuid":"4f72776b789b44d796722037ba7a1ff0"}
try:
response = requests.get(url=captcha, headers=HEADERS).text
except:
print("获取验证码出现错误!")
wxapp_notify('😂估计小北服务器崩了或者在升级中,稍后运行脚本或者自行在软件打卡', '小北打卡失败')
os._exit(0)
# 取得uuid及showCode
uuid = json.loads(response)['uuid']
showCode = json.loads(response)['showCode']
data = {
"username": USERNAME,
"password": PASSWORD,
"code": showCode,
"uuid": uuid
}
# 登录测试
# success return {"msg":"操作成功","code":200,"token":"eyJhb....."}
# error return {"msg":"用户不存在/密码错误","code":500}
try:
res = requests.post(url=login, headers=HEADERS, json=data).text
except:
print("用户登录不成功!")
wxapp_notify('😂估计小北服务器崩了或者在升级中,稍后运行脚本或者自行在软件打卡', '小北打卡失败')
os._exit(0)
code = json.loads(res)['code']
msg = json.loads(res)['msg']
if code != 200:
print("Sorry! Login failed! Error:" + msg)
# server酱
if SENDKEY != '':
sc_send("登录失败,失败原因:" + msg)
#
if WX_APP != '':
wxapp_notify("登录失败,失败原因:" + msg)
else:
print("登录成功!")
# HEADERS.update({'authorization', token})
# 换个方法
HEADERS['authorization'] = json.loads(res)['token']
# 获取位置
if COORD is None or COORD == '':
try:
COORD = get_location()
except:
print("位置信息获取失败!")
err = '😩可以正常登录但是位置信息获取失败,所以打卡失败了'
if SENDKEY != '':
sc_send(err)
if WX_APP != '':
wxapp_notify(err, '小北打卡失败')
os._exit(0)
else:
pass
health_param = None
print(COORD)
if LOCATION is not None and COORD is not None:
health_param = get_param(COORD)
else:
print("必要参数为空!")
try:
respond = requests.post(url=health, headers=HEADERS, json=health_param).text
except:
print("打卡失败!")
if SENDKEY != '':
sc_send("打卡失败!")
if WX_APP != '':
wxapp_notify('😩可以正常登录但是遇到异常,原因不明,请自行打卡', '小北打卡失败')
os._exit(0)
# error return {'msg': None, 'code': 500}
# succeed return {'msg': '操作成功', 'code': 200}
status = json.loads(respond)['code']
if status == 200:
print("恭喜您打卡成功啦!")
# server酱
if SENDKEY != '':
sc_send("打卡成功啦🎉")
if TG_BOT_TOKEN and TG_CHATID != '':
tg_send("打卡成功啦🎉")
#
if WX_APP != '':
wxapp_notify("打卡成功啦🎉")
else:
print("Error:" + json.loads(respond)['msg'])
print(SENDKEY)
# server酱
if SENDKEY != '':
sc_send("🙁抱歉打卡失败了,原因未知,请自行手动打卡,谢谢")
if TG_BOT_TOKEN and TG_CHATID != '':
tg_send("🙁抱歉打卡失败了,原因未知,请自行手动打卡,谢谢")
#
if WX_APP != '':
wxapp_notify("🙁抱歉打卡失败了,请自行手动打卡,谢谢--->失败原因:"+json.loads(respond)['msg'], '打卡失败')