-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
executable file
·122 lines (99 loc) · 4.65 KB
/
search.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
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
121
122
#!/usr/bin/python
from __future__ import print_function
import sys
import urllib
import json
import requests
import requests_cache
import urllib2
from workflow import Workflow
__author__ = "gexiaowei"
__version__ = "1.4"
__email__ = "gandxiaowei@gmail.com"
__status__ = "Development"
requests_cache.install_cache('packages')
path = {
'bower': 'https://libraries.io/api/bower-search',
'npm': 'https://ac.cnstrc.com/autocomplete/{0}',
'passport': 'http://passportjs.org/data.json',
'yeoman': 'https://cdn.rawgit.com/yeoman/yeoman-generator-list/a8e5052236d77f0e0f1a6e453e81b3718f2fa270/cache.json',
'yarn': 'https://ofcncog2cu-2.algolianet.com/1/indexes/*/queries'
}
def search(wf):
args = wf.args
domain = urllib.quote_plus(args[0].encode('utf-8'))
keyword = urllib.quote_plus(args[1].encode('utf-8'))
result = []
if domain == "bower":
result = bower(keyword)
elif domain == "npm":
result = npm(keyword)
elif domain == "passport":
result = passport(keyword)
elif domain == 'yeoman':
result = yeoman(keyword)
elif domain == 'yarn':
result = yarn(keyword)
if len(result) == 0:
wf.add_item('No Packages found.')
wf.send_feedback()
return 0
for item in result:
wf.add_item(item["name"],
subtitle=item["subtitle"],
valid=True,
arg=item["url"])
wf.send_feedback()
return 0
def bower(keywords):
response = requests.request("GET",
path['bower'],
headers={}, params={"q": keywords})
result = json.loads(response.text)
return map(lambda item: dict(name=item["name"], subtitle=item["description"], url=item["homepage"] or item["repository_url"]), result)
def npm(keywords):
key = "CD06z4gVeqSXRiDL2ZNK"
home = "https://www.npmjs.com"
response = requests.request("GET",
path['npm'].format(keywords),
headers={}, params={"query": keywords, "autocomplete_key": key, "callback": "callback"})
result = json.loads(response.text.replace(
"typeof callback === 'function' && callback(", "").replace(");", ""))
return map(lambda item: dict(name=item["value"],
subtitle=item["data"][
'description'] if 'description' in item["data"] else '',
url=home + item["data"]["url"]), result["sections"]["packages"])
def yeoman(keywords):
response = requests.request('GET', path['yeoman'])
result = json.loads(response.text)
before = filter(lambda item: item['name'].find(keywords) != -1, result)
after = map(lambda item: dict(name=item['name'], subtitle=item[
'description'], url=item['site']), before)
return after
def passport(keywords):
response = requests.request('GET', path['passport'])
result = json.loads(response.text)
before = filter(lambda item: item['label'].find(keywords) != -1, result)
after = map(lambda item: dict(name=item['label'], subtitle=item[
'desc'], url=item['url']), before)
return after
def yarn(keywords):
home = 'https://yarnpkg.com/zh-Hans/package/'
query_string = {
"x-algolia-agent": "Algolia for vanilla JavaScript (lite) 3.25.1;react-instantsearch 5.0.0;JS Helper 2.23.2",
"x-algolia-application-id": "OFCNCOG2CU",
"x-algolia-api-key": "f54e21fa3a2a0160595bb058179bfb1e"
}
payload = {"requests": [{"indexName": "npm-search",
"params": "query=" + keywords + "&hitsPerPage=5&maxValuesPerFacet=10&page=0&attributesToRetrieve=%5B%22deprecated%22%2C%22description%22%2C%22downloadsLast30Days%22%2C%22repository%22%2C%22homepage%22%2C%22humanDownloadsLast30Days%22%2C%22keywords%22%2C%22license%22%2C%22modified%22%2C%22name%22%2C%22owner%22%2C%22version%22%5D&attributesToHighlight=%5B%22name%22%2C%22description%22%2C%22keywords%22%5D&highlightPreTag=%3Cais-highlight%3E&highlightPostTag=%3C%2Fais-highlight%3E&facets=%5B%22keywords%22%2C%22keywords%22%2C%22owner.name%22%5D&tagFilters=&optionalFacetFilters=%5B%5B%22_searchInternal.concatenatedName%3Areact%22%5D%5D"}]}
response = requests.request(
"POST", path['yarn'], data=json.dumps(payload), params=query_string)
result = json.loads(response.text)
return map(lambda item: dict(name=item["name"],
subtitle=item["description"],
url=home + item["name"]), result["results"][0]['hits'])
def main(wf):
search(wf)
if __name__ == u"__main__":
wf = Workflow()
sys.exit(wf.run(main))