重构:新增定时插件业务逻辑内聚到各自插件目录
- daily_news 插件内置百度新闻与60s图片获取逻辑,移除对 base.func_news 的业务依赖\n- epic_free 插件内置周五判断与免费游戏抓取逻辑,移除对 base.func_epic 的业务依赖\n- daily_ranking 插件内置排行生成与积分奖励逻辑,不再依赖 MessageStorage 业务封装\n- sehuatang_push 改为引用插件目录内的抓取与PDF生成实现,将核心业务代码迁入插件目录\n- 确保新插件可独立承载自身业务逻辑,平台层仅提供调度与基础设施能力
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import asyncio
|
||||
import base64
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
import requests
|
||||
|
||||
from base.func_news import News
|
||||
from base.plugin_common.message_plugin_interface import MessagePluginInterface
|
||||
from base.plugin_common.plugin_interface import PluginStatus
|
||||
from utils.robot_cmd.robot_command import GroupBotManager
|
||||
@@ -108,9 +108,9 @@ class DailyNewsPlugin(MessagePluginInterface):
|
||||
return {"success": False, "summary": "没有可推送目标群", "detail": {"target_count": 0}}
|
||||
|
||||
try:
|
||||
# 新闻抓取为同步逻辑,放入线程池避免阻塞调度主循环。
|
||||
text_news = await asyncio.to_thread(News().get_baidu_news)
|
||||
image_url = await asyncio.to_thread(News().get_news_60s)
|
||||
# 新闻抓取逻辑内聚在插件内,避免依赖外部业务模块。
|
||||
text_news = await asyncio.to_thread(self._get_baidu_news)
|
||||
image_url = await asyncio.to_thread(self._get_news_60s_image)
|
||||
except Exception as e:
|
||||
return {"success": False, "summary": f"新闻抓取失败: {e}", "detail": {"error": str(e)}}
|
||||
|
||||
@@ -151,3 +151,41 @@ class DailyNewsPlugin(MessagePluginInterface):
|
||||
resp = requests.get(url, timeout=15)
|
||||
resp.raise_for_status()
|
||||
return base64.b64encode(resp.content).decode("utf-8")
|
||||
|
||||
@staticmethod
|
||||
def _get_baidu_news() -> str:
|
||||
"""获取百度热榜文本(插件内实现)。"""
|
||||
headers = {
|
||||
"User-Agent": (
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) "
|
||||
"Gecko/20100101 Firefox/110.0"
|
||||
)
|
||||
}
|
||||
url = "https://top.baidu.com/api/board?platform=wise&tab=realtime"
|
||||
now = datetime.now()
|
||||
current_date = now.strftime("%Y年%m月%d日")
|
||||
weekdays = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
|
||||
output = f"当前日期:{current_date} {weekdays[now.weekday()]}\n\n"
|
||||
|
||||
resp = requests.get(url, headers=headers, timeout=15)
|
||||
resp.raise_for_status()
|
||||
post = resp.json()
|
||||
cards = post.get("data", {}).get("cards", [])
|
||||
index = 1
|
||||
for card in cards:
|
||||
for block in card.get("content", []):
|
||||
for article in block.get("content", []):
|
||||
if isinstance(article, dict) and "word" in article:
|
||||
title = str(article.get("word", "")).strip().replace(" ", "_")
|
||||
output += f"{index} :#{title}\n"
|
||||
index += 1
|
||||
return output
|
||||
|
||||
@staticmethod
|
||||
def _get_news_60s_image() -> Optional[str]:
|
||||
"""获取 60s 新闻图片地址(插件内实现)。"""
|
||||
api_url = "http://192.168.2.32:4399/v2/60s"
|
||||
resp = requests.get(api_url, timeout=15)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
return (data or {}).get("data", {}).get("image")
|
||||
|
||||
Reference in New Issue
Block a user