Archived
1
0
Fork 0

Merge pull request #155 from HornCopper/master

Add new module: mcmod
This commit is contained in:
Dianliang233 2022-01-15 19:45:28 -06:00 committed by GitHub
commit f44c3472a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

15
modules/mcmod/__init__.py Normal file
View file

@ -0,0 +1,15 @@
from core.component import on_command
from core.elements import MessageSession
from .mcmod import mcmod as m
mcmod = on_command(
bind_prefix='mcmod',
desc='从 MCMOD 获取 Minecraft Mod 信息',
developers=['Dianliang233', 'HornCopper'],
)
@mcmod.handle('<mod_name> {通过模组名获取模组简介及链接,可使用无歧义简写和准确中文。}')
async def main(msg: MessageSession):
message = await m(msg.parsed_msg['<mod_name>'])
await msg.sendMessage(message)

25
modules/mcmod/mcmod.py Normal file
View file

@ -0,0 +1,25 @@
from bs4 import BeautifulSoup
from config import Config
from core.utils import get_url
api = 'https://search.mcmod.cn/s?key='
async def mcmod(keyword: str):
search_url = api + keyword
webrender = Config('web_render')
if webrender:
search_url = webrender + 'source?url=' + search_url
html = await get_url(search_url)
print(html)
bs = BeautifulSoup(html, 'html.parser')
results = bs.find_all('div', class_='result-item')
if results is not None:
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
return f'{name}\n{url}\n{desc}'
else:
return '未找到结果。'