From b9975d6f91d85093d0b0216ffa41c189ff48e976 Mon Sep 17 00:00:00 2001 From: fish Date: Sat, 2 May 2026 23:11:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=88=86=E7=BB=93=E6=9D=9F=E5=90=8E?= =?UTF-8?q?=E9=80=9A=E8=BF=87=20Bark=20=E6=8E=A8=E9=80=81=E7=BB=93?= =?UTF-8?q?=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.7 --- tushare/src/main.py | 11 ++++++++++- tushare/src/notifier.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tushare/src/notifier.py diff --git a/tushare/src/main.py b/tushare/src/main.py index af29e52..2275abc 100644 --- a/tushare/src/main.py +++ b/tushare/src/main.py @@ -1,7 +1,7 @@ import argparse import sys -from . import fetcher, scorer, storage +from . import fetcher, notifier, scorer, storage def run(ts_code: str) -> int: @@ -62,6 +62,15 @@ def run(ts_code: str) -> int: print(f" 持仓变化幅度: {ld['change_pct']:+.2f}%") print(f"\n[OK] 数据已持久化到 SQLite") + + push_title = f"{result.ts_code.split('.')[0]} {result.trade_date}" + push_body = ( + f"综合 {result.composite:.1f}\n" + f"短期 {result.short_term:.1f} | 中期 {result.medium_term:.1f} | 长期 {result.long_term:.1f}\n" + f"{result.signal}" + ) + if notifier.push_bark(push_title, push_body): + print("[Bark] 推送成功") return 0 diff --git a/tushare/src/notifier.py b/tushare/src/notifier.py new file mode 100644 index 0000000..f0f8b15 --- /dev/null +++ b/tushare/src/notifier.py @@ -0,0 +1,28 @@ +import os +from urllib.parse import quote + +import requests + +DEFAULT_BARK_KEY = "RvdtHq4py2avatt4AFJn9a" +BARK_BASE_URL = "https://api.day.app" + + +def push_bark(title: str, body: str, key: str | None = None, timeout: float = 15.0, retries: int = 1) -> bool: + bark_key = key or os.environ.get("BARK_KEY") or DEFAULT_BARK_KEY + url = f"{BARK_BASE_URL}/{bark_key}/{quote(title, safe='')}/{quote(body, safe='')}" + + last_err: Exception | None = None + for attempt in range(retries + 1): + try: + resp = requests.get(url, timeout=timeout) + except requests.RequestException as e: + last_err = e + continue + + if resp.status_code == 200: + return True + print(f"[WARN] Bark 推送返回非 200: {resp.status_code} {resp.text[:120]}") + return False + + print(f"[WARN] Bark 推送失败: {last_err}") + return False