feature: push all in one
This commit is contained in:
parent
7e21828f63
commit
21db1d8884
31
.github/workflows/main.yml
vendored
31
.github/workflows/main.yml
vendored
@ -6,8 +6,6 @@ on:
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
# auto merge from y1ndan/genshin-impact-helper, default: false
|
||||
ALLOW_MERGE: 'false'
|
||||
RUN_ENV: 'prod'
|
||||
TZ: 'Asia/Shanghai'
|
||||
|
||||
@ -21,16 +19,7 @@ jobs:
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
ref: master
|
||||
|
||||
- name: Auto merge
|
||||
if: ${{ env.ALLOW_MERGE != 'false' }}
|
||||
run: |
|
||||
git config --global user.name github-actions
|
||||
git config --global user.email github-actions@github.com
|
||||
git remote add upstream https://github.com/y1ndan/genshin-impact-helper
|
||||
git pull upstream master --allow-unrelated-histories
|
||||
git push origin master
|
||||
# ref: master
|
||||
|
||||
- name: Set up python
|
||||
uses: actions/setup-python@v2
|
||||
@ -42,7 +31,23 @@ jobs:
|
||||
run: sleep $(shuf -i 10-300 -n 1)
|
||||
|
||||
- name: Run sign
|
||||
env:
|
||||
COOKIE: ${{ secrets.COOKIE }}
|
||||
SCKEY: ${{ secrets.SCKEY }}
|
||||
COOL_PUSH_SKEY: ${{ secrets.COOL_PUSH_SKEY }}
|
||||
COOL_PUSH_MODE: ${{ secrets.COOL_PUSH_MODE }}
|
||||
BARK_KEY: ${{ secrets.BARK_KEY }}
|
||||
BARK_SOUND: ${{ secrets.BARK_SOUND }}
|
||||
TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }}
|
||||
TG_USER_ID: ${{ secrets.TG_USER_ID }}
|
||||
DD_BOT_TOKEN: ${{ secrets.DD_BOT_TOKEN }}
|
||||
DD_BOT_SECRET: ${{ secrets.DD_BOT_SECRET }}
|
||||
WW_BOT_KEY: ${{ secrets.WW_BOT_KEY }}
|
||||
IGOT_KEY: ${{ secrets.IGOT_KEY }}
|
||||
PUSH_PLUS_TOKEN: ${{ secrets.PUSH_PLUS_TOKEN }}
|
||||
PUSH_PLUS_USER: ${{ secrets.PUSH_PLUS_USER }}
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install -r requirements.txt
|
||||
echo '${{ secrets.COOKIE }}' | tr '#' '\n' | sed 's/$/&#${{ secrets.SCKEY }}/g' | xargs -I {} sh -c 'echo "{}" | python3 ./genshin.py'
|
||||
python3 ./genshin.py
|
||||
# echo '${{ secrets.COOKIE }}' | tr '#' '\n' | sed 's/$/&#${{ secrets.SCKEY }}/g' | xargs -I {} sh -c 'echo "{}" | python3 ./genshin.py'
|
||||
|
||||
160
genshin.py
160
genshin.py
@ -1,14 +1,22 @@
|
||||
'''
|
||||
@File : genshin.py
|
||||
@Github : https://github.com/y1ndan/genshin-impact-helper
|
||||
@Last modified by : y1ndan
|
||||
@Last modified time : 2021-01-13 11:10:30
|
||||
'''
|
||||
import hashlib
|
||||
import json
|
||||
import random
|
||||
import string
|
||||
import time
|
||||
import uuid
|
||||
import os
|
||||
|
||||
import requests
|
||||
from requests.exceptions import *
|
||||
from requests.exceptions import HTTPError
|
||||
|
||||
from settings import *
|
||||
from settings import log, CONFIG
|
||||
from notify import Notify
|
||||
|
||||
|
||||
def hexdigest(text):
|
||||
@ -20,8 +28,8 @@ def hexdigest(text):
|
||||
class Base(object):
|
||||
def __init__(self, cookies: str = None):
|
||||
if not isinstance(cookies, str):
|
||||
raise TypeError('%s want a %s but got %s' % (
|
||||
self.__class__, type(__name__), type(cookies)))
|
||||
raise TypeError('%s want a %s but got %s' %
|
||||
(self.__class__, type(__name__), type(cookies)))
|
||||
self._cookie = cookies
|
||||
|
||||
def get_header(self):
|
||||
@ -46,7 +54,8 @@ class Roles(Base):
|
||||
def get_awards(self):
|
||||
response = dict()
|
||||
try:
|
||||
content = requests.Session().get(CONFIG.AWARD_URL, headers=self.get_header()).text
|
||||
content = requests.Session().get(
|
||||
CONFIG.AWARD_URL, headers=self.get_header()).text
|
||||
response = self.to_python(content)
|
||||
except json.JSONDecodeError as e:
|
||||
log.error(e)
|
||||
@ -60,30 +69,34 @@ class Roles(Base):
|
||||
|
||||
for i in range(1, max_attempt_number):
|
||||
try:
|
||||
content = requests.Session().get(CONFIG.ROLE_URL, headers=self.get_header()).text
|
||||
content = requests.Session().get(
|
||||
CONFIG.ROLE_URL, headers=self.get_header()).text
|
||||
response = self.to_python(content)
|
||||
except HTTPError as error:
|
||||
log.error('HTTP error when get user game roles, retry %s time(s) ...' % i)
|
||||
log.error(
|
||||
'HTTP error when get game roles, retry %s time(s)...' % i)
|
||||
log.error('error is %s' % error)
|
||||
continue
|
||||
except KeyError as error:
|
||||
log.error('Wrong response to get user game roles, retry %s time(s) ...' % i)
|
||||
log.error(
|
||||
'Wrong response to get game roles, retry %s time(s)...'% i)
|
||||
log.error('response is %s' % error)
|
||||
continue
|
||||
except Exception as error:
|
||||
log.error('Unknown error %s, die' % error)
|
||||
raise error
|
||||
raise Exception(error)
|
||||
error = None
|
||||
break
|
||||
|
||||
if error:
|
||||
log.error('Maximum retry times have been reached, error is %s ' % error)
|
||||
raise error
|
||||
if response.get('retcode', 1) != 0 or response.get('data', None) is None:
|
||||
log.error(response)
|
||||
exit(-1)
|
||||
log.error(
|
||||
'Maximum retry times have been reached, error is %s ' % error)
|
||||
raise Exception(error)
|
||||
if response.get(
|
||||
'retcode', 1) != 0 or response.get('data', None) is None:
|
||||
raise Exception(response['message'])
|
||||
|
||||
log.info("账号信息获取完毕")
|
||||
log.info('账号信息获取完毕')
|
||||
return response
|
||||
|
||||
|
||||
@ -106,7 +119,8 @@ class Sign(Base):
|
||||
def get_header(self):
|
||||
header = super(Sign, self).get_header()
|
||||
header.update({
|
||||
'x-rpc-device_id': str(uuid.uuid3(uuid.NAMESPACE_URL, self._cookie)).replace('-', '').upper(),
|
||||
'x-rpc-device_id':str(uuid.uuid3(
|
||||
uuid.NAMESPACE_URL, self._cookie)).replace('-', '').upper(),
|
||||
# 1: ios
|
||||
# 2: android
|
||||
# 4: pc web
|
||||
@ -123,43 +137,45 @@ class Sign(Base):
|
||||
|
||||
# role list empty
|
||||
if not role_list:
|
||||
notify(sc_secret, '失败', user_game_roles.get('message', 'role list empty'))
|
||||
exit(-1)
|
||||
raise Exception(user_game_roles.get('message', 'Role list empty'))
|
||||
|
||||
log.info('当前账号绑定了 {} 个角色'.format(len(role_list)))
|
||||
log.info(f'当前账号绑定了 {len(role_list)} 个角色')
|
||||
info_list = []
|
||||
# cn_gf01: 天空岛
|
||||
# cn_qd01: 世界树
|
||||
self._region_list = [(i.get('region', 'NA')) for i in role_list]
|
||||
self._region_name_list = [(i.get('region_name', 'NA')) for i in role_list]
|
||||
self._region_name_list = [(i.get('region_name', 'NA'))
|
||||
for i in role_list]
|
||||
self._uid_list = [(i.get('game_uid', 'NA')) for i in role_list]
|
||||
|
||||
log.info('准备获取签到信息...')
|
||||
for i in range(len(self._uid_list)):
|
||||
info_url = CONFIG.INFO_URL.format(self._region_list[i], CONFIG.ACT_ID, self._uid_list[i])
|
||||
info_url = CONFIG.INFO_URL.format(
|
||||
self._region_list[i], CONFIG.ACT_ID, self._uid_list[i])
|
||||
try:
|
||||
content = requests.Session().get(info_url, headers=self.get_header()).text
|
||||
content = requests.Session().get(
|
||||
info_url, headers=self.get_header()).text
|
||||
info_list.append(self.to_python(content))
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
raise Exception(e)
|
||||
|
||||
if not info_list:
|
||||
log.error("user sign info list is empty, exit...")
|
||||
exit(-1)
|
||||
log.info("签到信息获取完毕")
|
||||
raise Exception('User sign info list is empty')
|
||||
log.info('签到信息获取完毕')
|
||||
return info_list
|
||||
|
||||
def run(self):
|
||||
log.info('任务开始')
|
||||
|
||||
info_list = self.get_info()
|
||||
# TODO 其实只会循环一次...
|
||||
message_list = []
|
||||
for i in range(len(info_list)):
|
||||
today = info_list[i]['data']['today']
|
||||
total_sign_day = info_list[i]['data']['total_sign_day']
|
||||
awards = Roles(self._cookie).get_awards()['data']['awards']
|
||||
uid = str(self._uid_list[i]).replace(str(self._uid_list[i])[3:6], '***', 1)
|
||||
uid = str(self._uid_list[i]).replace(
|
||||
str(self._uid_list[i])[1:8], '******', 1)
|
||||
|
||||
log.info(f'准备为旅行者 {i + 1} 号签到...')
|
||||
time.sleep(10)
|
||||
messgae = {
|
||||
'today': today,
|
||||
'region_name': self._region_name_list[i],
|
||||
@ -172,12 +188,12 @@ class Sign(Base):
|
||||
if info_list[i]['data']['is_sign'] is True:
|
||||
messgae['award_name'] = awards[total_sign_day - 1]['name']
|
||||
messgae['award_cnt'] = awards[total_sign_day - 1]['cnt']
|
||||
messgae['status'] = "👀 旅行者 {} 号, 你已经签到过了哦".format(i + 1)
|
||||
notify(sc_secret, "成功", self.message.format(**messgae))
|
||||
messgae['status'] = f'👀 旅行者 {i + 1} 号, 你已经签到过了哦'
|
||||
message_list.append(self.message.format(**messgae))
|
||||
continue
|
||||
if info_list[i]['data']['first_bind'] is True:
|
||||
messgae['status'] = "💪 旅行者 {} 号, 请先前往米游社App手动签到一次".format(i + 1)
|
||||
notify(sc_secret, "失败", self.message.format(**messgae))
|
||||
messgae['status'] = f'💪 旅行者 {i + 1} 号, 请先前往米游社App手动签到一次'
|
||||
message_list.append(self.message.format(**messgae))
|
||||
continue
|
||||
|
||||
data = {
|
||||
@ -186,10 +202,6 @@ class Sign(Base):
|
||||
'uid': self._uid_list[i]
|
||||
}
|
||||
|
||||
log.info('准备为旅行者 {} 号签到... {}'.format(i + 1, self.to_json({
|
||||
'区服': self._region_name_list[i],
|
||||
'UID': uid
|
||||
})))
|
||||
try:
|
||||
content = requests.Session().post(
|
||||
CONFIG.SIGN_URL,
|
||||
@ -197,51 +209,57 @@ class Sign(Base):
|
||||
data=json.dumps(data, ensure_ascii=False)).text
|
||||
response = self.to_python(content)
|
||||
except Exception as e:
|
||||
raise e
|
||||
raise Exception(e)
|
||||
code = response.get('retcode', 99999)
|
||||
# 0: success
|
||||
# -5003: already signed in
|
||||
if code != 0:
|
||||
notify(sc_secret, "失败", response)
|
||||
message_list.append(response)
|
||||
continue
|
||||
messgae['total_sign_day'] = total_sign_day + 1
|
||||
messgae['status'] = response['message']
|
||||
notify(sc_secret, "成功", self.message.format(**messgae))
|
||||
message_list.append(self.message.format(**messgae))
|
||||
log.info('签到完毕')
|
||||
|
||||
return ''.join(message_list)
|
||||
|
||||
@property
|
||||
def message(self):
|
||||
return CONFIG.MESSGAE_TEMPLATE
|
||||
|
||||
|
||||
def notify(secret: str, status: str, message):
|
||||
if isinstance(message, list) or isinstance(message, dict):
|
||||
message = Sign.to_json(message)
|
||||
log.info('签到{}: {}'.format(status, message))
|
||||
|
||||
if secret.startswith('SC'):
|
||||
log.info('准备推送通知...')
|
||||
url = 'https://sc.ftqq.com/{}.send'.format(secret)
|
||||
data = {'text': '原神签到小助手 签到{}'.format(status), 'desp': message}
|
||||
try:
|
||||
response = Sign.to_python(requests.Session().post(url, data=data).text)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
raise HTTPError
|
||||
else:
|
||||
errmsg = response['errmsg']
|
||||
if errmsg == 'success':
|
||||
log.info('推送成功')
|
||||
else:
|
||||
log.error('{}: {}'.format('推送失败', response))
|
||||
else:
|
||||
log.info('未配置SCKEY,正在跳过推送')
|
||||
return log.info('任务结束')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
secret = input().strip().split('#')
|
||||
secret.append('')
|
||||
cookie = secret[0]
|
||||
sc_secret = secret[1]
|
||||
log.info('任务开始')
|
||||
notify = Notify()
|
||||
msg_list = []
|
||||
ret = success_num = fail_num = 0
|
||||
# ============= miHoYo BBS COOKIE ============
|
||||
# 此处填米游社的COOKIE
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=COOKIE,Value=<获取的值>
|
||||
# 多个账号的COOKIE值之间用 # 号隔开,例如: 1#2#3#4
|
||||
COOKIE = ''
|
||||
|
||||
if os.environ.get('COOKIE', '') != '':
|
||||
COOKIE = os.environ['COOKIE']
|
||||
|
||||
cookie_list = COOKIE.split('#')
|
||||
log.info(f'检测到共配置了 {len(cookie_list)} 个帐号')
|
||||
for i in range(len(cookie_list)):
|
||||
log.info(f'准备为 NO.{i + 1} 账号签到...')
|
||||
try:
|
||||
msg = f' NO.{i + 1} 账号:{Sign(cookie_list[i]).run()}'
|
||||
msg_list.append(msg)
|
||||
success_num = success_num + 1
|
||||
except Exception as e:
|
||||
msg = f' NO.{i + 1} 账号:\n {e}'
|
||||
msg_list.append(msg)
|
||||
fail_num = fail_num + 1
|
||||
log.error(msg)
|
||||
ret = -1
|
||||
continue
|
||||
notify.send(status=f'成功: {success_num} | 失败: {fail_num}', msg=msg_list)
|
||||
if ret != 0:
|
||||
log.error('异常退出')
|
||||
exit(ret)
|
||||
log.info('任务结束')
|
||||
|
||||
Sign(cookie).run()
|
||||
|
||||
339
notify.py
Normal file
339
notify.py
Normal file
@ -0,0 +1,339 @@
|
||||
'''
|
||||
@File : notify.py
|
||||
@Github : https://github.com/y1ndan/genshin-impact-helper
|
||||
@Last modified by : y1ndan
|
||||
@Last modified time : 2021-01-13 11:01:10
|
||||
'''
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import hmac
|
||||
import hashlib
|
||||
import base64
|
||||
|
||||
import requests
|
||||
from requests.exceptions import HTTPError
|
||||
from urllib import parse
|
||||
|
||||
from settings import log
|
||||
|
||||
|
||||
class Notify(object):
|
||||
@staticmethod
|
||||
def to_python(json_str: str):
|
||||
return json.loads(json_str)
|
||||
|
||||
@staticmethod
|
||||
def to_json(obj):
|
||||
return json.dumps(obj, indent=4, ensure_ascii=False)
|
||||
|
||||
# ============================== Server Chan ==============================
|
||||
# 此处填你申请的SCKEY
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=SCKEY,Value=<获取的值>
|
||||
SCKEY = ''
|
||||
|
||||
if os.environ.get('SCKEY', '') != '':
|
||||
SCKEY = os.environ['SCKEY']
|
||||
|
||||
# ============================== Cool Push ================================
|
||||
# 此处填你申请的SKEY(详见文档: https://cp.xuthus.cc/)
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=COOL_PUSH_SKEY,Value=<获取的值>
|
||||
COOL_PUSH_SKEY = ''
|
||||
# 此处填写私聊(send)或群组(group)或者微信(wx)推送方式,默认私聊推送
|
||||
# 注: Github Actions用户若要更改,请到Settings->Secrets里设置,Name=COOL_PUSH_MODE,Value=<group或wx>
|
||||
COOL_PUSH_MODE = 'send'
|
||||
|
||||
if os.environ.get('COOL_PUSH_SKEY', '') != '':
|
||||
COOL_PUSH_SKEY = os.environ['COOL_PUSH_SKEY']
|
||||
if os.environ.get('COOL_PUSH_MODE', '') != '':
|
||||
COOL_PUSH_MODE = os.environ['COOL_PUSH_MODE']
|
||||
|
||||
# ============================== iOS Bark App =============================
|
||||
# 此处填你Bark App的信息(IP/设备码,例如: https://api.day.app/XXXXXXXX)
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=BARK_KEY,Value=<获取的值>
|
||||
BARK_KEY = ''
|
||||
# BARK App推送铃声,铃声列表去App内查看
|
||||
# 注: Github Actions用户若要更改,请到Settings->Secrets里设置,Name=BARK_SOUND,Value=<铃声名称>
|
||||
BARK_SOUND = 'healthnotification'
|
||||
|
||||
if os.environ.get('BARK_KEY', '') != '':
|
||||
if os.environ['BARK_KEY'].find(
|
||||
'https') != -1 or os.environ['BARK_KEY'].find('http') != -1:
|
||||
# 兼容BARK自建服务端用户
|
||||
BARK_KEY = os.environ['BARK_KEY']
|
||||
else:
|
||||
BARK_KEY = 'https://api.day.app/' + os.environ['BARK_KEY']
|
||||
elif os.environ.get('BARK_SOUND', '') != '':
|
||||
BARK_SOUND = os.environ['BARK_SOUND']
|
||||
elif BARK_KEY != '' or BARK_KEY.find('https') != -1 or BARK_KEY.find(
|
||||
'http') != -1:
|
||||
# 兼容BARK本地用户只填写设备码的情况
|
||||
BARK_KEY = 'https://api.day.app/' + BARK_KEY
|
||||
|
||||
# ============================== Telegram Bot =============================
|
||||
# 此处填你telegram bot的Token,例如: 1077xxx4424:AAFjv0FcqxxxxxxgEMGfi22B4yh15R5uw
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=TG_BOT_TOKEN,Value=<获取的值>
|
||||
TG_BOT_TOKEN = ''
|
||||
# 此处填你接收通知消息的telegram用户的id,例如: 129xxx206
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=TG_USER_ID,Value=<获取的值>
|
||||
TG_USER_ID = ''
|
||||
|
||||
if os.environ.get('TG_BOT_TOKEN', '') != '':
|
||||
TG_BOT_TOKEN = os.environ['TG_BOT_TOKEN']
|
||||
if os.environ.get('TG_USER_ID', '') != '':
|
||||
TG_USER_ID = os.environ['TG_USER_ID']
|
||||
|
||||
# ============================== DingTalk Bot =============================
|
||||
# 此处填你钉钉机器人的webhook,例如: 5a544165465465645d0f31dca676e7bd07415asdasd
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=DD_BOT_TOKEN,Value=<获取的值>
|
||||
DD_BOT_TOKEN = ''
|
||||
# 加签密钥,机器人安全设置页面,加签一栏下面显示的SEC开头的字符串
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=DD_BOT_SECRET,Value=<获取的值>
|
||||
DD_BOT_SECRET = ''
|
||||
|
||||
if os.environ.get('DD_BOT_TOKEN', '') != '':
|
||||
DD_BOT_TOKEN = os.environ['DD_BOT_TOKEN']
|
||||
if os.environ.get('DD_BOT_SECRET', '') != '':
|
||||
DD_BOT_SECRET = os.environ['DD_BOT_SECRET']
|
||||
|
||||
# ============================== WeChat Work Bot ==========================
|
||||
# 此处填你企业微信机器人的webhook(详见文档 https://work.weixin.qq.com/api/doc/90000/90136/91770) 例如: 693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=WW_BOT_KEY,Value=<获取的值>
|
||||
WW_BOT_KEY = ''
|
||||
|
||||
if os.environ.get('WW_BOT_KEY', '') != '':
|
||||
WW_BOT_KEY = os.environ['WW_BOT_KEY']
|
||||
|
||||
# ============================== iGot聚合推送 =================================
|
||||
# 此处填你iGot的信息(推送key,例如: https://push.hellyw.com/XXXXXXXX)
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=IGOT_KEY,Value=<获取的值>
|
||||
IGOT_KEY = ''
|
||||
|
||||
if os.environ.get('IGOT_KEY', '') != '':
|
||||
IGOT_KEY = os.environ['IGOT_KEY']
|
||||
|
||||
# ============================== push+ ====================================
|
||||
# 官方文档: https://pushplus.hxtrip.com/
|
||||
# PUSH_PLUS_TOKEN: 微信扫码登录后一对一推送或一对多推送下面的token(您的Token),不配置PUSH_PLUS_USER则默认为一对一推送
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=PUSH_PLUS_TOKEN,Value=<获取的值>
|
||||
PUSH_PLUS_TOKEN = ''
|
||||
# PUSH_PLUS_USER: 一对多推送的“群组编码”(一对多推送下面->您的群组(如无则新建)->群组编码,如果您是创建群组人。也需点击“查看二维码”扫描绑定,否则不能接受群组消息推送)
|
||||
# 注: Github Actions用户请到Settings->Secrets里设置,Name=PUSH_PLUS_USER,Value=<获取的值>
|
||||
PUSH_PLUS_USER = ''
|
||||
|
||||
if os.environ.get('PUSH_PLUS_TOKEN', '') != '':
|
||||
PUSH_PLUS_TOKEN = os.environ['PUSH_PLUS_TOKEN']
|
||||
if os.environ.get('PUSH_PLUS_USER', '') != '':
|
||||
PUSH_PLUS_USER = os.environ['PUSH_PLUS_USER']
|
||||
|
||||
def serverChan(self, text, status, desp):
|
||||
if Notify.SCKEY != '':
|
||||
url = 'https://sc.ftqq.com/{}.send'.format(Notify.SCKEY)
|
||||
data = {'text': '{} {}'.format(text, status), 'desp': desp}
|
||||
try:
|
||||
response = self.to_python(requests.post(url, data=data).text)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
raise HTTPError
|
||||
else:
|
||||
if response['errno'] == 0:
|
||||
log.info('Server酱推送成功')
|
||||
elif response['errno'] == 1024:
|
||||
# SCKEY错误或一分钟内发送相同内容
|
||||
log.error('Server酱推送失败:\n{}'.format(response['errmsg']))
|
||||
else:
|
||||
log.error('Server酱推送失败:\n{}'.format(response))
|
||||
else:
|
||||
log.info('您未配置Server酱推送所需的SCKEY,取消Server酱推送')
|
||||
pass
|
||||
|
||||
def coolPush(self, text, status, desp):
|
||||
if Notify.COOL_PUSH_SKEY != '':
|
||||
url = 'https://push.xuthus.cc/{}/{}'.format(
|
||||
Notify.COOL_PUSH_MODE, Notify.COOL_PUSH_SKEY)
|
||||
data = '{} {}\n\n{}'.format(text, status, desp).encode('utf-8')
|
||||
try:
|
||||
response = self.to_python(requests.post(url, data=data).text)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
raise HTTPError
|
||||
else:
|
||||
if response['code'] == 200:
|
||||
log.info('Cool Push推送成功')
|
||||
else:
|
||||
log.error('Cool Push推送失败:\n{}'.format(response))
|
||||
else:
|
||||
log.info('您未配置Cool Push推送所需的COOL_PUSH_SKEY,取消Cool Push推送')
|
||||
pass
|
||||
|
||||
def bark(self, text, status, desp):
|
||||
if Notify.BARK_KEY != '':
|
||||
url = '{}/{} {}/{}?sound={}'.format(Notify.BARK_KEY,
|
||||
text, status, parse.quote(desp), Notify.BARK_SOUND)
|
||||
try:
|
||||
response = self.to_python(requests.get(url).text)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
raise HTTPError
|
||||
else:
|
||||
if response['code'] == 200:
|
||||
log.info('Bark推送成功')
|
||||
elif response['code'] == 400:
|
||||
log.error('Bark推送失败:\n{}'.format(response['message']))
|
||||
else:
|
||||
log.error('Bark推送失败:\n{}'.format(response))
|
||||
else:
|
||||
log.info('您未配置Bark推送所需的BARK_KEY,取消Bark推送')
|
||||
pass
|
||||
|
||||
def tgBot(self, text, status, desp):
|
||||
if Notify.TG_BOT_TOKEN != '' or Notify.TG_USER_ID != '':
|
||||
url = 'https://api.telegram.org/bot{}/sendMessage'.format(
|
||||
Notify.TG_BOT_TOKEN)
|
||||
data = {
|
||||
'chat_id': Notify.TG_USER_ID,
|
||||
'text': '{} {}\n\n{}'.format(text, status, desp),
|
||||
'disable_web_page_preview': True
|
||||
}
|
||||
try:
|
||||
response = self.to_python(requests.post(url, data=data).text)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
raise HTTPError
|
||||
else:
|
||||
if response['ok']:
|
||||
log.info('Telegram推送成功')
|
||||
elif response['error_code'] == 400:
|
||||
log.error('请主动给bot发送一条消息并检查接收用户ID是否正确')
|
||||
elif response['error_code'] == 401:
|
||||
log.error('TG_BOT_TOKEN错误')
|
||||
else:
|
||||
log.error('Telegram推送失败:\n{}'.format(response))
|
||||
else:
|
||||
log.info('您未配置Telegram推送所需的TG_BOT_TOKEN和TG_USER_ID,取消Telegram推送')
|
||||
pass
|
||||
|
||||
def ddBot(self, text, status, desp):
|
||||
if Notify.DD_BOT_TOKEN != '':
|
||||
url = 'https://oapi.dingtalk.com/robot/send?access_token={}'.format(
|
||||
Notify.DD_BOT_TOKEN)
|
||||
data = {
|
||||
'msgtype': 'text',
|
||||
'text': {
|
||||
'content': '{} {}\n\n{}'.format(text, status, desp)
|
||||
}
|
||||
}
|
||||
if Notify.DD_BOT_SECRET != '':
|
||||
secret = Notify.DD_BOT_SECRET
|
||||
timestamp = int(round(time.time() * 1000))
|
||||
secret_enc = bytes(secret).encode('utf-8')
|
||||
string_to_sign = '{}\n{}'.format(timestamp, secret)
|
||||
string_to_sign_enc = bytes(string_to_sign).encode('utf-8')
|
||||
hmac_code = hmac.new(
|
||||
secret_enc, string_to_sign_enc,
|
||||
digestmod=hashlib.sha256).digest()
|
||||
sign = parse.quote_plus(base64.b64encode(hmac_code))
|
||||
url = 'https://oapi.dingtalk.com/robot/send?access_token={}×tamp={}&sign={}'.format(
|
||||
Notify.DD_BOT_TOKEN, timestamp, sign)
|
||||
try:
|
||||
response = self.to_python(requests.post(url, data=data).text)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
raise HTTPError
|
||||
else:
|
||||
if response['errcode'] == 0:
|
||||
log.info('钉钉推送成功')
|
||||
else:
|
||||
log.error('钉钉推送失败:\n{}'.format(response))
|
||||
else:
|
||||
log.info('您未配置钉钉推送所需的DD_BOT_TOKEN或DD_BOT_SECRET,取消钉钉推送')
|
||||
pass
|
||||
|
||||
def wwBot(self, text, status, desp):
|
||||
if Notify.WW_BOT_KEY != '':
|
||||
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}'.format(
|
||||
Notify.WW_BOT_KEY)
|
||||
data = {
|
||||
'msgtype': 'text',
|
||||
'text': {
|
||||
'content': '{} {}\n\n{}'.format(text, status, desp)
|
||||
}
|
||||
}
|
||||
try:
|
||||
response = self.to_python(requests.post(url, data=data).text)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
raise HTTPError
|
||||
else:
|
||||
if response['errcode'] == 0:
|
||||
log.info('企业微信推送成功')
|
||||
else:
|
||||
log.error('企业微信推送失败:\n{}'.format(response))
|
||||
else:
|
||||
log.info('您未配置企业微信推送所需的WW_BOT_KEY,取消企业微信推送')
|
||||
pass
|
||||
|
||||
def iGot(self, text, status, desp):
|
||||
if Notify.IGOT_KEY != '':
|
||||
url = 'https://push.hellyw.com/{}'.format(Notify.IGOT_KEY)
|
||||
data = {'title': '{} {}'.format(text, status), 'content': desp}
|
||||
try:
|
||||
response = self.to_python(requests.post(url, data=data).text)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
raise HTTPError
|
||||
else:
|
||||
if response['ret'] == 0:
|
||||
log.info('iGot推送成功')
|
||||
else:
|
||||
log.error('iGot推送失败:\n{}'.format(response))
|
||||
else:
|
||||
log.info('您未配置iGot推送所需的IGOT_KEY,取消iGot推送')
|
||||
pass
|
||||
|
||||
def pushPlus(self, text, status, desp):
|
||||
if Notify.PUSH_PLUS_TOKEN != '':
|
||||
url = 'https://pushplus.hxtrip.com/send'
|
||||
data = {
|
||||
'token': Notify.PUSH_PLUS_TOKEN,
|
||||
'title': '{} {}'.format(text, status),
|
||||
'content': desp,
|
||||
'topic': Notify.PUSH_PLUS_USER
|
||||
}
|
||||
try:
|
||||
response = self.to_python(requests.post(url, data=data).text)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
raise HTTPError
|
||||
else:
|
||||
if response['code'] == 200:
|
||||
log.info('pushplus推送成功')
|
||||
else:
|
||||
log.error('pushplus推送失败:\n{}'.format(response))
|
||||
else:
|
||||
log.info('您未配置pushplus推送所需的PUSH_PLUS_TOKEN,取消pushplus推送')
|
||||
pass
|
||||
|
||||
def send(self, **kwargs):
|
||||
app = '原神签到小助手'
|
||||
status = kwargs.get('status', '')
|
||||
msg = kwargs.get('msg', '')
|
||||
if isinstance(msg, list) or isinstance(msg, dict):
|
||||
# msg = self.to_json(msg)
|
||||
msg = '\n\n'.join(msg)
|
||||
log.info(f'签到结果: {status}\n\n{msg}')
|
||||
log.info('准备推送通知...')
|
||||
|
||||
self.serverChan(app, status, msg)
|
||||
self.coolPush(app, status, msg)
|
||||
self.bark(app, status, msg)
|
||||
self.tgBot(app, status, msg)
|
||||
self.ddBot(app, status, msg)
|
||||
self.wwBot(app, status, msg)
|
||||
self.iGot(app, status, msg)
|
||||
self.pushPlus(app, status, msg)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
Notify().send(app='原神签到小助手', status='签到状态', msg='内容详情')
|
||||
|
||||
@ -46,11 +46,12 @@ log.basicConfig(level=CONFIG.LOG_LEVEL)
|
||||
|
||||
|
||||
MESSGAE_TEMPLATE = '''
|
||||
{today:#^30}
|
||||
{today:#^28}
|
||||
🔅[{region_name}]{uid}
|
||||
今日奖励: {award_name} × {award_cnt}
|
||||
本月累签: {total_sign_day} 天
|
||||
签到结果: {status}
|
||||
{end:#^30}'''
|
||||
{end:#^28}'''
|
||||
|
||||
CONFIG.MESSGAE_TEMPLATE = MESSGAE_TEMPLATE
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user