-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
120 lines (96 loc) · 3.42 KB
/
__init__.py
File metadata and controls
120 lines (96 loc) · 3.42 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
# Copyright (c) 2024 Manuel Schneider
import json
import re
from pathlib import Path
from time import sleep
from urllib import request, parse
from albert import *
md_iid = "5.0"
md_version = "2.2.0"
md_name = "Arch Linux Wiki"
md_description = "Search Arch Linux Wiki articles"
md_license = "MIT"
md_url = "https://github.com/albertlauncher/albert-plugin-python-arch-wiki"
md_authors = ["@ManuelSchneid3r"]
md_maintainers = ["@ManuelSchneid3r"]
class Plugin(PluginInstance, GeneratorQueryHandler):
wikiurl = "https://wiki.archlinux.org/title/"
baseurl = 'https://wiki.archlinux.org/api.php'
search_url = "https://wiki.archlinux.org/index.php?search=%s"
user_agent = "org.albert.extension.python.archwiki"
def __init__(self):
PluginInstance.__init__(self)
GeneratorQueryHandler.__init__(self)
def defaultTrigger(self):
return 'awiki '
@staticmethod
def makeIcon():
return Icon.image(Path(__file__).parent / "arch.svg")
def fetch(self, query: str, batch_size: int, offset: int):
params = {
'action': 'query',
'format': 'json',
'formatversion': 2,
'list': 'search',
'srlimit': batch_size,
'sroffset': offset,
'srsearch': query,
}
get_url = "%s?%s" % (self.baseurl, parse.urlencode(params))
req = request.Request(get_url, headers={'User-Agent': self.user_agent})
with request.urlopen(req) as response:
data = json.loads(response.read().decode())
results = []
for match in data['query']['search']:
title = match['title']
snippet = re.sub("<.*?>", "", match['snippet'])
url = self.wikiurl + parse.quote(title.replace(' ', '_'))
results.append(
StandardItem(
id=self.id(),
text=title,
subtext=snippet if snippet else url,
icon_factory=Plugin.makeIcon,
actions=[
Action("open", "Open article", lambda u=url: openUrl(u)),
Action("copy", "Copy URL", lambda u=url: setClipboardText(u))
]
)
)
return results
def items(self, ctx):
query = ctx.query.strip()
if not query:
yield [
StandardItem(
id=self.id(),
text=self.name(),
subtext="Enter a query to search in the Arch Wiki",
icon_factory=Plugin.makeIcon
)
]
return
# avoid rate limiting
for _ in range(50):
sleep(0.01)
if not ctx.isValid:
return
offset = 0
items = self.fetch(query, 10, offset)
if not items:
yield [
StandardItem(
id=self.id(),
text="Search '%s'" % query,
subtext="No results. Start online search on Arch Wiki",
icon_factory=self.makeIcon,
actions=[Action("search", "Open search",
lambda s=query: openUrl(self.search_url % s))]
)
]
return
while items:
yield items
offset += 10
items = self.fetch(query, 10, offset)