From 7c9a0c79d35bc56be6e179a3832170ba83b54d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E7=BE=85=E7=8B=BC?= Date: Wed, 15 Jan 2025 23:08:04 +0800 Subject: [PATCH] Downgrade nbnhhsh --- modules/ask/tools/__init__.py | 2 -- modules/ask/tools/meme.py | 19 ---------- modules/meme/__init__.py | 61 ++++++++++++++++++++------------- modules/meme/jiki.py | 34 ------------------ modules/meme/locales/en_us.json | 13 ------- modules/meme/locales/zh_cn.json | 15 +++----- modules/meme/locales/zh_tw.json | 13 ------- modules/meme/moegirl.py | 50 --------------------------- modules/meme/nbnhhsh.py | 35 ------------------- modules/meme/urban.py | 42 ----------------------- 10 files changed, 42 insertions(+), 242 deletions(-) delete mode 100644 modules/ask/tools/meme.py delete mode 100644 modules/meme/jiki.py delete mode 100644 modules/meme/locales/en_us.json delete mode 100644 modules/meme/locales/zh_tw.json delete mode 100644 modules/meme/moegirl.py delete mode 100644 modules/meme/nbnhhsh.py delete mode 100644 modules/meme/urban.py diff --git a/modules/ask/tools/__init__.py b/modules/ask/tools/__init__.py index 080c6bcf52..1eed8314ac 100644 --- a/modules/ask/tools/__init__.py +++ b/modules/ask/tools/__init__.py @@ -1,7 +1,6 @@ from .bugtracker import bugtracker_tool from .ip_whois import ip_whois_tool from .mcv import mcv_tool -from .meme import meme_tool from .random import random_choice_tool, random_number_tool, random_uuid_tool from .search import search_tool from .self_knowledge import self_knowledge_tool @@ -13,7 +12,6 @@ ip_whois_tool, mcv_tool, server_tool, - meme_tool, random_choice_tool, random_number_tool, random_uuid_tool, diff --git a/modules/ask/tools/meme.py b/modules/ask/tools/meme.py deleted file mode 100644 index 39bf1b60a0..0000000000 --- a/modules/ask/tools/meme.py +++ /dev/null @@ -1,19 +0,0 @@ -import asyncio - -from modules.meme.moegirl import moegirl -from modules.meme.nbnhhsh import nbnhhsh -from modules.meme.urban import urban -from .utils import locale_en, AkariTool - - -async def meme(query: str): - results = await asyncio.gather( - moegirl(query, locale_en), nbnhhsh(query, locale_en), urban(query, locale_en) - ) - return f"Moegirlpedia result: {results[0]}\n\nNbnhhsh result: {results[1]}\n\nUrban Dictionary result: {results[2]}" - - -meme_tool = AkariTool.from_function( - func=meme, - description="A tool for looking up Internet memes, powered by Moegirlpedia, Nbnhhsh and Urban Dictionary. Input should be the exact name of the meme.", -) diff --git a/modules/meme/__init__.py b/modules/meme/__init__.py index 21b6932793..1f597d9d22 100644 --- a/modules/meme/__init__.py +++ b/modules/meme/__init__.py @@ -1,32 +1,47 @@ +import orjson as json + from core.builtins import Bot from core.component import module from core.dirty_check import check +from core.logger import Logger +from core.utils.http import post_url -# from modules.meme.jiki import jiki -from modules.meme.moegirl import moegirl -from modules.meme.nbnhhsh import nbnhhsh -from modules.meme.urban import urban - -meme = module( - bind_prefix="meme", - # well, people still use it though it only lived for an hour or so - alias="nbnhhsh", - desc="{meme.help.desc}", - doc=True, - developers=["Dianliang233"], - support_languages=["zh_cn", "en_us"], -) +n = module("nbnhhsh", + desc="{nbnhhsh.help.desc}", + doc=True, + developers=["Dianliang233"], + support_languages=["zh_cn"] + ) -@meme.command(" {{meme.help}}") +@n.command(" {{nbnhhsh.help}}") async def _(msg: Bot.MessageSession, term: str): - # res_jiki = await jiki(msg.parsed_msg[''], msg.locale) - # R.I.P. jikipedia - res_moegirl = await moegirl(term, msg.locale) - res_nbnhhsh = await nbnhhsh(term, msg.locale) - res_urban = await urban(term, msg.locale) - chk = await check(res_moegirl, res_nbnhhsh, res_urban) - res = "" + res_nbnhhsh = await nbnhhsh(msg, term) + chk = await check(res_nbnhhsh) + res = f"{term.lower()}\n" for i in chk: - res += i["content"] + "\n" + res += i["content"] await msg.finish(res.strip()) + + +async def nbnhhsh(msg: Bot.MessageSession, term: str): + req = json.dumps({'text': term}) + data = await post_url('https://lab.magiconch.com/api/nbnhhsh/guess', + data=req, + headers={'Content-Type': 'application/json', 'Accept': '*/*', + 'Content-Length': str(len(req))}, + fmt='json') + Logger.debug(data) + try: + result = data[0] + except IndexError: + await msg.finish(msg.locale.t("nbnhhsh.message.not_found")) + if 'trans' in result: + trans = result['trans'] + return "、".join(trans) + if 'inputting' in result: + inputting = result['inputting'] + if inputting: + return f'{msg.locale.t("nbnhhsh.message.guess", term=term)}{"、".join(inputting)}' + else: + await msg.finish(msg.locale.t("nbnhhsh.message.not_found")) diff --git a/modules/meme/jiki.py b/modules/meme/jiki.py deleted file mode 100644 index bf915a8e1d..0000000000 --- a/modules/meme/jiki.py +++ /dev/null @@ -1,34 +0,0 @@ -from bs4 import BeautifulSoup - -from core.builtins import Url -from core.logger import Logger -from core.utils.http import get_url -from core.utils.i18n import Locale -from core.utils.web_render import webrender - - -async def jiki(term: str, locale: Locale): - '''查询小鸡百科。 - - :param term: 需要查询的term。 - :returns: 查询结果。''' - try: - api = 'https://jikipedia.com/search?phrase=' + term - html = await get_url(webrender('source', api), 200, request_private_ip=True) - Logger.debug(html) - bs = BeautifulSoup(html, 'html.parser') - result = bs.select_one('[data-index="0"]') - title_ele = result.select_one( - 'a.title-container.block.title-normal') - content_ele = result.select_one('.lite-card-content') - - title = title_ele.get_text() - link = title_ele.get('href') - content = content_ele.get_text() - - results = bs.select('.lite-card').__len__() - count = str(result) if results < 15 else '15+' - return f'[{locale.t("meme.message.jiki")}] {locale.t("meme.message.result", result=count)}{ - title}\n{content}\n{str(Url(link))}' - except Exception: - return f'[{locale.t("meme.message.jiki")}] {locale.t("meme.message.error")}' diff --git a/modules/meme/locales/en_us.json b/modules/meme/locales/en_us.json deleted file mode 100644 index fa82af1ab3..0000000000 --- a/modules/meme/locales/en_us.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "meme.help": "Enquire for memes in Moegirlpedia, nbnhhsh, Urban Dictionary, etc.", - "meme.help.desc": "Enquire for memes in Moegirlpedia, nbnhhsh, Urban Dictionary, etc.", - "meme.message.error": "Something went wrong while querying.", - "meme.message.jiki": "Jikipedia", - "meme.message.moegirl": "Moegirlpedia", - "meme.message.nbnhhsh": "nbnhhsh", - "meme.message.nbnhhsh.result": "(${result} results, recorded): ", - "meme.message.nbnhhsh.result.ai": "(${result} results, AI speculation): ", - "meme.message.not_found": "No results found.", - "meme.message.result": "(${result} results): ", - "meme.message.urban": "Urban Dictionary" -} \ No newline at end of file diff --git a/modules/meme/locales/zh_cn.json b/modules/meme/locales/zh_cn.json index 7a878eea77..d3627529ce 100644 --- a/modules/meme/locales/zh_cn.json +++ b/modules/meme/locales/zh_cn.json @@ -1,13 +1,6 @@ { - "meme.help": "在萌娘百科、nbnhhsh、Urban Dictionary 等中查询迷因。", - "meme.help.desc": "全功能迷因查询。", - "meme.message.error": "查询出错。", - "meme.message.jiki": "小鸡百科", - "meme.message.moegirl": "萌娘百科", - "meme.message.nbnhhsh": "nbnhhsh", - "meme.message.nbnhhsh.result": "(${result} 个结果,已收录):", - "meme.message.nbnhhsh.result.ai": "(${result} 个结果,AI 猜测):", - "meme.message.not_found": "没有找到相关结果。", - "meme.message.result": "(${result} 个结果):", - "meme.message.urban": "Urban Dictionary" + "nbnhhsh.help.desc": "能不能好好说话?拼音首字母缩写转义工具。", + "nbnhhsh.help": "输入含有首字母缩写的文字获取含义。", + "nbnhhsh.message.guess": "有可能是:", + "nbnhhsh.message.not_found": "没有匹配到拼音首字母缩写。" } \ No newline at end of file diff --git a/modules/meme/locales/zh_tw.json b/modules/meme/locales/zh_tw.json deleted file mode 100644 index c46cae3338..0000000000 --- a/modules/meme/locales/zh_tw.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "meme.help": "在萌娘百科、nbnhhsh、Urban Dictionary 等中查詢迷因。", - "meme.help.desc": "全功能迷因查詢。", - "meme.message.error": "查詢出錯。", - "meme.message.jiki": "小雞百科", - "meme.message.moegirl": "萌娘百科", - "meme.message.nbnhhsh": "nbnhhsh", - "meme.message.nbnhhsh.result": "(${result} 個結果,已收錄):", - "meme.message.nbnhhsh.result.ai": "(${result} 個結果,AI 猜測):", - "meme.message.not_found": "沒有找到相關結果。", - "meme.message.result": "(${result} 個結果):", - "meme.message.urban": "Urban Dictionary" -} \ No newline at end of file diff --git a/modules/meme/moegirl.py b/modules/meme/moegirl.py deleted file mode 100644 index 96b2a19dd8..0000000000 --- a/modules/meme/moegirl.py +++ /dev/null @@ -1,50 +0,0 @@ -import re - -from core.logger import Logger -from core.utils.i18n import Locale -from modules.wiki import query_pages -from modules.wiki.utils.wikilib import QueryInfo - - -async def moegirl(term: str, locale: Locale): - result = await query_pages( - QueryInfo.assign( - "https://mzh.moegirl.org.cn/api.php", - headers={ - "accept": "*/*", - "accept-encoding": "gzip, deflate", - "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6", - "content-type": "application/json", - "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62", - }, - locale=locale.locale, - ), - term, - ) - msg = "" - if result["msg_list"]: - for msg_item in result["msg_list"]: - msg += msg_item.text - if result["wait_msg_list"]: - for msg_item in result["wait_msg_list"]: - Logger.debug("msg_item.text: ", msg_item.text) - redirect = re.search(r"(?<=是:\[)(.*?)(?=\]。)", msg_item.text).group(0) - Logger.debug(redirect) - if redirect: - wait = await query_pages( - QueryInfo.assign( - "https://mzh.moegirl.org.cn/api.php", - headers={ - "accept": "*/*", - "accept-encoding": "gzip, deflate", - "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6", - "content-type": "application/json", - "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62", - }, - locale=locale.locale, - ), - redirect, - ) - msg += wait["msg_list"][0].text - - return f'[{locale.t("meme.message.moegirl")}] {msg}' diff --git a/modules/meme/nbnhhsh.py b/modules/meme/nbnhhsh.py deleted file mode 100644 index ade3dae197..0000000000 --- a/modules/meme/nbnhhsh.py +++ /dev/null @@ -1,35 +0,0 @@ -import orjson as json - -from core.logger import Logger -from core.utils.http import post_url -from core.utils.i18n import Locale - - -async def nbnhhsh(term: str, locale: Locale): - '''查询nbnhhsh。 - - :param term: 需要查询的term。 - :returns: 查询结果。''' - try: - url = 'https://lab.magiconch.com/api/nbnhhsh/guess' - req = json.dumps({'text': term}) - data = await post_url(url, data=req, headers={'Content-Type': 'application/json', 'Accept': '*/*', - 'Content-Length': str(len(req))}, fmt='json') - Logger.debug(data) - try: - result = data[0] - except IndexError: - return f'[{locale.t("meme.message.nbnhhsh")}] {locale.t("meme.message.not_found")}' - if 'trans' in result: - trans = result['trans'] - count = trans.__len__() - return f'[{locale.t("meme.message.nbnhhsh")}] {locale.t( - "meme.message.nbnhhsh.result", result=count)}{"、".join(trans)}' - if 'inputting' in result and result['inputting']: - inputting = result['inputting'] - count = inputting.__len__() - return f'[{locale.t("meme.message.nbnhhsh")}] {locale.t( - "meme.message.nbnhhsh.result.ai", result=count)}{"、".join(inputting)}' - return f'[{locale.t("meme.message.nbnhhsh")}] {locale.t("meme.message.not_found")}' - except Exception: - return f'[{locale.t("meme.message.nbnhhsh")}] {locale.t("meme.message.error")}' diff --git a/modules/meme/urban.py b/modules/meme/urban.py deleted file mode 100644 index dc8464d92d..0000000000 --- a/modules/meme/urban.py +++ /dev/null @@ -1,42 +0,0 @@ -import orjson as json - -from core.builtins import Url -from core.logger import Logger -from core.utils.http import get_url -from core.utils.i18n import Locale -from core.utils.web_render import webrender - - -async def urban(term: str, locale: Locale): - '''查询urban dictionary。 - - :param term: 需要查询的term。 - :returns: 查询结果。''' - try: - url = 'http://api.urbandictionary.com/v0/define?term=' + term - text = await get_url(webrender('source', url), 200, headers={'accept': '*/*', - 'accept-encoding': 'gzip, deflate', - 'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6', - 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'}, - request_private_ip=True) - Logger.debug(text) - data = json.loads(text)['list'] - if not data: - return f'[{locale.t("meme.message.urban")}] {locale.t("meme.message.not_found")}' - count = data.__len__() - word = data[0]['word'] - definition = limit_length(data[0]['definition']) - example = limit_length(data[0]['example']) - link = data[0]['permalink'] - return f'[{locale.t("meme.message.urban")}] {locale.t("meme.message.result", result=count)}\n{ - word}\n{definition}\nExample: {example}\n{str(Url(link))}' - except Exception: - return f'[{locale.t("meme.message.urban")}] {locale.t("meme.message.error")}' - - -def limit_length(text, limit=50): - new = text - length = new.split(' ').__len__() - if length > limit: - new = ' '.join(new.split(' ')[0:limit]) + '…' - return new