Skip to content

Commit

Permalink
Update getnews.py
Browse files Browse the repository at this point in the history
  • Loading branch information
congxuma committed Apr 13, 2023
1 parent 0719f89 commit 9df2178
Showing 1 changed file with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions plugins/plugin_getnews/getnews.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,82 @@
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):

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的默认逻辑

Expand All @@ -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

0 comments on commit 9df2178

Please sign in to comment.