Files
GX-gp-notify/app/services/notification_service.py
T
v6ole 7455d7e426 chore: ruff 代码检查与修复
130 issues auto-fixed (import ordering, UP045/UP006 type annotations),
33 issues manually fixed (E712/E501/E402/E722 + N818 rename + per-file
wechat ignore for N8xx naming conventions). All 33 tests pass.
2026-05-09 14:28:09 +08:00

47 lines
1.4 KiB
Python

from typing import Any
from app.config import settings
from app.wechat.client import WeChatClient
class NotificationService:
def __init__(self):
self.client = WeChatClient()
async def send(self, announcements: list[dict[str, Any]]) -> int:
if not settings.wechat_enabled:
return 0
if not announcements:
return 0
sent = 0
for ann in announcements:
try:
title = ann.get("title", "")
if len(title) > 128:
title = title[:125] + "..."
purchase_name = ann.get("purchase_name", "")
if len(purchase_name) > 25:
purchase_name = purchase_name[:22] + "..."
pub_date = ann.get("publish_date")
time_str = pub_date.strftime("%Y-%m-%d %H:%M") if pub_date else "时间未知"
source_name = ann.get("source_name", "")
description = (
f'<div style="font-size: 14px; margin-top: 8px;">'
f'{source_name} | {purchase_name} | {time_str}'
f'</div>'
)
url = ann.get("content_url", "")
if await self.client.send_textcard(title, description, url):
sent += 1
except Exception:
continue
return sent