-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
60 lines (44 loc) · 2.34 KB
/
main.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
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.OpenUrlAction import OpenUrlAction
import requests
class HexExtension(Extension):
def __init__(self):
super(HexExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
query = event.get_argument() or str()
if len(query.strip()) == 0:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name='Open Elixir docs',
on_enter=OpenUrlAction('https://hexdocs.pm/elixir'))
])
else:
search_url = 'https://hex.pm/api/packages?search=%s&sort=recent_downloads'
api_results = requests.get(search_url % query).json()
result_items = list(map(self.build_result_item, api_results))
return RenderResultListAction(result_items)
def build_result_item(self, package):
primary_action = OpenUrlAction(package['html_url'])
if (package['docs_html_url'] != None):
options = [
ExtensionResultItem(icon='images/hex.png',
name='View package',
on_enter=OpenUrlAction(package['html_url'])),
ExtensionResultItem(icon='images/hexdocs.png',
name='View documentation',
on_enter=OpenUrlAction(package['docs_html_url']))
]
primary_action = RenderResultListAction(options)
return ExtensionResultItem(icon='images/icon.png',
name=package['name'],
description=package['meta']['description'],
on_enter=primary_action)
if __name__ == '__main__':
HexExtension().run()