Skip to content

Commit

Permalink
更改推送样式 (chen310#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen310 authored Mar 29, 2022
1 parent d2eebf2 commit ea44644
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 43 deletions.
2 changes: 1 addition & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
/* TG机器人token */
"botToken": "",
/* 是否将多个账号的信息合并推送 */
"merge": true
"merge": false
},
"Bark": {
/* 项目地址: https://github.com/Finb/Bark */
Expand Down
15 changes: 3 additions & 12 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,10 @@


def md2text(data):
data = re.sub(r'\n\n', r'\n', data)
data = re.sub(r'\[(.*?)\]\((.*?)\)', r'\1: \2 ', data)
data = re.sub(r'\t', r' ', data)
data = re.sub(r'\*\*(.*?)\*\*\n', r'【\1】\n', data)
data = re.sub(r'- ', r' ', data)
data = re.sub(r'#### (.*?)\n', r'【\1】\n\n', data)
data = re.sub(r'### ', r'\n', data)
data = re.sub(r'`', r'', data)
return data


def md2fullMd(data):
data = re.sub(r'\*\*(.*?)\*\*\n', r'#### \1\n', data)
data = re.sub(r'\t', r'- ', data)
return data


Expand Down Expand Up @@ -91,8 +83,7 @@ def start(event={}, context={}):
continue
data = {
'title': user.title,
'mdmsg': md2fullMd(user.msg),
'mdmsg_compat': user.msg,
'mdmsg': user.msg,
'textmsg': md2text(user.msg),
'config': push
}
Expand Down
2 changes: 1 addition & 1 deletion push/Bark.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def getKey(data):
return (config['module'], config['Bark_key'], config['Bark_url'], config['sound'], config['group'], config['icon'])


def push(title, mdmsg, mdmsg_compat, textmsg, config):
def push(title, mdmsg, textmsg, config):
msg = mdmsg
Bark_key = config['Bark_key']
Bark_url = config['Bark_url']
Expand Down
2 changes: 1 addition & 1 deletion push/CoolPush.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def getKey(data):
return (config['module'], config['Skey'], config['method'])


def push(title, mdmsg, mdmsg_compat, textmsg, config):
def push(title, mdmsg, textmsg, config):
msg = textmsg
skey = config['Skey']
method = config['method']
Expand Down
6 changes: 3 additions & 3 deletions push/Telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def getKey(data):
return (config['module'], config['userId'], config['botToken'])


def push(title, mdmsg, mdmsg_compat, textmsg, config):
def push(title, mdmsg, textmsg, config):
#deprecate str.replace() funciton since Telegram support ** syntax to bold text.
msg = mdmsg_compat # .replace('**', '*')
msg = textmsg
if len(config['userId']) == 0 or len(config['botToken']) == 0:
return
server = config['server']
Expand All @@ -21,7 +21,7 @@ def push(title, mdmsg, mdmsg_compat, textmsg, config):
else:
server = 'https://api.telegram.org'
url = server + '/bot' + config['botToken'] + '/sendMessage'
ret = requests.post(url, data={'chat_id': config['userId'], 'text': msg, 'parse_mode': "Markdown"}, headers={
ret = requests.post(url, data={'chat_id': config['userId'], 'text': msg}, headers={
'Content-Type': 'application/x-www-form-urlencoded'})
print('Telegram response: \n', ret.status_code)
if ret.status_code != 200:
Expand Down
2 changes: 1 addition & 1 deletion push/WeCom.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def get_token(corpid, corpsecret):
return None


def push(title, mdmsg, mdmsg_compat, textmsg, config):
def push(title, mdmsg, textmsg, config):
msgtype = config['msgtype']
if msgtype == 'markdown':
msg = mdmsg
Expand Down
2 changes: 1 addition & 1 deletion push/pushPlus.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def getKey(data):
return (config['module'], config['pushToken'], config['topic'], config['template'])


def push(title, mdmsg, mdmsg_compat, textmsg, config):
def push(title, mdmsg, textmsg, config):
msg = mdmsg
token = config['pushToken']
topic = config['topic']
Expand Down
2 changes: 1 addition & 1 deletion push/pushdeer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def getKey(data):
return (config['module'], config['pushkey'], config['server'])


def push(title, mdmsg, mdmsg_compat, textmsg, config):
def push(title, mdmsg, textmsg, config):
msg = mdmsg
pushkey = config['pushkey']
if len(pushkey) == 0:
Expand Down
2 changes: 1 addition & 1 deletion push/serverChan.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def getKey(data):
return (config['module'], config['KEY'])


def push(title, mdmsg, mdmsg_compat, textmsg, config):
def push(title, mdmsg, textmsg, config):
msg = mdmsg
key = config['KEY']
if len(key) == 0:
Expand Down
2 changes: 1 addition & 1 deletion push/wxpusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def getKey(data):
return (config['module'], config['APP_TOKEN'], config['UID'])


def push(title, mdmsg, mdmsg_compat, textmsg, config):
def push(title, mdmsg, textmsg, config):
data = {
"appToken": config['APP_TOKEN'],
"content": mdmsg,
Expand Down
8 changes: 4 additions & 4 deletions pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Pusher():
def __init__(self):
self.datas = {}
self.separator = '-------------------------\n'
self.separator = '-----------------------------------\n\n'

def append(self, data):
config = data['config']
Expand All @@ -17,21 +17,21 @@ def append(self, data):
return
# 是否合并推送
if not config['merge']:
exec('{}.push(data["title"], data["mdmsg"], data["mdmsg_compat"], data["textmsg"], config)'.format(
exec('{}.push(data["title"], data["mdmsg"], data["textmsg"], config)'.format(
config['module']))
return

# 配置相同才会合并推送
key = eval('{}.getKey(data)'.format(config['module']))
if key is not None:
if key in self.datas:
for syntax in ['mdmsg', 'mdmsg_compat', 'textmsg']:
for syntax in ['mdmsg', 'textmsg']:
self.datas[key][syntax] += self.separator
self.datas[key][syntax] += data[syntax]
else:
self.datas[key] = data

def push(self):
for data in self.datas.values():
exec('{}.push(data["title"], data["mdmsg"], data["mdmsg_compat"], data["textmsg"], data["config"])'.format(
exec('{}.push(data["title"], data["mdmsg"], data["textmsg"].strip(), data["config"])'.format(
data['config']['module']))
29 changes: 13 additions & 16 deletions user.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,16 @@ def login_check(self, username, pwd='', cookie='', countrycode='', ip=''):
return music

def taskTitle(self, title):
msg = '**{}**\n'.format(title)
self.msg += msg + '\n'
print(msg)
def taskInfo(self, key, value='', useCodeblock = True):
self.msg += '#### ' + title + '\n'
print('#### ' + title)

def taskInfo(self, key, value=''):
if value == '':
msg = f"\t{str(key)}"
elif useCodeblock:
# Use `codeblock` to prevent markdown 's keywords containing in value which leads to 400 Bad Request
msg = f"\t{str(key)}: `{str(value)}`"
self.msg += '- ' + str(key) + '\n'
print('- ' + str(key))
else:
msg = f"\t{str(key)}: {str(value)}"
self.msg += msg + '\n'
print(msg)
self.msg += '- ' + str(key) + ': ' + str(value) + '\n'
print('- ' + str(key) + ': ' + str(value))


def finishTask(self):
Expand Down Expand Up @@ -330,7 +327,7 @@ def auto_daka(self):
self.taskInfo('听歌总数', str(resp['listenSongs']) + '首')
if resp['listenSongs'] - self.songnumber < 300:
self.taskInfo(
'温馨提示', '数据更新可能有延时,[点击查看最新数据](https://music.163.com/#/user/home?id='+str(self.uid)+')', useCodeblock=False)
'温馨提示', '数据更新可能有延时,[点击查看最新数据](https://music.163.com/#/user/home?id='+str(self.uid)+')')
return
else:
total = 300 - (resp['listenSongs'] - self.songnumber)
Expand All @@ -351,7 +348,7 @@ def auto_daka(self):
self.taskInfo('听歌总数', str(resp['listenSongs']) + '首')
if resp['listenSongs'] - self.songnumber < 300:
self.taskInfo(
'温馨提示', '数据更新可能有延时,[点击查看最新数据](https://music.163.com/#/user/home?id='+str(self.uid)+')', useCodeblock=False)
'温馨提示', '数据更新可能有延时,[点击查看最新数据](https://music.163.com/#/user/home?id='+str(self.uid)+')')
self.finishTask()

def daka(self):
Expand Down Expand Up @@ -415,7 +412,7 @@ def daka(self):
self.taskInfo('听歌总数', str(resp['listenSongs']) + '首')
if resp['listenSongs'] - self.listenSongs < 300:
self.taskInfo(
'温馨提示', '数据更新可能有延时,[点击查看最新数据](https://music.163.com/#/user/home?id='+str(self.uid)+')', useCodeblock=False)
'温馨提示', '数据更新可能有延时,[点击查看最新数据](https://music.163.com/#/user/home?id='+str(self.uid)+')')
return

time.sleep(user_setting['daka']['sleep_time'] + 5)
Expand All @@ -428,7 +425,7 @@ def daka(self):
self.taskInfo('听歌总数', str(resp['listenSongs']) + '首')
if resp['listenSongs'] - self.listenSongs < 300:
self.taskInfo(
'温馨提示', '数据更新可能有延时,[点击查看最新数据](https://music.163.com/#/user/home?id='+str(self.uid)+')', useCodeblock=False)
'温馨提示', '数据更新可能有延时,[点击查看最新数据](https://music.163.com/#/user/home?id='+str(self.uid)+')')
self.finishTask()

def play_playlists(self):
Expand Down Expand Up @@ -530,7 +527,7 @@ def follow(self):
self.taskInfo('感谢关注', author_nickname)
# self.taskInfo('如果不想关注,请在配置文件里修改,并在官方客户端里取消关注')
self.taskInfo(
'如果不想关注,请在配置文件里修改,并在[主页](https://music.163.com/#/user/home?id='+str(author_uid)+')里取消关注', useCodeblock=False)
'如果不想关注,请在配置文件里修改,并在[主页](https://music.163.com/#/user/home?id='+str(author_uid)+')里取消关注')
self.finishTask()

def sign(self):
Expand Down

0 comments on commit ea44644

Please sign in to comment.