Skip to content

Commit 9c27b03

Browse files
committed
🔧修改入参方式,新增推送接口类(干掉了一堆if)
1 parent 7b1b372 commit 9c27b03

File tree

2 files changed

+95
-57
lines changed

2 files changed

+95
-57
lines changed

.github/workflows/run.yml

+1-7
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,4 @@ jobs:
3030
pwd='${{ secrets.PWD }}'
3131
step='${{ secrets.STEP }}'
3232
33-
python3 main.py <<EOF
34-
${pmode}
35-
${pkey}
36-
${user}
37-
${pwd}
38-
${step}
39-
EOF
33+
python3 main.py ${pmode} ${pkey} ${user} ${pwd} ${step}

main.py

+94-50
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json
88
import random
99
import re
10+
import sys
1011
import time
1112

1213
import requests
@@ -187,16 +188,16 @@ def push_server(_sckey, desp=""):
187188
print(f"[{now}] 推送失败:{json_data['code']}({json_data['message']})")
188189

189190

190-
def push_pushplus(_token, content=""):
191+
def push_pushplus(token, content=""):
191192
"""
192193
推送消息到pushplus
193194
"""
194-
if _token == '':
195+
if token == '':
195196
print("[注意] 未提供token,不进行pushplus推送!")
196197
else:
197198
server_url = "http://www.pushplus.plus/send"
198199
params = {
199-
"token": _token,
200+
"token": token,
200201
"title": '小米运动 步数修改',
201202
"content": content
202203
}
@@ -210,16 +211,16 @@ def push_pushplus(_token, content=""):
210211
print(f"[{now}] 推送失败:{json_data['code']}({json_data['message']})")
211212

212213

213-
def push_tg(_token, chat_id, desp=""):
214+
def push_tg(token, chat_id, desp=""):
214215
"""
215216
推送消息到TG
216217
"""
217-
if _token == '':
218+
if token == '':
218219
print("[注意] 未提供token,不进行tg推送!")
219220
elif chat_id == '':
220221
print("[注意] 未提供chat_id,不进行tg推送!")
221222
else:
222-
server_url = f"https://api.telegram.org/bot{_token}/sendmessage"
223+
server_url = f"https://api.telegram.org/bot{token}/sendmessage"
223224
params = {
224225
"text": '小米运动 步数修改\n\n' + desp,
225226
"chat_id": chat_id
@@ -300,69 +301,112 @@ def get_message(_msg, _usr):
300301
send_message(msg, usr)
301302

302303

303-
if __name__ == "__main__":
304-
# Push Mode
305-
Pm = input()
306-
if Pm == 'wx' or Pm == 'nwx':
307-
# ServerChan
308-
sckey = input()
309-
if str(sckey) == '0':
310-
sckey = ''
311-
elif Pm == 'tg':
312-
token = input()
313-
sl = token.split('@')
314-
if len(sl) != 2:
304+
class ToPush:
305+
"""
306+
推送接口类
307+
处理pkey并转发推送消息到推送函数
308+
"""
309+
push_msg: str
310+
311+
def __init__(self, _pkey):
312+
self.pkey = _pkey
313+
314+
def to_push_wx(self):
315+
"""
316+
推送server酱接口
317+
"""
318+
if str(self.pkey) == '0':
319+
self.pkey = ''
320+
push_wx(self.pkey, self.push_msg)
321+
322+
def to_push_server(self):
323+
"""
324+
推送消息到微信接口
325+
"""
326+
if str(self.pkey) == '0':
327+
self.pkey = ''
328+
push_server(self.pkey, self.push_msg)
329+
330+
def to_push_tg(self):
331+
"""
332+
推送消息到TG接口
333+
"""
334+
try:
335+
token, chat_id = self.pkey.split('@')
336+
push_tg(token, chat_id, self.push_msg)
337+
except ValueError:
315338
print('tg推送参数有误!')
316-
elif Pm == 'qwx':
317-
token = input()
318-
sl = token.split('-')
319-
if len(sl) < 3:
339+
340+
def to_wxpush(self):
341+
"""
342+
企业微信推送接口
343+
"""
344+
try:
345+
usr, corpid, corpsecret, *agentid = self.pkey.split('-')
346+
if agentid:
347+
wxpush(self.push_msg, usr, corpid, corpsecret, int(agentid[0]))
348+
else:
349+
wxpush(self.push_msg, usr, corpid, corpsecret)
350+
except ValueError:
320351
print('企业微信推送参数有误!')
321-
elif Pm == 'pp':
322-
token = input()
323-
if token == '':
352+
353+
def to_push_pushplus(self):
354+
"""
355+
接口
356+
"""
357+
if self.pkey == '':
324358
print('pushplus token错误')
325-
elif Pm == 'off':
326-
input()
359+
else:
360+
push_pushplus(self.pkey, self.push_msg)
361+
362+
@staticmethod
363+
def no_push():
364+
"""
365+
不推送
366+
"""
327367
print('不推送')
328-
else:
329-
print('推送选项有误!')
330-
exit(0)
368+
369+
370+
if __name__ == "__main__":
371+
# Push Mode
372+
Pm = sys.argv[0]
373+
pkey = sys.argv[1]
374+
375+
to_push = ToPush(pkey)
331376

332377
# 用户名(格式为 13800138000)
333-
user = input()
378+
user = sys.argv[2]
334379
# 登录密码
335-
passwd = input()
380+
passwd = sys.argv[3]
336381
# 要修改的步数,直接输入想要修改的步数值,留空为随机步数
337-
step = input().replace('[', '').replace(']', '')
382+
step = sys.argv[4].replace('[', '').replace(']', '')
338383

339384
user_list = user.split('#')
340385
passwd_list = passwd.split('#')
341386
setp_array = step.split('-')
342387

343388
if len(user_list) == len(passwd_list):
344-
push = ''
389+
push_msg = ''
345390
for user, passwd in zip(user_list, passwd_list):
346391
if len(setp_array) == 2:
347392
step = str(random.randint(int(setp_array[0]), int(setp_array[1])))
348393
print(f"已设置为随机步数({setp_array[0]}-{setp_array[1]})")
349394
elif str(step) == '0':
350395
step = ''
351-
push += main(user, passwd, step) + '\n'
352-
if Pm == 'wx':
353-
push_wx(sckey, push)
354-
elif Pm == 'nwx':
355-
push_server(sckey, push)
356-
elif Pm == 'tg':
357-
push_tg(sl[0], sl[1], push)
358-
elif Pm == 'qwx':
359-
if len(sl) == 4:
360-
wxpush(push, sl[0], sl[1], sl[2], int(sl[3]))
361-
else:
362-
wxpush(push, sl[0], sl[1], sl[2])
363-
elif Pm == 'pp':
364-
push_pushplus(token, push)
365-
elif Pm == 'off':
366-
pass
396+
push_msg += main(user, passwd, step) + '\n'
397+
398+
push = {
399+
'wx': to_push.to_push_wx,
400+
'nwx': to_push.to_push_server,
401+
'tg': to_push.to_push_tg,
402+
'qwx': to_push.to_wxpush,
403+
'pp': to_push.to_push_pushplus,
404+
'off': to_push.no_push
405+
}
406+
try:
407+
push[Pm]()
408+
except KeyError:
409+
print('推送选项有误!')
410+
exit(0)
367411
else:
368412
print('用户名和密码数量不对')

0 commit comments

Comments
 (0)