Skip to content

Commit

Permalink
feat: 增加推送方式 pushdeer (chen310#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen310 authored Mar 18, 2022
1 parent 2d88bc5 commit fa1c3df
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
5. 音乐人自动签到领取云豆
6. 音乐人自动完成任务,并领取云豆
7. 自动领取 vip 成长值
8. 多种推送方式
8. 多种[推送方式](#推送)
9. 支持多账号
10. 支持[腾讯云函数](#一部署到腾讯云函数) & [青龙面板](#二部署到青龙面板) & [本地运行](#三本地运行) & [docker 部署](#四使用docker部署)

Expand Down Expand Up @@ -394,6 +394,7 @@ cookie 获取方式:首先在网页登录[网易云音乐](https://music.163.c
4. [pushPlus](https://www.pushplus.plus)
5. Telegram
6. [Bark](https://github.com/Finb/Bark)
7. [pushdeer](https://github.com/easychen/pushdeer)

要使用推送的话将相应的 `enable` 设为 `true`,并填写配置

Expand Down Expand Up @@ -500,6 +501,24 @@ cookie 获取方式:首先在网页登录[网易云音乐](https://music.163.c

要使用 Bark 的话需要填写 `Bark_url``Bark_key`。可以使用 Bark 官方 API 或者自行搭建。

##### pushdeer

```json5
"pushdeer": {
"module": "pushdeer",
/* 是否启用推送 */
"enable": false,
/* 服务器地址,放空则使用官方服务器: https://api2.pushdeer.com */
"server": "",
/* pushkey */
"pushkey": "",
/* 是否将多个账号的信息合并推送 */
"merge": false
}
```

要使用 pushdeer 的话需要填写 `pushkey`。如果使用自己搭建的服务器,请填写 `server`

#### 刷单曲播放量

```json5
Expand Down
12 changes: 12 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@
"Bark_key": "",
/* 是否将多个账号的信息合并推送, 建议为false,iOS推送消息过长可能会失败 */
"merge": false
},
/* https://github.com/easychen/pushdeer */
"pushdeer": {
"module": "pushdeer",
/* 是否启用推送 */
"enable": false,
/* 服务器地址,放空则使用官方服务器: https://api2.pushdeer.com */
"server": "",
/* pushkey */
"pushkey": "",
/* 是否将多个账号的信息合并推送 */
"merge": false
}
},
/* 签到 */
Expand Down
94 changes: 94 additions & 0 deletions push/pushdeer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import json
from typing import Optional, Union
import requests


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


def push(title, mdmsg, mdmsg_compat, textmsg, config):
msg = mdmsg
pushkey = config['pushkey']
if len(pushkey) == 0:
return
if config['server']:
pushdeer = PushDeer(server=config['server'], pushkey=pushkey)
else:
pushdeer = PushDeer(pushkey=pushkey)
pushdeer.send_markdown(title, desp=msg)

# From https://github.com/gaoliang/pypushdeer
class PushDeer:
server = "https://api2.pushdeer.com"
endpoint = "/message/push"
pushkey = None

def __init__(self, server: Optional[str] = None, pushkey: Optional[str] = None):
if server:
self.server = server
if pushkey:
self.pushkey = pushkey

def _push(self, text: str, desp: Optional[str] = None, server: Optional[str] = None,
pushkey: Optional[str] = None, text_type: Optional[str] = None):

if not pushkey and not self.pushkey:
raise ValueError("pushkey must be specified")

res = self._send_push_request(desp, pushkey or self.pushkey, server or self.server, text, text_type)
if res["content"]["result"]:
result = json.loads(res["content"]["result"][0])
if result["success"] == "ok":
return True
else:
return False
else:
return False

def _send_push_request(self, desp, key, server, text, type):
return requests.get(server + self.endpoint, params={
"pushkey": key,
"text": text,
"type": type,
"desp": desp,
}).json()

def send_text(self, text: str, desp: Optional[str] = None, server: Optional[str] = None,
pushkey: Union[str, list, None] = None):
"""
Any text are accepted when type is text.
@param text: message : text
@param desp: the second part of the message (optional)
@param server: server base
@param pushkey: pushDeer pushkey
@return: success or not
"""
return self._push(text=text, desp=desp, server=server, pushkey=pushkey, text_type='text')

def send_markdown(self, text: str, desp: Optional[str] = None, server: Optional[str] = None,
pushkey: Union[str, list, None] = None):
"""
Text in Markdown format are accepted when type is markdown.
@param text: message : text in markdown
@param desp: the second part of the message in markdown (optional)
@param server: server base
@param pushkey: pushDeer pushkey
@return: success or not
"""
return self._push(text=text, desp=desp, server=server, pushkey=pushkey, text_type='markdown')

def send_image(self, image_src: str, desp: Optional[str] = None, server: Optional[str] = None,
pushkey: Union[str, list, None] = None):
"""
Only image src are accepted by API now, when type is image.
@param image_src: message : image URL
@param desp: the second part of the message (optional)
@param server: server base
@param pushkey: pushDeer pushkey
@return: success or not
"""
return self._push(text=image_src, desp=desp, server=server, pushkey=pushkey, text_type='image')

0 comments on commit fa1c3df

Please sign in to comment.