-
Notifications
You must be signed in to change notification settings - Fork 3
/
debian.py
78 lines (68 loc) · 2.28 KB
/
debian.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
67
68
69
70
71
72
73
74
75
76
77
78
"""
Debian 包查询
"""
import requests
import re
from handlers.message import Message
def __search_debian(keywords):
baseurl = 'https://packages.debian.org'
searchurl = '/search'
params = {
'suite': 'all',
'section': 'all',
'arch': 'any',
'searchon': 'names',
'keywords': keywords,
}
try:
res = requests.get(url=baseurl+searchurl, params=params, timeout=20)
except requests.exceptions.ReadTimeout:
return 'Search timeout.'
if res.status_code == 200:
restexts = res.text.split()
restext = ''
for s in restexts:
restext += ' ' + s
searchRes = {
'package': '',
'links': [],
}
hits = re.findall(r'<h3>.*?</ul>', restext, flags=0)
if len(hits) > 0:
hits = hits[0]
name = re.findall(r'<h3>(.*?)</h3>', hits, flags=0)
if len(name) > 0:
searchRes['package'] = name[0]
links = re.findall(r'<li.*?</li>', hits, flags=0)
for ln in links:
href = re.findall(r'href="(.*?)</a>', ln, flags=0)
if len(href) > 0:
href = href[0]
ln = ln.split(href, 1)[1][4:-5]
ln = ln.split('<br>', 1)
des = ln[0].strip()
ver = ln[1].strip()
dis = href.split('">', 1)
href = dis[0].strip()
dis = dis[1].strip()
searchRes['links'].append({
'distribution': dis,
'link': baseurl + href,
'description': des,
'version': ver,
})
result = searchRes['package']
for ln in searchRes['links']:
result += f"\n{ln['distribution']}:\n{ln['description']}\n{ln['version']}\n{ln['link']}"
return result
else:
return 'No search result.'
else:
return 'Debian server error.'
def run(message: Message) -> str:
req = list(message.raw_message.strip().split(' ', 1))
help_msg = 'Debian 包查询'
if len(req) > 1:
keywords = req[1].strip()
return __search_debian(keywords)
return help_msg