-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from LeslieLeung/main
Add: support Bark sender. #8
- Loading branch information
Showing
7 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "普通人搞一百万有多难?", | ||
}, | ||
}, | ||
}, | ||
) |