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/mcmod/mcmod.py

34 lines
1 KiB
Python
Raw Normal View History

2022-06-12 07:07:53 +00:00
from urllib.parse import quote
2022-01-16 01:44:31 +00:00
from bs4 import BeautifulSoup
2022-01-20 12:13:03 +00:00
2022-01-16 01:44:31 +00:00
from config import Config
2023-02-05 14:33:33 +00:00
from core.builtins import Url
2022-08-04 07:52:42 +00:00
from core.logger import Logger
2023-02-05 14:33:33 +00:00
from core.utils.http import get_url
2022-01-16 01:44:31 +00:00
api = 'https://search.mcmod.cn/s?key='
2022-02-22 13:03:30 +00:00
api_details = 'https://search.mcmod.cn/s?filter=3&key='
2022-01-16 01:44:31 +00:00
2023-03-23 15:25:46 +00:00
async def mcmod(msg, keyword: str, detail: bool = False):
2022-02-22 13:03:30 +00:00
endpoint = api_details if detail else api
search_url = endpoint + quote(keyword)
2022-01-16 01:44:31 +00:00
webrender = Config('web_render')
2022-06-15 00:29:20 +00:00
if not webrender:
return
2023-07-29 12:49:47 +00:00
search_url = webrender + '/source?url=' + quote(search_url)
2022-08-01 15:33:35 +00:00
html = await get_url(search_url, 200)
2022-08-04 07:52:42 +00:00
Logger.debug(html)
2022-01-16 01:44:31 +00:00
bs = BeautifulSoup(html, 'html.parser')
results = bs.find_all('div', class_='result-item')
2022-05-05 04:39:01 +00:00
if results:
2022-01-16 01:44:31 +00:00
res = results[0]
a = res.find('div', class_='head').find('a', recursive=False)
name = a.text
url = a['href']
desc = res.find('div', class_='body').text
2022-01-17 13:28:49 +00:00
return f'{name}\n{str(Url(url))}\n{desc}'
2022-01-16 01:44:31 +00:00
else:
2023-03-23 15:25:46 +00:00
return msg.locale.t('mcmod.message.not_found')