Add nbnhhsh from dev/meme
This commit is contained in:
parent
cf26736977
commit
a0344f8d3d
2 changed files with 55 additions and 0 deletions
|
@ -62,6 +62,18 @@ async def get_url(url: str, status_code: int = False, headers: dict = None, fmt=
|
|||
return text
|
||||
|
||||
|
||||
@retry(stop=stop_after_attempt(3), wait=wait_fixed(3), reraise=True)
|
||||
async def post_url(url: str, data: any, headers: dict = None):
|
||||
'''发送POST请求。
|
||||
:param url: 需要发送的url。
|
||||
:param data: 需要发送的数据。
|
||||
:param headers: 请求时使用的http头。
|
||||
:returns: 发送请求后的响应。'''
|
||||
async with aiohttp.ClientSession(headers=headers) as session:
|
||||
async with session.post(url, data=data, headers=headers) as req:
|
||||
return await req.text()
|
||||
|
||||
|
||||
@retry(stop=stop_after_attempt(3), wait=wait_fixed(3), reraise=True)
|
||||
async def download_to_cache(link: str) -> Union[str, bool]:
|
||||
'''利用AioHttp下载指定url的内容,并保存到缓存(./cache目录)。
|
||||
|
|
43
modules/nbnhhsh/__init__.py
Normal file
43
modules/nbnhhsh/__init__.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
import traceback
|
||||
|
||||
import ujson as json
|
||||
|
||||
from core.elements import MessageSession
|
||||
from core.component import on_command
|
||||
from core.utils import post_url
|
||||
|
||||
n = on_command(
|
||||
bind_prefix='nbnhhsh',
|
||||
desc='能不能好好说话?拼音首字母缩写释义工具',
|
||||
developers=['Dianliang233'],)
|
||||
|
||||
|
||||
@n.handle('<term>')
|
||||
async def _(msg: MessageSession):
|
||||
await msg.sendMessage(await nbnhhsh(msg.parsed_msg['<term>']))
|
||||
|
||||
async def nbnhhsh(term: str):
|
||||
'''查询nbnhhsh。
|
||||
|
||||
:param term: 需要查询的term。
|
||||
:returns: 查询结果。'''
|
||||
try:
|
||||
url = 'https://lab.magiconch.com/api/nbnhhsh/guess'
|
||||
req = json.dumps({'text': term})
|
||||
text = await post_url(url, data=req, headers={'Content-Type': 'application/json', 'Accept': '*/*', 'Content-Length': str(len(req))})
|
||||
print(text)
|
||||
data = json.loads(text)
|
||||
result = data[0]
|
||||
if 'trans' in result:
|
||||
trans = result['trans']
|
||||
count = trans.__len__()
|
||||
return f'[nbnhhsh]({count}个结果,已收录):{"、".join(trans)}'
|
||||
elif 'inputting' in result and result['inputting'] != []:
|
||||
inputting = result['inputting']
|
||||
count = inputting.__len__()
|
||||
return f'[nbnhhsh]({count}个结果,AI 猜测):{"、".join(inputting)}'
|
||||
else:
|
||||
return '[nbnhhsh] 没有找到相关结果。'
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
return '[nbnhhsh] 查询出错。'
|
Reference in a new issue