2021-07-30 08:06:10 +00:00
|
|
|
import traceback
|
|
|
|
|
2023-02-05 14:33:33 +00:00
|
|
|
from core.builtins import Bot, Url, ErrorMessage
|
|
|
|
from core.utils.http import get_url
|
2021-11-20 01:35:38 +00:00
|
|
|
from modules.github.utils import dirty_check, darkCheck
|
2021-07-30 08:06:10 +00:00
|
|
|
|
2021-08-07 07:56:48 +00:00
|
|
|
|
2023-02-05 14:33:33 +00:00
|
|
|
async def search(msg: Bot.MessageSession):
|
2021-07-30 08:06:10 +00:00
|
|
|
try:
|
2022-08-10 13:22:44 +00:00
|
|
|
result = await get_url('https://api.github.com/search/repositories?q=' + msg.parsed_msg['<query>'], 200,
|
|
|
|
fmt='json')
|
2022-08-18 15:24:46 +00:00
|
|
|
if 'message' in result and result['message'] == 'Not Found':
|
2023-04-04 14:19:34 +00:00
|
|
|
await msg.finish(msg.locale.t("github.search.message.not_found"))
|
2022-08-18 15:24:46 +00:00
|
|
|
elif 'message' in result and result['message']:
|
|
|
|
await msg.finish(result['message'])
|
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:
|
2022-01-17 13:28:49 +00:00
|
|
|
items_out.append(str(item['full_name'] + ': ' + str(Url(item['html_url']))))
|
2021-07-30 08:06:10 +00:00
|
|
|
except TypeError:
|
|
|
|
continue
|
2023-04-08 15:27:36 +00:00
|
|
|
footnotes = msg.locale.t("github.search.message.more_information", more_result=result['total_count'] - 5) if item_count_expected == 5 else ''
|
2023-04-04 14:19:34 +00:00
|
|
|
message = msg.locale.t("github.search.message", result=result['total_count']) + '\n' + '\n'.join(
|
2021-08-07 07:56:48 +00:00
|
|
|
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
|
|
|
|
2022-05-21 16:04:29 +00:00
|
|
|
await msg.finish(message)
|
2022-08-10 13:22:44 +00:00
|
|
|
except ValueError:
|
2023-04-04 14:19:34 +00:00
|
|
|
await msg.finish(msg.locale.t("github.message.error"))
|
2021-07-30 08:06:10 +00:00
|
|
|
except Exception as error:
|
2022-08-10 13:22:44 +00:00
|
|
|
await msg.sendMessage(ErrorMessage(str(error)))
|
|
|
|
traceback.print_exc()
|