-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotword_search.py
42 lines (34 loc) · 1.58 KB
/
hotword_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import Annotated
from graia.ariadne.app import Ariadne
from graia.ariadne.event.message import GroupMessage
from graia.ariadne.message.chain import MessageChain
from graia.ariadne.message.parser.base import DetectSuffix
from graia.ariadne.model import Group
from graia.saya import Channel
from graiax.shortcut.saya import listen
from lxml import etree
from yarl import URL
import aiohttp
channel = Channel.current()
channel.name("HotwordSearch")
channel.description("获取热词解释")
channel.author("I_love_study")
@listen(GroupMessage)
async def hotword(app: Ariadne, group: Group, message: Annotated[MessageChain, DetectSuffix("是什么梗")]):
await app.send_message(group, MessageChain("我不造啊"))
return
url = URL("https://jikipedia.com/search") % {"phrase": str(message)}
headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53"
}
async with aiohttp.request("GET", url, headers=headers) as r:
html = etree.HTML(await r.text()) # type: ignore
for i in range(1, 11):
title = html.xpath(f'//div[@class="masonry"]/div[{i}]//strong[@class="title pre"]//text()')
desc = html.xpath(f'//div[@class="masonry"]/div[{i}]//span[contains(@class,"text brax-node pre")]//text()')
if title and desc:
return await app.send_group_message(group, MessageChain(f"{''.join(title)}\n\n{''.join(desc)}"))
await app.send_group_message(group, MessageChain("错误:找不到相关结果"))