104 lines
3.8 KiB
Python
104 lines
3.8 KiB
Python
"""JPush 批量下发。未配置凭证时全部标记 skipped(不阻塞扫描)。"""
|
||
from __future__ import annotations
|
||
|
||
import httpx
|
||
|
||
from ..config import settings
|
||
|
||
API = "https://api.jpush.cn/v3/push"
|
||
|
||
|
||
def push_batch(rows: list[dict]) -> dict[int, str]:
|
||
"""rows: [{id, user_id, signal_id, symbol, interval, divergence_time}, ...]
|
||
|
||
返回 {notification_id: status},status ∈ success | failed | skipped
|
||
"""
|
||
if not settings.jpush_app_key or not settings.jpush_master_secret:
|
||
return {r["id"]: "skipped" for r in rows}
|
||
|
||
results: dict[int, str] = {}
|
||
batch_size = max(1, settings.push_batch_size)
|
||
for i in range(0, len(rows), batch_size):
|
||
chunk = rows[i : i + batch_size]
|
||
results.update(_push_chunk(chunk))
|
||
return results
|
||
|
||
|
||
def _push_chunk(chunk: list[dict]) -> dict[int, str]:
|
||
out: dict[int, str] = {}
|
||
# 同一用户多条信号:JPush 一次调用只能一个 payload,按用户聚合,单用户
|
||
# 一轮 >3 条合并为一条摘要(PRD §11.1 突发合流)。
|
||
by_user: dict[int, list[dict]] = {}
|
||
for r in chunk:
|
||
by_user.setdefault(r["user_id"], []).append(r)
|
||
|
||
with httpx.Client(timeout=10) as http:
|
||
for user_id, items in by_user.items():
|
||
rids = []
|
||
for it in items:
|
||
rid = it["jpush_rid"] if "jpush_rid" in it.keys() else None
|
||
if rid and rid not in rids:
|
||
rids.append(rid)
|
||
if not rids:
|
||
for it in items:
|
||
out[it["id"]] = "skipped"
|
||
continue
|
||
payload = _payload(items, rids)
|
||
try:
|
||
resp = http.post(
|
||
API,
|
||
json=payload,
|
||
auth=(settings.jpush_app_key, settings.jpush_master_secret),
|
||
)
|
||
ok = resp.status_code == 200
|
||
for it in items:
|
||
out[it["id"]] = "success" if ok else "failed"
|
||
except Exception:
|
||
for it in items:
|
||
out[it["id"]] = "failed"
|
||
return out
|
||
|
||
|
||
_KIND_LABEL = {"long_ignition": "做多起爆点", "short_ignition": "做空起爆点", "bd_resonance": "卖出(BD共振)"}
|
||
|
||
|
||
def _payload(items: list[dict], rids: list[str]) -> dict:
|
||
first = items[0]
|
||
sig_ids = [it["signal_id"] for it in items]
|
||
row_kind = first["kind"] if "kind" in first.keys() else "divergence"
|
||
label = _KIND_LABEL.get(row_kind, "量价背离")
|
||
if len(items) > 3:
|
||
content = f"本轮新增 {len(items)} 个信号"
|
||
extras = {"type": "signal_alarm", "kind": "digest",
|
||
"signal_ids": ",".join(str(s) for s in sig_ids)}
|
||
else:
|
||
content = (
|
||
f"【{label}】{first['symbol']} · {first['interval']} · {first['divergence_time'][5:]}"
|
||
)
|
||
extras = {"type": "signal_alarm", "kind": row_kind,
|
||
"signal_id": str(first["signal_id"]),
|
||
"symbol": first["symbol"], "interval": first["interval"],
|
||
"divergence_time": first["divergence_time"]}
|
||
return {
|
||
"platform": "all",
|
||
"audience": {"registration_id": rids},
|
||
"message": {"msg_title": "玄龙期货", "msg_content": content, "extras": extras},
|
||
"options": {"time_to_live": 86400},
|
||
}
|
||
|
||
|
||
def push_text(rids: list[str], title: str, body: str) -> None:
|
||
"""纯文本推送(到期提醒等),无深链 extras。"""
|
||
if not settings.jpush_app_key or not rids:
|
||
return
|
||
payload = {
|
||
"platform": "all",
|
||
"audience": {"registration_id": rids[:1000]},
|
||
"notification": {"android": {"alert": body, "title": title}},
|
||
"options": {"time_to_live": 86400},
|
||
}
|
||
try:
|
||
httpx.post(API, json=payload, auth=(settings.jpush_app_key, settings.jpush_master_secret), timeout=8)
|
||
except Exception:
|
||
pass
|