-
Notifications
You must be signed in to change notification settings - Fork 3
/
archlinux.py
82 lines (70 loc) · 2.63 KB
/
archlinux.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
79
80
81
82
"""
Archlinux 包查询
"""
import requests
import re
from handlers.message import Message
def search_arch(keywords: str) -> str:
baseurl = 'https://archlinux.org'
searchurl = '/packages/'
params = {
'sort': '',
'maintainer': '',
'flagged': '',
'q': 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
search_res = []
hits = re.findall(r'<table class="results">(.*?)</table>', restext, flags=0)
if len(hits) == 0:
return 'No search result.'
hits = re.findall(r'<tr>(.*?)</tr>', hits[0], flags=0)
result = 'Search Result(s):'
for i in range(min(5, len(hits))):
if i == 0:
continue
p = re.findall(r'<td.*?</td>', hits[i], flags=0)
try:
pkg = {
'arch': re.findall(r'<td>(.*?)</td>', p[0], flags=0)[0],
'repo': re.findall(r'<td>(.*?)</td>', p[1], flags=0)[0],
'name': re.findall(r'">(.*?)</a></td>', p[2], flags=0)[0],
'link': baseurl+re.findall(r'href="(.*?)"', p[2], flags=0)[0],
'version': re.findall(r'<td>(.*?)</td>', p[3], flags=0)[0],
'description': re.findall(r'">(.*?)</td>', p[4], flags=0)[0],
'lastupdate': re.findall(r'<td>(.*?)</td>', p[5], flags=0)[0],
'flagdate': re.findall(r'<td>(.*?)</td>', p[6], flags=0)[0],
}
except Exception:
pass
else:
for k in pkg.keys():
inhtmls = re.findall(r'<.*?>', pkg[k], flags=0)
for s in inhtmls:
pkg[k] = pkg[k].replace(s, '')
search_res.append(pkg)
if len(search_res) == 0:
return 'No search result.'
for rs in search_res:
result += f"\nPackage {rs['name']} {rs['version']}\n{rs['arch']} {rs['repo']}\n"\
f"{rs['description']}\n{rs['link']}"
return result
else:
return 'Archlinux server error.'
def run(message: Message) -> str:
req = list(message.raw_message.strip().split(' ', 1))
helpmsg = 'Archlinux 包查询'
if len(req) > 1:
keywords = req[1].strip()
return search_arch(keywords)
return helpmsg
if __name__ == '__main__':
print(search_arch('linux'))