From 611481e2277eca5b0057d6e321ceb0b86ff27a32 Mon Sep 17 00:00:00 2001 From: HUYO-OS Date: Mon, 1 Aug 2022 20:30:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix=20=E6=97=A0=E6=B3=95=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E5=92=8C=E5=88=A0=E9=99=A4=E6=8F=90=E9=86=92=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 有关的issues #136 --- Paimon_Info/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Paimon_Info/__init__.py b/Paimon_Info/__init__.py index c1d68fe..d5479f6 100644 --- a/Paimon_Info/__init__.py +++ b/Paimon_Info/__init__.py @@ -254,11 +254,11 @@ async def ssbq_handler(event: MessageEvent, msg: Message = CommandArg()): await update_note_remind2(user_id, uid, gid, 1) await ssbq.finish('开启提醒成功', at_sender=True) elif find_remind_enable.group('action') == '关闭提醒': - await ssbq.finish('关闭提醒成功', at_sender=True) await update_note_remind2(user_id, uid, gid, 0) + await ssbq.finish('关闭提醒成功', at_sender=True) elif find_remind_enable.group('action') == '删除提醒': - await ssbq.finish('删除提醒成功', at_sender=True) await delete_note_remind(user_id, uid) + await ssbq.finish('删除提醒成功', at_sender=True) else: data = await get_daily_note_data(uid) if isinstance(data, str): From 10a56ad8a37e5719ecb57fb71ecb53fd14cb282b Mon Sep 17 00:00:00 2001 From: HUYO-OS Date: Mon, 1 Aug 2022 20:46:15 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=8D=E6=B4=BB=20=E7=82=B9=E9=A4=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Paimon_Plugins/_order.py | 61 ------------------- Paimon_Plugins/order.py | 123 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 61 deletions(-) delete mode 100644 Paimon_Plugins/_order.py create mode 100644 Paimon_Plugins/order.py diff --git a/Paimon_Plugins/_order.py b/Paimon_Plugins/_order.py deleted file mode 100644 index 92a3ab0..0000000 --- a/Paimon_Plugins/_order.py +++ /dev/null @@ -1,61 +0,0 @@ -import random - -from littlepaimon_utils import aiorequests -from littlepaimon_utils.tools import FreqLimiter -from nonebot import on_command -from nonebot.adapters.onebot.v11 import Message, MessageEvent, MessageSegment -from nonebot.params import CommandArg -from nonebot.plugin import PluginMetadata - -""" -由于点餐api已经失效,本功能暂不可用,直到找到其他可用的点餐api -""" - -__plugin_meta__ = PluginMetadata( - name="点餐", - description="点餐查看食物图片", - usage=( - "点餐 食物名" - ), - extra={ - 'type': '娱乐', - 'range': ['private', 'group', 'guild'], - "author": "惜月 <277073121@qq.com>", - "version": "1.0.0", - }, -) - -order_pic = on_command('点菜', aliases={'点餐', '食谱', '我想吃'}, priority=13, block=True) -order_pic.__paimon_help__ = { - "usage": "点餐<食品名>", - "introduce": "群主,来一份派蒙!", - "priority": 11 -} - -order_lmt = FreqLimiter(10) - - -@order_pic.handle() -async def order_pic_handler(event: MessageEvent, msg=CommandArg()): - msg = str(msg).strip() - if not msg: - return - if not order_lmt.check(event.user_id): - await order_pic.finish(f'点餐冷却ing(剩余{order_lmt.left_time(event.user_id)}秒)') - else: - url = 'https://api.iyk0.com/shipu/?key=' + msg - res = await aiorequests.get(url=url) - res = res.json() - if res['code'] == 202: - await order_pic.finish('没有找到这种食品哦~') - order_lmt.start_cd(event.user_id, 10) - number = random.randint(1, 3) - recipe_list = [] - for i in range(0, number): - recipe = random.choice(res['data']) - if recipe not in recipe_list: - recipe_list.append(recipe) - mes = Message() - for recipe in recipe_list: - mes += MessageSegment.text(recipe['name'] + '\n') + MessageSegment.image(recipe['img']) + '\n' - await order_pic.finish(mes, at_sender=True) diff --git a/Paimon_Plugins/order.py b/Paimon_Plugins/order.py new file mode 100644 index 0000000..57e6aa1 --- /dev/null +++ b/Paimon_Plugins/order.py @@ -0,0 +1,123 @@ +import inspect +import json +import os +import random +import re +import sys +from typing import Dict, TypedDict + +import aiohttp +import lxml.html +from littlepaimon_utils.tools import FreqLimiter +from nonebot import on_command +from nonebot.adapters.onebot.v11 import Message, MessageEvent, MessageSegment +from nonebot.params import CommandArg +from nonebot.plugin import PluginMetadata + + +__plugin_meta__ = PluginMetadata( + name="点餐", + description="点餐查看食物图片", + usage=( + "点餐 食物名" + ), + extra={ + 'type': '娱乐', + 'range': ['private', 'group', 'guild'], + "author": "惜月 <277073121@qq.com>", + "version": "1.0.1", + }, +) + +order_pic = on_command('点菜', aliases={'点餐', '食谱', '我想吃'}, priority=13, block=True) +order_pic.__paimon_help__ = { + "usage": "点餐<食品名>", + "introduce": "群主,来一份派蒙!", + "priority": 11 +} + +order_lmt = FreqLimiter(10) +PATH_ROOT = os.path.dirname(os.path.abspath(__file__)) +PATH_CONFIG = os.path.join(PATH_ROOT, 'config.json') + + +class TypeFood(TypedDict): + name: str + image: str + +class base(): + def __init__(self): + self.default_haaders = { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0', + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', + 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', + 'Accept-Encoding': 'gzip, deflate', + } + + async def request(self, method: str, url: str, ctype: str, **kwargs) -> str: + async with aiohttp.ClientSession(headers=self.default_haaders, raise_for_status=True) as session: + async with session.request(method, url, **kwargs) as resp: + if ctype == 'text': + ret = await resp.text() + elif ctype == 'json': + ret = await resp.json(content_type=None) + else: + raise Exception('Unknow content type') + return ret + + async def order(self, name: str) -> TypeFood: + raise NotImplementedError + + +# 心食谱 +class xinshipu(base): + + URL_QUERY = 'https://www.xinshipu.com/doSearch.html?q=%s' + + async def order(self, name: str) -> TypeFood: + text = await self.request('GET', self.URL_QUERY % name, 'text') + html = lxml.html.fromstring(text) + nodes = html.cssselect('a.shipu > div.v-pw > img') + if not nodes: + return {} + node = random.choice(nodes) + image = node.get('src') + image_new = re.sub('@196w_126h_99q_1e_1c.jpg', '', image) + return { + 'name': node.get('alt'), + 'image': 'https:' + image_new, + } + + +def load_config(filename: str) -> Dict: + if not os.path.isfile(filename): + return {} + with open(filename, 'r') as f: + return json.load(f) + +config = load_config(PATH_CONFIG) + +waiters = dict(filter( + lambda x: issubclass(x[1], base) and x[1] != base, + inspect.getmembers(sys.modules[__name__], inspect.isclass) +)) +waiter = waiters[config.get('source', 'xinshipu')]() # 使用心食谱进行点餐 + + +@order_pic.handle() +async def order_pic_handler(event: MessageEvent, msg=CommandArg()): + msg = str(msg).strip() + info = await waiter.order(msg) + print(info) + if not msg: + return + if not order_lmt.check(event.user_id): + await order_pic.finish(f'点餐冷却ing(剩余{order_lmt.left_time(event.user_id)}秒)') + else: + if info.get('image'): + image = info['image'] + msg_ = MessageSegment.image(image) + info.get('name', '') + await order_pic.send(msg_, at_sender=True) + order_lmt.start_cd(event.user_id, 10) + else: + await order_pic.finish('派蒙没有找到这种食品哦')