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/github/search.py

35 lines
1.5 KiB
Python
Raw Normal View History

2021-07-30 08:06:10 +00:00
import traceback
from core.elements import MessageSession
2021-11-20 01:35:38 +00:00
from core.utils.bot import get_url
from modules.github.utils import dirty_check, darkCheck
2021-07-30 08:06:10 +00:00
2021-08-07 07:56:48 +00:00
2021-07-30 08:06:10 +00:00
async def search(msg: MessageSession):
try:
2021-11-20 01:35:38 +00:00
result = await get_url('https://api.github.com/search/repositories?q=' + msg.parsed_msg['<query>'], fmt='json')
2021-07-30 08:06:10 +00:00
items = result['items']
item_count_expected = int(result['total_count']) if result['total_count'] < 5 else 5
items_out = []
for item in items:
try:
items_out.append(str(item['full_name'] + ': ' + item['html_url']))
except TypeError:
continue
footnotes = f"另有 {result['total_count'] - 5} 个结果未显示。" if item_count_expected == 5 else ''
2021-08-07 07:56:48 +00:00
message = f"搜索成功:共 {result['total_count']} 个结果。\n" + '\n'.join(
items_out[0:item_count_expected]) + f'\n{footnotes}'
2021-07-30 08:06:10 +00:00
2021-11-20 01:35:38 +00:00
is_dirty = await dirty_check(message) or darkCheck(message)
2021-07-30 08:06:10 +00:00
if is_dirty:
2021-08-01 14:26:55 +00:00
message = 'https://wdf.ink/6OUp'
2021-07-30 08:06:10 +00:00
2021-08-01 14:26:55 +00:00
await msg.sendMessage(message)
2021-07-30 08:06:10 +00:00
except Exception as error:
2021-12-18 05:42:34 +00:00
if result['message'] == 'Not Found':
await msg.sendMessage('发生错误:查无此人,请检查拼写是否正确。')
else:
await msg.sendMessage('发生错误:' + str(
error) + '\n错误汇报地址https://github.com/Teahouse-Studios/bot/issues/new?assignees=OasisAkari&labels=bug&template=report_bug.yaml&title=%5BBUG%5D%3A+')
traceback.print_exc()