-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
53 lines (37 loc) · 2.02 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
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
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
import requests
import webbrowser
def clamp(n, smallest, largest): return max(smallest, min(n, largest))
class NPMSearch(Extension):
def __init__(self):
super().__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
class ItemEnterEventListener(EventListener):
def on_event(self, event, extension):
# event is instance of ItemEnterEvent
data = event.get_data()
# do additional actions here...
webbrowser.open_new_tab(data['url'])
return RenderResultListAction([])
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
items = []
packages = event.query.split(" ")[1]
query = "https://api.npms.io/v2/search?q={}".format(packages)
response = requests.get(query).json()
for i in range(clamp(9, 0, len(response["results"]))):
data = {'url': response["results"][i]["package"]["links"]["npm"]}
items.append(ExtensionResultItem(icon='images/icon.png',
name=response["results"][i]["package"]["name"] + "@" + response["results"][i]["package"]["version"],
description=response["results"][i]["package"]["description"],
on_enter=ExtensionCustomAction(data, keep_app_open=False)))
return RenderResultListAction(items)
if __name__ == '__main__':
NPMSearch().run()