-
Notifications
You must be signed in to change notification settings - Fork 3
/
dizzylab.py
66 lines (55 loc) · 2.32 KB
/
dizzylab.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import html
import os
import requests
import re
from handlers.message import Message
def search(key: str = 'Static World') -> dict:
url = 'https://www.dizzylab.net/'
params = {'s': key}
res = requests.get(url=os.path.join(url, 'search/'), params=params, timeout=15)
ans = {'l': [], "d": []}
if res.status_code != 200:
return ans
blks = re.findall(r'<a href="(.+?)">.+? <img src="(.+?)".+?/>.+? <h1>(.+?)</h1>.+? <h3.*?>(.*?)</h3>.+? </a>',
res.text, re.M | re.S)
for blk in blks:
if re.match(r'/l/.+', blk[0]):
ans['l'].append({'link': os.path.join(url, blk[0][1:] if blk[0][0] == '/' else blk[0]),
'cover': blk[1],
'title': blk[2],
'summary': blk[3]})
elif re.match(r'/d/.+', blk[0]):
ans['d'].append({'link': os.path.join(url, blk[0][1:] if blk[0][0] == '/' else blk[0]),
'cover': blk[1],
'title': blk[2],
'summary': blk[3]})
return ans
def run(message: Message) -> str:
help_msg = '小白会试着从 dizzylab 搜索 ´ ▽ ` )ノ'
req = list(message.raw_message.split(' ', 1))
ans = '好像啥也没有找到诶'
if len(req) == 1:
return help_msg
key = req[1].strip()
res = search(key)
key = None
if len(res['l']):
key = res['l'][0]
elif len(res['d']):
key = res['d'][0]
if not key:
return ans
# ans = f'[CQ:share,url={key["link"]},title={key["title"]}]'
ans = '<?xml version="1.0" encoding="utf-8"?>' \
'<msg templateID="12345" action="web" brief="[分享] %s" serviceID="1" url="%s">' \
'<item layout="2"><title>%s</title>' \
'<summary>%s</summary>' \
'<picture cover="%s"/></item>' \
'<source name="dizzylab" icon="https://cdn.dizzylab.net/static/favicon.ico" action="" appid="-1" /></msg>' \
% (html.escape(key['title']), html.escape(key["link"]), html.escape(key['title']),
html.escape(key["summary"]), html.escape(key["cover"]))
ans = ans.replace(',', ',').replace('&', '&').replace('[', '[').replace(']', ']')
ans = '[CQ:xml,data=%s]' % ans
return ans
if __name__ == "__main__":
print(search())