From f3ce5c45a9a7b6f376d21aa3932152f3edbfc25b Mon Sep 17 00:00:00 2001 From: congxu Date: Thu, 13 Apr 2023 18:20:45 +0800 Subject: [PATCH] Update getnews.py --- plugins/plugin_getnews/getnews.py | 66 +++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/plugins/plugin_getnews/getnews.py b/plugins/plugin_getnews/getnews.py index f121ec69e..71987b1b8 100644 --- a/plugins/plugin_getnews/getnews.py +++ b/plugins/plugin_getnews/getnews.py @@ -8,17 +8,15 @@ from common.log import logger import requests import json +import re +from datetime import datetime -@plugins.register(name="getnews", desire_priority=-1, hidden=True, desc="A simple plugin that get news", version="0.1", author="congxu") +@plugins.register(name="getnews", desire_priority=-1, hidden=True, desc="A simple plugin that says getnews", version="0.1", author="congxu") class getnews(Plugin): def __init__(self): super().__init__() self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context - self.url = "https://v2.alapi.cn/api/zaobao" - self.headers = {'Content-Type': "application/x-www-form-urlencoded"} - self.getnews_api_token = "UDuxUGXTKAlCJ3qt" - self.payload = "token="+self.getnews_api_token+"&format=json" logger.info("[getnews] inited") def on_handle_context(self, e_context: EventContext): @@ -26,22 +24,66 @@ def on_handle_context(self, e_context: EventContext): if e_context['context'].type != ContextType.TEXT: return + getnews_api_token = "UDuxUGXTKAlCJ3qt" content = e_context['context'].content logger.debug("[getnews] on_handle_context. content: %s" % content) - if content == "news": + + if re.search(r"每日新闻|getnews|今日新闻|今天有什么新闻", content): reply = Reply() reply.type = ReplyType.TEXT + + #接口信息 + url = "https://v2.alapi.cn/api/zaobao" + headers = {'Content-Type': "application/x-www-form-urlencoded"} + payload = "token="+getnews_api_token+"&format=json" + #获取新闻 req = requests.request("POST", url, data=payload, headers=headers) news_json = json.loads(req.text) news_date = news_json["data"]["date"] news_reasult = '\n'.join(news_json["data"]["news"]) - msg:ChatMessage = e_context['context']['msg'] - if e_context['context']['isgroup']: - reply.content = f"早上好, " + news_date +"\n" + response - else: - reply.content = f"早上好, " + news_date +"\n" + response + reply.content = f"今天是 " + news_date +"\n" + news_reasult + + e_context['reply'] = reply + e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑 + + if re.search(r"每日图片|getimg|今日摄影|每日摄影", content): + reply = Reply() + reply.type = ReplyType.IMAGE_URL + + #获取日期信息 + # 定义日期格式的正则表达式 + date_regex = r"(\d{2,4}).?(\d{1,2}).?(\d{1,2})" + + # 定义日期格式的列表,按照优先级排序 + date_formats = ["%Y-%m-%d", "%Y.%m.%d", "%Y年%m月%d日", "%Y年的%m月%d日", "%y年%m月%d"] + +# # 使用正则表达式查找日期信息 +# match = re.search(date_regex, content) +# if match: +# year, month, day = match.groups() +# for date_format in date_formats: +# try: +# parsed_date = datetime.strptime("{}-{}-{}".format(year, month, day), date_format) +# formatted_date = parsed_date.strftime("%Y-%m-%d") +# break +# except ValueError: +# formatted_date = datetime.now().strftime("%Y-%m-%d") +# else: +# formatted_date = datetime.now().strftime("%Y-%m-%d") + + #接口信息 https://alapi.cn/api/view/10 + url = "https://v2.alapi.cn/api/bing" + headers = {'Content-Type': "application/x-www-form-urlencoded"} + payload = "token="+getnews_api_token+"&format=json" + + #获取新闻 + req = requests.request("POST", url, data=payload, headers=headers) + img_json = json.loads(req.text) + img_reasult = img_json["data"]["url"] + reply.content = img_reasult + e_context['reply'] = reply e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑 @@ -59,5 +101,5 @@ def on_handle_context(self, e_context: EventContext): # e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑 def get_help_text(self, **kwargs): - help_text = "输入getnews,今天新闻\n" + help_text = "输入今日新闻,获取今天新闻\n输入今日图片,获取今日摄影" return help_text