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

32 lines
1.1 KiB
Python
Raw Normal View History

2022-06-19 14:55:24 +00:00
# https://github.com/XeroAlpha/caidlist/blob/master/backend/API.md
2022-06-12 07:07:53 +00:00
import urllib.parse
2022-05-13 17:13:11 +00:00
2023-02-05 14:33:33 +00:00
from core.builtins import Bot
2023-03-04 08:51:56 +00:00
from core.component import module
2023-02-05 14:33:33 +00:00
from core.utils.http import get_url
2023-03-02 10:38:19 +00:00
from core.utils.i18n import get_target_locale
2022-05-13 17:13:11 +00:00
api = 'https://ca.projectxero.top/idlist/search'
2023-03-04 08:51:56 +00:00
i = module('idlist')
2022-05-13 17:13:11 +00:00
2023-03-02 10:38:19 +00:00
@i.handle('<query> {{idlist.desc}}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2023-03-02 10:38:19 +00:00
lang = get_target_locale(msg)
2022-05-13 17:13:11 +00:00
query = msg.parsed_msg['<query>']
query_options = {'q': query, 'limit': '6'}
query_url = api + '?' + urllib.parse.urlencode(query_options)
2022-08-01 15:33:35 +00:00
resp = await get_url(query_url, 200, fmt='json')
2022-05-13 17:13:11 +00:00
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:
2023-03-02 10:38:19 +00:00
plain_texts.append(lang.t('idlist.collapse'))
2022-06-29 14:58:43 +00:00
plain_texts.append('https://ca.projectxero.top/idlist/' + resp['data']['hash'])
2022-05-21 16:04:29 +00:00
await msg.finish('\n'.join(plain_texts))
2022-05-13 17:13:11 +00:00
else:
2023-03-02 10:38:19 +00:00
await msg.finish(lang.t('idlist.none'))