Skip to content

Commit

Permalink
refactor: 推送
Browse files Browse the repository at this point in the history
remove: API 网关触发
  • Loading branch information
chen310 committed Jan 19, 2022
1 parent 89cbb81 commit d466956
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 257 deletions.
122 changes: 66 additions & 56 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,61 +26,71 @@
/* 多个账号删除上面的注释,以此类推 */
],
"setting": {
/* 使用API网关触发时停止消息推送 */
"stopPushOnAPIGateway": true,
/* server酱微信推送 */
"serverChan": {
/* 是否启用server酱微信推送 */
"enable": true,
/* 填写server酱旧版SCKEY或新版SendKey */
"KEY": ""
},
/* pushPlus微信推送 */
/* 官网 http://www.pushplus.plus */
"pushPlus": {
/* 是否启用pushplus微信推送 */
"enable": true,
/* pushplus的token */
"pushToken": "",
/* 群组编码,为空时发给自己 */
"topic": ""
},
/* Cool Push酷推QQ推送 */
"CoolPush": {
/* 是否启用酷推推送 */
"enable": true,
/* 推送方式,如果想要使用多种方式,请用逗号隔开,如["send","group"] */
/* send:QQ号私人推送, group:QQ群推送, wx:微信推送, email:邮件推送 */
"method": [
"send"
],
/* 酷推Skey */
"Skey": ""
},
/* 企业微信推送 */
"WeCom": {
/* 是否启用企业微信推送 */
"enable": true,
/* 企业ID,登录企业微信后在管理后台“我的企业”-“企业信息”下查看 */
"corpid": "",
/* 应用ID,在“应用管理”里,点进相应的应用可查看 */
"agentid": "",
/* 应用密钥,在“应用管理”里,点进相应的应用可查看 */
"secret": "",
/* 要推送的人的用户ID,每个成员都有唯一的userid,即所谓“帐号”。在管理后台->“通讯录”->点进某个成员的详情页,可以看到。默认为"@all",向该企业应用的全部成员发送 */
/* 若该企业应用有多个成员,请将id改为自己的id,如“ZhangSan”,以免其他成员也收到消息 */
"userid": "@all",
/* 消息类型:text 文本消息(微信、企业微信里均可查看) | textcard 文本卡片消息(微信、企业微信里均可查看) | markdown markdown消息(只能在企业微信里查看) */
"msgtype": "text"
},
/* Telegram推送 */
"Telegram": {
/* 是否启用Telegram推送 */
"enable": true,
/* Telegram账号ID */
"userId": "",
/* TG机器人token */
"botToken": ""
/* 推送 */
"push": {
"serverChan": {
"module": "serverChan",
/* 是否启用server酱微信推送 */
"enable": true,
/* 填写server酱旧版SCKEY或新版SendKey */
"KEY": "",
/* 是否将多个账号的信息合并推送 */
"merge": true
},
/* 官网 http://www.pushplus.plus */
"pushPlus": {
"module": "pushPlus",
/* 是否启用pushplus微信推送 */
"enable": true,
/* pushplus的token */
"pushToken": "",
/* 消息模板: markdown | html | txt | json */
"template": "markdown",
/* 群组编码,为空时发给自己 */
"topic": "",
/* 是否将多个账号的信息合并推送 */
"merge": true
},
"CoolPush": {
"module": "CoolPush",
/* 是否启用酷推推送 */
"enable": true,
/* 推送方式: send QQ号私人推送 | group QQ群推送 | wx 微信推送 | email 邮件推送 */
"method": "send",
/* 酷推Skey */
"Skey": "",
/* 是否将多个账号的信息合并推送 */
"merge": true
},
"WeCom": {
"module": "WeCom",
/* 是否启用企业微信推送 */
"enable": true,
/* 企业ID,登录企业微信后在管理后台“我的企业”-“企业信息”下查看 */
"corpid": "",
/* 应用ID,在“应用管理”里,点进相应的应用可查看 */
"agentid": "",
/* 应用密钥,在“应用管理”里,点进相应的应用可查看 */
"secret": "",
/* 要推送的人的用户ID,每个成员都有唯一的userid,即所谓“帐号”。在管理后台->“通讯录”->点进某个成员的详情页,可以看到。默认为"@all",向该企业应用的全部成员发送 */
/* 若该企业应用有多个成员,请将id改为自己的id,如“ZhangSan”,以免其他成员也收到消息 */
"userid": "@all",
/* 消息类型:text 文本消息(微信、企业微信里均可查看) | textcard 文本卡片消息(微信、企业微信里均可查看) | markdown markdown消息(只能在企业微信里查看) */
"msgtype": "text",
/* 是否将多个账号的信息合并推送 */
"merge": false
},
"Telegram": {
"module": "Telegram",
/* 是否启用Telegram推送 */
"enable": true,
/* Telegram账号ID */
"userId": "",
/* TG机器人token */
"botToken": "",
/* 是否将多个账号的信息合并推送 */
"merge": true
}
},
/* 签到 */
"sign": true,
Expand Down Expand Up @@ -246,4 +256,4 @@
/* 在网易云音乐中关注作者 */
"follow": true
}
}
}
152 changes: 23 additions & 129 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import re
import os
from user import User
from wecom import WeComAlert
from pusher import Pusher


def md2text(data):
Expand Down Expand Up @@ -37,32 +37,26 @@ def getSongNumber():


def start(event={}, context={}):
with open('./config.json', 'r', encoding='utf-8') as f:
with open('config.json', 'r', encoding='utf-8') as f:
config = json5.load(f)
setting = config['setting']

user_count = 0

SCKEYs = {}
Skeys = {}
pushToken = {}
tgkeys = {}
# 公共配置
setting = config['setting']

res = ''
songnumber = getSongNumber()
# 推送
pusher = Pusher()
for user_config in config['users']:
if user_config['enable'] == False:
if not user_config['enable']:
continue
user_count += 1

# 获取账号配置
if "setting" in user_config:
user_setting = updateConfig(user_config["setting"], setting)
else:
user_setting = setting

user = User()
user.setUser(username=user_config['username'], password=user_config['password'], countrycode=user_config.get(
'countrycode', ''), user_setting=user_setting, No=user_count, ip=user_config['X-Real-IP'])
user.setUser(user_config, user_setting)
if user.isLogined:
user.songnumber = songnumber.get(str(user.uid), -1)
user.userInfo()
Expand Down Expand Up @@ -97,107 +91,17 @@ def start(event={}, context={}):
if user_setting['other']['play_playlists']['enable']:
user.play_playlists()

res += user.msg
# user.msg = user.msg.strip()

# API网关访问
if 'requestContext' in event and user_setting.get('noPushOnAPIGateway', True):
continue

sckey = user_setting['serverChan']['KEY']
if user_setting['serverChan']['enable'] and len(sckey) > 0:
if sckey in SCKEYs:
SCKEYs[sckey]['msg'] += user.msg
else:
SCKEYs[sckey] = {'title': user.title, 'msg': user.msg}


telegram_userId = user_setting['Telegram']['userId']
bot_token = user_setting['Telegram']['botToken']
if user_setting['Telegram']['enable'] and len(telegram_userId) > 0 and len(bot_token) > 0:
if bot_token in tgkeys:
tgkeys[bot_token]['msg'] += user.msg
else:
tgkeys[bot_token] = {'msg': user.msg}

skey = user_setting['CoolPush']['Skey']
if user_setting['CoolPush']['enable'] and len(skey) > 0:
if skey in Skeys:
Skeys[skey]['msg'] += user.msg
else:
Skeys[skey] = {
'title': user.title, 'method': user_setting['CoolPush']['method'], 'msg': user.msg}

pushtoken = user_setting['pushPlus']['pushToken']
if user_setting['pushPlus']['enable'] and len(pushtoken) > 0:
# 一对多单独发送
if len(user_setting['pushPlus']['topic']) > 0:
push_url = 'http://www.pushplus.plus/send'
data = {
"token": pushtoken,
"title": user.title,
"content": user.msg,
"topic": user_setting['pushPlus']['topic']
}
body = json.dumps(data).encode(encoding='utf-8')
headers = {'Content-Type': 'application/json'}
requests.post(push_url, data=body, headers=headers)
else:
if pushtoken in pushToken:
pushToken[pushtoken]['msg'] += user.msg
else:
pushToken[pushtoken] = {
'title': user.title, 'msg': user.msg}

if user_setting['WeCom']['enable'] and len(user_setting['WeCom']['corpid']) > 0 and len(user_setting['WeCom']['secret']) > 0 and len(user_setting['WeCom']['agentid']) > 0:
alert = WeComAlert(
user_setting['WeCom']['corpid'], user_setting['WeCom']['secret'], user_setting['WeCom']['agentid'])
msg = user.msg
if user_setting['WeCom']['msgtype'] != 'markdown':
msg = md2text(msg)
alert.send_msg(user_setting['WeCom']['userid'], user_setting['WeCom']['msgtype'],
msg, "网易云音乐打卡", 'https://music.163.com/#/user/home?id='+str(user.uid))

if 'requestContext' in event and user_setting.get('noPushOnAPIGateway', True):
return res

for sckey in SCKEYs:
serverChan_url = ''
if sckey.startswith('SCT'):
serverChan_url = 'https://sctapi.ftqq.com/'+sckey+'.send'
else:
serverChan_url = 'https://sc.ftqq.com/'+sckey+'.send'
requests.post(serverChan_url, data={
"text": SCKEYs[sckey]['title'], "desp": SCKEYs[sckey]['msg']})

for tgkey in tgkeys:
push_url = 'https://api.telegram.org/bot' + tgkey + '/sendMessage'
requests.post(push_url, data={
'chat_id': telegram_userId, 'text': tgkeys[tgkey]['msg']}, headers = {'Content-Type': 'application/x-www-form-urlencoded'})

for skey in Skeys:
for method in Skeys[skey]['method']:
CoolPush_url = "https://push.xuthus.cc/{}/{}".format(method, skey)
if method == "email":
requests.post(CoolPush_url, data={
"t": Skeys[skey]['title'], "c": md2text(Skeys[skey]['msg'])})
else:
requests.get(CoolPush_url, params={"c": Skeys[skey]['msg']})

# Pushplus推送
for pushtoken in pushToken:
push_url = 'http://www.pushplus.plus/send'
data = {
"token": pushtoken,
"title": pushToken[pushtoken]['title'],
"content": pushToken[pushtoken]['msg']
}
body = json.dumps(data).encode(encoding='utf-8')
headers = {'Content-Type': 'application/json'}
requests.post(push_url, data=body, headers=headers)
if user_count == 0:
print('没有待运行的账号')
return res

for push in user_setting['push'].values():
if not push['enable']:
continue
data = {
'title': user.title,
'msg': user.msg,
'config': push
}
pusher.append(data)
pusher.push()


def setSongNumber():
Expand All @@ -217,8 +121,7 @@ def setSongNumber():
continue

user = User()
user.setUser(username=user_config['username'], password=user_config['password'], countrycode=user_config.get(
'countrycode', ''), user_setting=user_setting, No=0, ip=user_config['X-Real-IP'])
user.setUser(user_config, user_setting)
if user.isLogined:
resp = user.music.user_detail(user.uid)
if user_setting['daka']['full_stop'] == True and (resp['level'] == 10 or resp['listenSongs'] >= 20000):
Expand Down Expand Up @@ -278,19 +181,10 @@ def setSongNumber():


def main_handler(event, context):
if event.get("Type", "") == "Timer" and event.get("Message", "") == "set_song_number":
if event.get("Type") == "Timer" and event.get("TriggerName") == "timer-songnumber":
setSongNumber()
return
res = start(event, context)
if 'requestContext' in event:
data = {
"isBase64Encoded": False,
"statusCode": 200,
"headers": {"Content-Type": "text/html"},
"body": "<html lang=\"zh-CN\"><meta charset=\"utf-8\"><body><p>" + "<br />" + md2text(res).replace("\n", "<br />") + "</p></body></html>"
}
return data

start(event, context)

if __name__ == '__main__':
start()
22 changes: 22 additions & 0 deletions push/CoolPush.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
import requests


def getKey(data):
config = data['config']
if len(config['Skey']) == 0:
return None
return (config['module'], config['Skey'], config['method'])


def push(title, msg, config):
skey = config['Skey']
method = config['method']
if len(skey) == 0 or len(method) == 0:
return

CoolPush_url = "https://push.xuthus.cc/{}/{}".format(method, skey)
if method == "email":
requests.post(CoolPush_url, data={"t": title, "c": msg})
else:
requests.get(CoolPush_url, params={"c": msg})
19 changes: 19 additions & 0 deletions push/Telegram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
import requests
import json


def getKey(data):
config = data['config']
if len(config['userId']) == 0 or len(config['botToken']):
return None
return (config['module'], config['userId'], config['botToken'])


def push(title, msg, config):
if len(config['userId']) == 0 or len(config['botToken']):
return

url = 'https://api.telegram.org/bot' + config['botToken'] + '/sendMessage'
requests.post(url, data={'chat_id': config['userId'], 'text': msg}, headers={
'Content-Type': 'application/x-www-form-urlencoded'})
Loading

0 comments on commit d466956

Please sign in to comment.