forked from RolfKoenders/ulauncher-npms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
46 lines (34 loc) · 1.57 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
import requests
import logging
import json
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.OpenUrlAction import OpenUrlAction
logger = logging.getLogger(__name__)
class NpmsExtension(Extension):
def __init__(self):
super(NpmsExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
searchKeyword = event.get_argument()
if not searchKeyword:
return;
url = 'https://api.npms.io/v2/search?q={}&size=5'.format(searchKeyword)
logger.debug(url)
response = requests.get(url, headers={'User-Agent' : 'ulauncher-npms'})
data = response.json()
items = []
for result in data['results']:
package = result['package']
logger.debug(package['name'])
items.append(ExtensionResultItem(icon='images/icon.png',
name=package['name'],
description=package['description'],
on_enter=OpenUrlAction(package['links']['npm'])))
return RenderResultListAction(items)
if __name__ == '__main__':
NpmsExtension().run()