-
Notifications
You must be signed in to change notification settings - Fork 399
/
JDRouterPush.py
405 lines (361 loc) · 15.6 KB
/
JDRouterPush.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import datetime
import time
import pytz
import requests
import GlobalVariable
import JDServiceAPI
import NoticePush
import NoticeTemplate
from urllib.parse import quote
# 获取当天时间和当天积分
def todayPointIncome():
today_total_point = 0
today_date = ""
res = requests.get(GlobalVariable.jd_base_url + "todayPointIncome", headers=GlobalVariable.headers)
if res.status_code == 200:
res_json = res.json()
result = res_json["result"]
today_total_point = result["todayTotalPoint"]
todayDate = result["todayDate"]
today_date = datetime.datetime.strptime(todayDate, "%Y%m%d").strftime("%Y年%m月%d日")
else:
errorMessage = res.json()['error']['message']
print(errorMessage)
print("Request todayPointIncome failed!")
GlobalVariable.final_result["today_date"] = today_date
GlobalVariable.final_result["today_total_point"] = str(today_total_point)
return today_total_point
# 获取今日总积分
def pinTotalAvailPoint():
total_avail_point = 0
res = requests.get(GlobalVariable.jd_base_url + "pinTotalAvailPoint", headers=GlobalVariable.headers)
if res.status_code == 200:
res_json = res.json()
result = res_json["result"]
total_avail_point = result["totalAvailPoint"]
else:
print("Request pinTotalAvailPoint failed!")
GlobalVariable.final_result["total_avail_point"] = str(total_avail_point)
return total_avail_point
# 路由账户信息
def routerAccountInfo(mac):
params = {
"mac": mac,
}
res = requests.get(GlobalVariable.jd_base_url + "routerAccountInfo", params=params, headers=GlobalVariable.headers)
if res.status_code == 200:
res_json = res.json()
result = res_json["result"]
accountInfo = result["accountInfo"]
mac = accountInfo["mac"]
amount = accountInfo["amount"]
bindAccount = accountInfo["bindAccount"]
GlobalVariable.service_headers["pin"] = quote(bindAccount)
recentExpireAmount = accountInfo["recentExpireAmount"]
recentExpireTime = accountInfo["recentExpireTime"]
recentExpireTime_str = datetime.datetime.fromtimestamp(recentExpireTime / 1000).strftime("%Y-%m-%d %H:%M:%S")
account_info = {"amount": str(amount), "bindAccount": str(bindAccount),
"recentExpireAmount": str(recentExpireAmount), "recentExpireTime": recentExpireTime_str}
index = GlobalVariable.findALocation(mac)
if index != -1:
point_info = GlobalVariable.final_result["pointInfos"][index]
point_info.update(account_info)
else:
print("Find mac failure!")
else:
print("Request routerAccountInfo failed!")
# 路由活动信息
def routerActivityInfo(mac):
params = {
"mac": mac,
}
res = requests.get(GlobalVariable.jd_base_url + "router:activityInfo", params=params,
headers=GlobalVariable.headers)
if res.status_code == 200:
res_json = res.json()
result = res_json["result"]
# finishActivity = result["finishActivity"]
if result["routerUnderwayResult"] is None:
exit
else:
totalIncomeValue = result["routerUnderwayResult"]["totalIncomeValue"]
satisfiedTimes = result["routerUnderwayResult"]["satisfiedTimes"]
activity_info = {"mac": mac, "totalIncomeValue": totalIncomeValue, "satisfiedTimes": satisfiedTimes}
index = GlobalVariable.findALocation(mac)
if index != -1:
point_info = GlobalVariable.final_result["pointInfos"][index]
point_info.update(activity_info)
else:
print("Request routerActivityInfo failed!")
# 收益信息
def todayPointDetail():
params = {
"sortField": "today_point",
"sortDirection": "DESC",
"pageSize": "30",
"currentPage": "1"
}
MACS = []
res = requests.get(GlobalVariable.jd_base_url + "todayPointDetail", params=params, headers=GlobalVariable.headers)
if res.status_code == 200:
res_json = res.json()
result = res_json["result"]
todayDate = result["todayDate"]
totalRecord = result["pageInfo"]["totalRecord"]
pointInfos = result["pointInfos"]
GlobalVariable.final_result["todayDate"] = datetime.datetime.strptime(todayDate, "%Y%m%d").strftime("%Y-%m-%d")
GlobalVariable.final_result["totalRecord"] = str(totalRecord)
GlobalVariable.final_result["pointInfos"] = pointInfos
for info in pointInfos:
mac = info["mac"]
MACS.append(mac)
routerActivityInfo(mac)
routerAccountInfo(mac)
pointOperateRecordsShow(mac)
JDServiceAPI.getListAllUserDevices()
for mac in MACS:
JDServiceAPI.getControlDevice(mac, 2)
JDServiceAPI.getControlDevice(mac, 3)
else:
errorMessage = res.json()['error']['message']
print(errorMessage)
print("Request todayPointDetail failed!")
# 点操作记录显示
def pointOperateRecordsShow(mac):
params = {
"source": 1,
"mac": mac,
"pageSize": GlobalVariable.records_num,
"currentPage": 1
}
point_records = []
res = requests.get(GlobalVariable.jd_base_url + "pointOperateRecords:show", params=params,
headers=GlobalVariable.headers)
if res.status_code == 200:
res_json = res.json()
result = res_json["result"]
pointRecords = result["pointRecords"]
for pointRecord in pointRecords:
recordType = pointRecord["recordType"]
pointAmount = pointRecord["pointAmount"]
createTime = pointRecord["createTime"]
createTime_str = datetime.datetime.fromtimestamp(createTime / 1000).strftime("%Y-%m-%d")
point_record = {"recordType": recordType, "pointAmount": pointAmount, "createTime": createTime_str}
point_records.append(point_record)
index = GlobalVariable.findALocation(mac)
if index != -1:
point_info = GlobalVariable.final_result["pointInfos"][index]
point_info.update({"pointRecords": point_records})
else:
print("Request pointOperateRecordsShow failed!")
# 解析设备名称
def resolveDeviceName(DEVICENAME):
if "" == DEVICENAME:
# print("未设置自定义设备名")
pass
else:
devicenames = DEVICENAME.split("&")
for devicename in devicenames:
mac = devicename.split(":")[0]
name = devicename.split(":")[1]
GlobalVariable.device_name.update({mac: name})
# 解析设备ip
def resolveDeviceIP(DEVICE_IP):
if "" == DEVICE_IP:
# print("未设置自定义IP")
pass
else:
deviceIPs = DEVICE_IP.split("&")
for deviceIP in deviceIPs:
mac = deviceIP.split(":")[0]
ip = deviceIP.split(":")[1]
GlobalVariable.device_ip.update({mac: ip})
# 检测更新
def checkForUpdates():
remote_address = "https://raw.githubusercontent.com/leifengwl/JDRouterPush/main/config.ini"
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36"
}
res = requests.get(url=remote_address, headers=GlobalVariable.headers)
if res.status_code == 200:
res_json = res.json()
GlobalVariable.final_result["announcement"] = res_json["announcement"]
if res_json["version"] != GlobalVariable.version:
GlobalVariable.final_result["updates_version"] = res_json["version"]
GlobalVariable.final_result["update_log"] = res_json["updateLog"]
else:
print("欢迎使用JDRouterPush!")
else:
print("checkForUpdate failed!")
# region 通知结果
# 结果显示
def resultDisplay():
today_date = GlobalVariable.final_result["today_date"]
today_total_point = GlobalVariable.final_result["today_total_point"]
title = today_date + "到账积分:" + today_total_point
if GlobalVariable.final_result.get("todayDate") is None:
push("信息获取失败,无权限", "请检查wskey是否有效")
return
todayDate = GlobalVariable.final_result["todayDate"]
total_avail_point = GlobalVariable.final_result["total_avail_point"]
totalRecord = GlobalVariable.final_result["totalRecord"]
pointInfos = GlobalVariable.final_result["pointInfos"]
content = ""
point_infos = ""
bindAccount = ""
# 更新检测
if GlobalVariable.final_result.get("updates_version"):
content = content + "**JDRouterPush更新提醒:**" \
+ "\n```\n最新版:" + GlobalVariable.final_result["updates_version"] \
+ " 当前版本:" + GlobalVariable.version
if GlobalVariable.final_result.get("update_log"):
content = content + "\n" + GlobalVariable.final_result["update_log"] + "\n```"
if GlobalVariable.final_result.get("announcement"):
content = content + "\n> " + GlobalVariable.final_result["announcement"] + " \n\n"
for pointInfo in pointInfos:
mac = pointInfo["mac"]
todayPointIncome = pointInfo.get("todayPointIncome","获取失败")
allPointIncome = pointInfo.get("allPointIncome","获取失败")
amount = pointInfo.get("amount","获取失败")
bindAccount = pointInfo.get("bindAccount","获取失败")
recentExpireAmount = pointInfo.get("recentExpireAmount","获取失败")
recentExpireTime = pointInfo.get("recentExpireTime","获取失败")
satisfiedTimes = ""
if pointInfo.get("satisfiedTimes"):
satisfiedTimes = pointInfo["satisfiedTimes"]
point_infos += "\n" + "- " + GlobalVariable.device_name.get(str(mac[-6:]), GlobalVariable.device_list[mac][
"device_name"]) + "==>" \
+ "\n - 今日积分:" + str(todayPointIncome) \
+ "\n - 可用积分:" + str(amount) \
+ "\n - 总收积分:" + str(allPointIncome)
if satisfiedTimes != "":
point_infos += "\n - 累计在线:" + str(satisfiedTimes) + "天"
if pointInfo.get("runInfo"):
point_infos += "\n - 当前网速:" + pointInfo["speed"] \
+ "\n - 当前IP:" + pointInfo["wanip"] \
+ "\n - 当前模式:" + pointInfo["model"] \
+ "\n - 固件版本:" + pointInfo["rom"]
if pointInfo.get("pluginInfo"):
point_infos += "\n - 插件状态:" + pointInfo["status"] \
+ "\n - 缓存大小:" + pointInfo["cache_size"]
point_infos += "\n - 在线时间:" + pointInfo.get("onlineTime", "---") \
+ "\n - 最近到期积分:" + str(recentExpireAmount) \
+ "\n - 最近到期时间:" + recentExpireTime \
+ "\n - 最近" + str(GlobalVariable.records_num) + "条记录:"
pointRecords = pointInfo["pointRecords"]
if pointInfo.get("pointRecords") is not None:
for pointRecord in pointRecords:
recordType = pointRecord["recordType"]
recordType_str = ""
if recordType == 1:
recordType_str = "积分收入:"
else:
recordType_str = "积分支出:"
pointAmount = pointRecord["pointAmount"]
createTime = pointRecord["createTime"]
point_infos = point_infos + "\n - " + \
createTime + " " + recordType_str + str(pointAmount)
notifyContentJson = {"content": content, "date": todayDate, "total_today": today_total_point,
"avail_today": total_avail_point, "account": bindAccount, "devicesCount": totalRecord,
"detail": point_infos}
push(title,notifyContentJson)
def push(title,content):
if isinstance(content,str):
markdownContent = content
normalContent = content
else:
# mk模板
markdownContent = NoticeTemplate.markdownTemplate().format(**content)
# 普通模板
normalContent = NoticeTemplate.normalTemplate().format(**content)
NoticePush.server_push(title, markdownContent.replace("- ***", "```"))
NoticePush.push_plus(title, markdownContent)
# print("标题->", title)
# print("内容->\n", markdownContent)
NoticePush.telegram_bot(title, normalContent)
NoticePush.bark(title, normalContent)
NoticePush.enterprise_wechat(title, normalContent)
# 信息输出测试
print("标题->", title)
# print("内容->\n", normalContent)
# endregion
# 处理IP
def handleIP(wanip, ipSegment):
print("当前IP:%s ===> 期待IP:%s" % (wanip, ipSegment))
wanip_list = wanip.split(".")
ipSegment_list = ipSegment.split(".")
for wanip, ipSegment in zip(wanip_list, ipSegment_list):
if wanip == ipSegment or ipSegment == "*":
pass
else:
if "<" in ipSegment:
ip = ipSegment.split("<")[1]
if int(wanip) >= int(ip):
return False
elif ">" in ipSegment:
ip = ipSegment.split(">")[1]
if int(wanip) <= int(ip):
return False
else:
return False
return True
# ip切换
def networkSegmentSwitch():
resolveDeviceIP(GlobalVariable.NETWORK_SEGMENT)
todayPointDetail()
if GlobalVariable.final_result.get("pointInfos"):
pointInfos = GlobalVariable.final_result["pointInfos"]
for pointInfo in pointInfos:
mac = pointInfo["mac"]
wanip = pointInfo["wanip"]
if GlobalVariable.device_ip.get(str(mac[-6:])) is not None:
ipSegment = GlobalVariable.device_ip.get(str(mac[-6:]))
if handleIP(wanip, ipSegment):
print("ip段符合")
else:
print("IP段不符合")
# 重启路由器
JDServiceAPI.getControlDevice(mac, 4)
print("等待重启。。。")
time.sleep(30)
raise Exception('重新启动')
else:
raise Exception('获取IP失败')
# 主操作
def main():
if GlobalVariable.RECORDSNUM.isdigit():
GlobalVariable.records_num = int(GlobalVariable.RECORDSNUM)
resolveDeviceName(GlobalVariable.DEVICENAME)
checkForUpdates()
todayPointIncome()
pinTotalAvailPoint()
todayPointDetail()
resultDisplay()
# endregion
def runTest(i):
if i > 10:
return
try:
if GlobalVariable.WSKEY is None or GlobalVariable.WSKEY.strip() == '':
print("未获取到环境变量'WSKEY',执行中止")
return
GlobalVariable.headers["wskey"] = GlobalVariable.WSKEY
GlobalVariable.service_headers["tgt"] = GlobalVariable.WSKEY
if GlobalVariable.NETWORK_SEGMENT is None or GlobalVariable.NETWORK_SEGMENT.strip() == '':
main()
else:
hourNow = datetime.datetime.now(pytz.timezone('PRC')).hour
if hourNow < 6:
print("当前时间小于6点,执行IP切换")
networkSegmentSwitch()
else:
print("当前时间大于6点,执行信息推送")
main()
except Exception as e:
print("出现错误:", e)
print("准备重新执行...")
time.sleep(3)
runTest(++i)
# 读取配置文件
if __name__ == '__main__':
runTest(0)