打分结束后通过 Bark 推送结果
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
28
tushare/src/notifier.py
Normal file
28
tushare/src/notifier.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user