Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/idlist/__init__.py
2023-06-09 18:12:08 +08:00

28 lines
1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# https://github.com/XeroAlpha/caidlist/blob/master/backend/API.md
import urllib.parse
from core.builtins import Bot
from core.component import module
from core.utils.http import get_url
api = 'https://ca.projectxero.top/idlist/search'
i = module('idlist', support_languages=['zh_cn'])
@i.handle('<query> {{idlist.help}}')
async def _(msg: Bot.MessageSession, query: str):
query_options = {'q': query, 'limit': '6'}
query_url = api + '?' + urllib.parse.urlencode(query_options)
resp = await get_url(query_url, 200, fmt='json')
result = resp['data']['result']
plain_texts = []
if result:
for x in result[0:5]:
plain_texts.append(f'{x["enumName"]}{x["key"]} -> {x["value"]}')
if resp['data']['count'] > 5:
plain_texts.append(msg.locale.t('idlist.message.collapse'))
plain_texts.append('https://ca.projectxero.top/idlist/' + resp['data']['hash'])
await msg.finish('\n'.join(plain_texts))
else:
await msg.finish(msg.locale.t('idlist.message.none'))