Skip to content

Commit

Permalink
Merge pull request #27 from LeslieLeung/main
Browse files Browse the repository at this point in the history
Add: support Bark sender. #8
  • Loading branch information
howie6879 authored Jan 1, 2022
2 parents 015040f + 902a4b8 commit 12ac2e8
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ LL_WECOM_PARTY=""
LL_TG_CHAT_ID=""
LL_TG_TOKEN=""
LL_PROXY=""
LL_WECHAT_ACCOUNT="是不是很酷;老胡的储物柜"
LL_WECHAT_ACCOUNT="是不是很酷;老胡的储物柜"
LL_BARK_URL=""
22 changes: 22 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Release
on:
push:
tags:
- 'v*'
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@master
with:
name: liuliio/schedule
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
tag_names: true
dockerfile: schedule.Dockerfile
buildoptions: "--no-cache=true"
2 changes: 2 additions & 0 deletions docs/02.环境变量.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ LL_WECOM_SECRET=""
LL_WECOM_TO_USER=""
# 企业微信分发部门(填写部门名称),多个部门用;分割
LL_WECOM_PARTY=""
# Bark推送链接
LL_BARK_URL=""
```

15 changes: 14 additions & 1 deletion docs/03.分发器配置.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,17 @@ DD_TOKEN = os.getenv('LL_D_TOKEN', '1dea61224e683d90c5d3694c89e30841681567747f41
LL_TG_CHAT_ID = "1222431510"
# 你TelegramBot的Token
LL_TG_TOKEN = "5065136980:BBF4uhUcGLP1-6qOHBfIT_0EArbXjek91GY"
```
```

## Bark
仅介绍Bark客户端的使用,要自行部署Bark服务端,请参考 [bark-server](https://github.com/Finb/bark-server)

### 获得推送url

打开APP,复制URL

<img src="https://wx4.sinaimg.cn/mw2000/003rYfqply1grd1meqrvcj60bi08zt9i02.jpg" width=365 />

### 设置对应环境变量

修改.env文件设置环境变量`LL_BARK_URL`
File renamed without changes.
2 changes: 2 additions & 0 deletions src/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ class Config:
# TG分发器参数配置
TG_CHAT_ID = os.getenv("LL_TG_CHAT_ID", "")
TG_TOKEN = os.getenv("LL_TG_TOKEN", "")
# Bark 分发器参数配置
BARK_URL = os.getenv("LL_BARK_URL", "")
132 changes: 132 additions & 0 deletions src/sender/bark_sender.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/usr/bin/env python
"""
Created by Leslie Leung at 2021/12/28.
Description:分发到 Bark 终端
Changelog: all notable changes to this file will be documented
"""
import json
import time

import requests

from src.config import Config
from src.sender.base import SenderBase
from src.utils import LOGGER


class BarkSender(SenderBase):
"""
Bark分发类
"""

def __init__(self, send_config: dict):
super(BarkSender, self).__init__(send_type="bark", send_config=send_config)
bark_url = send_config.get("bark_url", Config.BARK_URL)
self.url = bark_url[:-1] if bark_url.endswith("/") else bark_url

def send(self, send_data) -> bool:
"""
下发到Bark终端
:param send_data: 下发内容字典,字段开发者自定义
:return:
"""
doc_name = send_data["doc_name"]
doc_source = send_data["doc_source"]
doc_link = send_data["doc_link"]
# doc_content = send_data["doc_content"]
doc_cus_des = send_data["doc_cus_des"]
doc_source_name = send_data["doc_source_name"]
doc_keywords = send_data["doc_keywords"]
doc_date = send_data["doc_date"]
doc_id = send_data["doc_id"]
is_send = self.is_send(doc_id=doc_id)
send_status = True
notice_msg = f"{doc_cus_des}👉{doc_source_name}_{doc_name}{doc_link} 分发到 {self.send_type}"
if not is_send:
url = self.compose(send_data)
resp = requests.post(url)
if resp.status_code == 200 and json.loads(resp.text)["code"] == 200:
# 将状态持久化到数据库
self.sl_coll.insert_one(
{
"send_type": self.send_type,
"doc_id": doc_id,
"ts": time.time(),
}
)
# 下发成功
LOGGER.info(f"{notice_msg} 成功!")
send_status = True
else:
errmsg = json.loads(resp.text)["code"]
LOGGER.error(f"{notice_msg} 失败:{errmsg}")

else:
LOGGER.error(f"{notice_msg} 失败!")
return send_status

def compose(self, send_data) -> str:
"""
根据发送数据产生Bark请求url
:param send_data: 下发内容字典,字段开发者自定义
:return:
"""
doc_name = send_data["doc_name"]
doc_source = send_data["doc_source"]
doc_link = send_data["doc_link"]
# doc_content = send_data["doc_content"]
doc_cus_des = send_data["doc_cus_des"]
doc_source_name = send_data["doc_source_name"]
doc_keywords = send_data["doc_keywords"]
doc_date = send_data["doc_date"]
title = f"[{doc_source_name}]{doc_name}".replace("/", "")
body = f"{doc_date} | {doc_cus_des}\n亲,来自 {doc_source} 源的 {doc_source_name} 有更新啦! \n\n文章关键字:{doc_keywords}\n来自[2c]👉技术支持❤"
copy = f"?copy={doc_link}"
return f"{self.url}/{title}/{body}{copy}"


def send(send_config: dict, send_data: dict) -> bool:
"""
下方到Bark终端
:param send_config: 下发终端配置
:param send_data: 下发内容字典,字段开发者自定义
:return:
"""
return BarkSender(send_config=send_config).send(send_data)


if __name__ == "__main__":
send(
send_config={
"wecom_id": "",
"wecom_agent_id": 0,
"wecom_secret": "",
"wecom_party_list": [],
"wecom_to_user": "",
},
send_data={
"doc_id": "f42460107f69c9e929f8d591243efeb2",
"doc_content": "普通人搞一百万有多难?",
"doc_date": "2021-04-11",
"doc_des": "",
"doc_ext": {},
"doc_link": "https://mp.weixin.qq.com/s/J9Ejaw9x9fXDZ4-hsrhhtw",
"doc_name": "普通人搞一百万有多难?",
"doc_source": "wechat",
"doc_source_des": "前码农&产品人,现自由职业者,创业者。",
"doc_source_name": "stormzhang",
"doc_cus_des": "广告",
"doc_keywords": [],
"doc_ts": 1618136819.0,
"cos_model": {
"model_name": "cos",
"result": 0,
"probability": 0.0,
"feature_dict": {
"is_black": False,
"is_white": False,
"text": "普通人搞一百万有多难?",
},
},
},
)

0 comments on commit 12ac2e8

Please sign in to comment.