⭐AI-driven public opinion & trend monitor with multi-platform aggregation, RSS, and smart alerts.🎯 告别信息过载,你的 AI 舆情监控助手与热点筛选工具!聚合多平台热点 + RSS 订阅,支持关键词精准筛选。AI 智能筛选新闻 + AI 翻译 + AI 分析简报直推手机,也支持接入 MCP 架构,赋能 AI 自然语言对话分析、情感洞察与趋势预测等。支持 Docker ,数据本地/云端自持。集成微信/飞书/钉钉/Telegram/邮件/ntfy/bark/slack 等渠道智能推送。
Python aibarkdata-analysisdockerhot-newsllmmailmcpmcp-servernewsntfypythonrsstrending-topicswechatwework
fix(mcp): 修复关键词正则配置的 JSON 序列化 (#1137)
VanAlwaysd09c77eaaa33
diff --git a/mcp_server/services/data_service.py b/mcp_server/services/data_service.pyindex 7ad0f8c60b..1db1758381 100644--- a/mcp_server/services/data_service.py+++ b/mcp_server/services/data_service.py@@ -43,6 +43,16 @@ class DataService:self.parser = ParserService(project_root)self.cache = get_cache()+ def _make_json_serializable(self, obj):+ """将对象中的 re.Pattern 转换为字符串,使其可 JSON 序列化"""+ if isinstance(obj, re.Pattern):+ return obj.pattern+ elif isinstance(obj, dict):+ return {k: self._make_json_serializable(v) for k, v in obj.items()}+ elif isinstance(obj, list):+ return [self._make_json_serializable(item) for item in obj]+ return obj+def get_latest_news(self,platforms: Optional[List[str]] = None,@@ -476,6 +486,9 @@ class DataService:config_data = self.parser.parse_yaml_config()word_groups = self.parser.parse_frequency_words()+ # 将 word_groups 中的正则表达式对象转换为字符串,使其可 JSON 序列化+ word_groups = self._make_json_serializable(word_groups)+# 根据section返回对应配置advanced = config_data.get("advanced", {})advanced_crawler = advanced.get("crawler", {})diff --git a/tests/test_config_serialization.py b/tests/test_config_serialization.pynew file mode 100644index 0000000000..ed0598a362--- /dev/null+++ b/tests/test_config_serialization.py@@ -0,0 +1,56 @@+"""Configuration responses serialize regexes without changing cached matchers."""++import json+import re+import tempfile+import unittest+from pathlib import Path+from unittest.mock import patch++from mcp_server.services.data_service import DataService+from trendradar.core.frequency import matches_word_groups+++class ConfigSerializationTests(unittest.TestCase):+ def test_config_response_keeps_cached_regexes_usable(self):+ with tempfile.TemporaryDirectory() as directory:+ config = Path(directory) / "config"+ config.mkdir()+ (config / "config.yaml").write_text("{}\n", encoding="utf-8")+ (config / "frequency_words.txt").write_text(+ "[WORD_GROUPS]\n/ai|ml/i\n", encoding="utf-8"+ )+ service = DataService(directory)+ cached_groups = service.parser.parse_frequency_words()+ self.assertTrue(matches_word_groups("AI news", cached_groups, []))++ with patch.dict("os.environ", {}, clear=True):+ for section in ("keywords", "all"):+ with self.subTest(section=section):+ result = service.get_current_config(section)+ decoded = json.loads(json.dumps(result))+ keywords = decoded if section == "keywords" else decoded["keywords"]+ self.assertEqual(keywords["total_groups"], 1)+ self.assertTrue(keywords["word_groups"])++ self.assertIs(service.parser.parse_frequency_words(), cached_groups)+ regexes = []++ def collect_regexes(value):+ if isinstance(value, re.Pattern):+ regexes.append(value)+ elif isinstance(value, dict):+ for child in value.values():+ collect_regexes(child)+ elif isinstance(value, list):+ for child in value:+ collect_regexes(child)++ collect_regexes(cached_groups)+ self.assertTrue(regexes)+ self.assertTrue(matches_word_groups("ML update", cached_groups, []))+ self.assertFalse(matches_word_groups("unrelated headline", cached_groups, []))+++if __name__ == "__main__":+ unittest.main()