forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
84 lines (70 loc) · 3.23 KB
/
Copy path__init__.py
File metadata and controls
84 lines (70 loc) · 3.23 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
# -*- coding: utf-8 -*-
import json
from pathlib import Path
from time import sleep
from urllib import request, parse
from albert import *
md_iid = '2.0'
md_version = "1.4"
md_name = "ArchLinux Wiki"
md_description = "Search ArchLinux Wiki articles"
md_license = "BSD-3"
md_url = "https://github.com/albertlauncher/python/tree/master/awiki"
class Plugin(PluginInstance, TriggerQueryHandler):
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):
TriggerQueryHandler.__init__(self,
id=md_id,
name=md_name,
description=md_description,
defaultTrigger='awiki ')
PluginInstance.__init__(self, extensions=[self])
self.iconUrls = [f"file:{Path(__file__).parent}/arch.svg"]
def handleTriggerQuery(self, query):
stripped = query.string.strip()
if stripped:
# avoid rate limiting
for _ in range(50):
sleep(0.01)
if not query.isValid:
return
results = []
params = {
'action': 'opensearch',
'search': stripped,
'limit': "max",
'redirects': 'resolve',
'utf8': 1,
'format': 'json'
}
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())
for i in range(0, len(data[1])):
title = data[1][i]
summary = data[2][i]
url = data[3][i]
results.append(StandardItem(id=md_id,
text=title,
subtext=summary if summary else url,
iconUrls=self.iconUrls,
actions=[
Action("open", "Open article", lambda u=url: openUrl(u)),
Action("copy", "Copy URL", lambda u=url: setClipboardText(u))
]))
if results:
query.add(results)
else:
query.add(StandardItem(id=md_id,
text="Search '%s'" % query.string,
subtext="No results. Start online search on Arch Wiki",
iconUrls=self.iconUrls,
actions=[Action("search", "Open search", lambda s=query.string: self.search_url % s)]))
else:
query.add(StandardItem(id=md_id,
text=md_name,
iconUrls=self.iconUrls,
subtext="Enter a query to search on the Arch Wiki"))