From 81c9e5410a6d358ffbdb5328d0dab058f58ccf2e Mon Sep 17 00:00:00 2001 From: LeslieLeung Date: Tue, 28 Dec 2021 20:53:30 +0800 Subject: [PATCH 1/5] feat: Add support for Bark distributor --- src/config/config.py | 2 + src/sender/bark_sender.py | 105 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/sender/bark_sender.py diff --git a/src/config/config.py b/src/config/config.py index afbe57a..8689298 100644 --- a/src/config/config.py +++ b/src/config/config.py @@ -67,6 +67,8 @@ class Config: WECOM_PARTY_LIST = os.getenv("CC_WECOM_PARTY", "").split(";") # 企业微信分发用户,多个用户用;分割 WECOM_TO_USER = os.getenv("CC_WECOM_TO_USER", "").replace(";", "|") + # Bark URL + BARK_URL = os.getenv("BARK_URL", "") # 订阅的公众号配置 WECHAT_LIST = os.getenv( "CC_WECHAT_ACCOUNT", diff --git a/src/sender/bark_sender.py b/src/sender/bark_sender.py new file mode 100644 index 0000000..49cd9d4 --- /dev/null +++ b/src/sender/bark_sender.py @@ -0,0 +1,105 @@ +# -*- coding:utf-8 -*- +from src.config import Config +from src.sender.base import SenderBase +from src.utils import LOGGER +import requests, json, time, os + + +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: + 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: + 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: + 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": "普通人搞一百万有多难?", + }, + }, + }, + ) \ No newline at end of file From 8c035298dedad55975117b64ca6a9bbbf4a1a282 Mon Sep 17 00:00:00 2001 From: LeslieLeung Date: Wed, 29 Dec 2021 22:38:22 +0800 Subject: [PATCH 2/5] feat: Add support for Bark distributor --- .env | 3 +- ...57\345\242\203\345\217\230\351\207\217.md" | 2 ++ ...21\345\231\250\351\205\215\347\275\256.md" | 15 +++++++- src/config/config.py | 2 +- src/sender/bark_sender.py | 35 ++++++++++++++++--- 5 files changed, 50 insertions(+), 7 deletions(-) diff --git a/.env b/.env index 78a2504..111f83e 100644 --- a/.env +++ b/.env @@ -19,4 +19,5 @@ CC_WECOM_PARTY="" CC_TG_CHAT_ID="" CC_TG_TOKEN="" CC_PROXY="" -CC_WECHAT_ACCOUNT="是不是很酷;老胡的储物柜" \ No newline at end of file +CC_WECHAT_ACCOUNT="是不是很酷;老胡的储物柜" +LL_BARK_URL="" \ No newline at end of file diff --git "a/docs/02.2C\347\216\257\345\242\203\345\217\230\351\207\217.md" "b/docs/02.2C\347\216\257\345\242\203\345\217\230\351\207\217.md" index f8c8bab..69d961f 100644 --- "a/docs/02.2C\347\216\257\345\242\203\345\217\230\351\207\217.md" +++ "b/docs/02.2C\347\216\257\345\242\203\345\217\230\351\207\217.md" @@ -48,5 +48,7 @@ CC_WECOM_SECRET="" CC_WECOM_TO_USER="" # 企业微信分发部门(填写部门名称),多个部门用;分割 CC_WECOM_PARTY="" +# Bark推送链接 +LL_BARK_URL="" ``` diff --git "a/docs/03.2C\345\210\206\345\217\221\345\231\250\351\205\215\347\275\256.md" "b/docs/03.2C\345\210\206\345\217\221\345\231\250\351\205\215\347\275\256.md" index 2068609..c0a89d6 100644 --- "a/docs/03.2C\345\210\206\345\217\221\345\231\250\351\205\215\347\275\256.md" +++ "b/docs/03.2C\345\210\206\345\217\221\345\231\250\351\205\215\347\275\256.md" @@ -82,4 +82,17 @@ DD_TOKEN = os.getenv('CC_D_TOKEN', '1dea61224e683d90c5d3694c89e30841681567747f41 CC_TG_CHAT_ID = "1222431510" # 你TelegramBot的Token CC_TG_TOKEN = "5065136980:BBF4uhUcGLP1-6qOHBfIT_0EArbXjek91GY" -``` \ No newline at end of file +``` + +## Bark +仅介绍Bark客户端的使用,要自行部署Bark服务端,请参考 [bark-server](https://github.com/Finb/bark-server) 。 + +### 获得推送url + +打开APP,复制URL + + + +### 设置对应环境变量 + +修改.env文件设置环境变量`LL_BARK_URL` \ No newline at end of file diff --git a/src/config/config.py b/src/config/config.py index c997222..8df6592 100644 --- a/src/config/config.py +++ b/src/config/config.py @@ -68,7 +68,7 @@ class Config: # 企业微信分发用户,多个用户用;分割 WECOM_TO_USER = os.getenv("CC_WECOM_TO_USER", "").replace(";", "|") # Bark URL - BARK_URL = os.getenv("BARK_URL", "") + BARK_URL = os.getenv("LL_BARK_URL", "") # 订阅的公众号配置 WECHAT_LIST = os.getenv( "CC_WECHAT_ACCOUNT", diff --git a/src/sender/bark_sender.py b/src/sender/bark_sender.py index 49cd9d4..76b7f7b 100644 --- a/src/sender/bark_sender.py +++ b/src/sender/bark_sender.py @@ -1,8 +1,17 @@ -# -*- coding:utf-8 -*- +#!/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 -import requests, json, time, os class BarkSender(SenderBase): @@ -16,6 +25,11 @@ def __init__(self, send_config: dict): 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"] @@ -52,6 +66,11 @@ def send(self, send_data) -> bool: 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"] @@ -65,10 +84,18 @@ def compose(self, send_data) -> str: 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__': + +if __name__ == "__main__": send( send_config={ "wecom_id": "", @@ -102,4 +129,4 @@ def send(send_config: dict, send_data: dict) -> bool: }, }, }, - ) \ No newline at end of file + ) From 889f66c11765f8cbd40f68a4fe65d58288b168c4 Mon Sep 17 00:00:00 2001 From: Leslie Leung Date: Wed, 29 Dec 2021 23:22:57 +0800 Subject: [PATCH 3/5] Create release.yml --- .github/workflows/release.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..b8474c4 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,25 @@ +name: Release +on: + pull_request: + branches: [ main ] + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Get version + id: get_version + run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} + + - name: Build Docker Image + run: docker build --no-cache=true -t leslieleung/liuli:schedule_${{ steps.get_version.outputs.VERSION }} -f schdule.Dockerfile . + + - name: Publish to Registry + uses: elgohr/Publish-Docker-Github-Action@master + with: + name: leslieleung/liuli + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} From 180ce285d612478356a98aab3bf1da05b682d117 Mon Sep 17 00:00:00 2001 From: LeslieLeung Date: Wed, 29 Dec 2021 23:52:19 +0800 Subject: [PATCH 4/5] fix: CI/CD --- .github/workflows/release.yml | 12 ++++-------- schdule.Dockerfile => schedule.Dockerfile | 0 2 files changed, 4 insertions(+), 8 deletions(-) rename schdule.Dockerfile => schedule.Dockerfile (100%) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b8474c4..91f6fc2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,17 +9,13 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - - name: Get version - id: get_version - run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} - - - name: Build Docker Image - run: docker build --no-cache=true -t leslieleung/liuli:schedule_${{ steps.get_version.outputs.VERSION }} -f schdule.Dockerfile . - + - name: Publish to Registry uses: elgohr/Publish-Docker-Github-Action@master with: name: leslieleung/liuli username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} + tag_names: true + dockerfile: schedule.Dockerfile + buildoptions: "--no-cache=true" diff --git a/schdule.Dockerfile b/schedule.Dockerfile similarity index 100% rename from schdule.Dockerfile rename to schedule.Dockerfile From ed3e449589b0376c2706b6291cbc4001d9359af5 Mon Sep 17 00:00:00 2001 From: LeslieLeung Date: Thu, 30 Dec 2021 23:40:50 +0800 Subject: [PATCH 5/5] fix: CI/CD --- .github/workflows/release.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 91f6fc2..8604083 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,7 +1,8 @@ name: Release on: - pull_request: - branches: [ main ] + push: + tags: + - 'v*' workflow_dispatch: jobs: @@ -13,7 +14,7 @@ jobs: - name: Publish to Registry uses: elgohr/Publish-Docker-Github-Action@master with: - name: leslieleung/liuli + name: liuliio/schedule username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} tag_names: true