This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sourcegraph.py
83 lines (64 loc) · 2.55 KB
/
sourcegraph.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
# encoding: utf-8
import sys
from workflow import Workflow, ICON_WEB, web
from urllib import quote
icon_mapping = {
"Java": "images/icons/java.png",
"C": "images/icons/c-original.png",
"Go": "images/icons/go-original.png",
"C#": "images/icons/csharp-original.png",
"Python": "images/icons/python-original.png"
}
def get_posts(query):
url = "https://sourcegraph.com/.api/global-search?Query=%s&Limit=15" % query.replace(' ' , '+')
request = web.get(url)
# throw an error if request failed
# Workflow will catch this and show it to the user
request.raise_for_status()
# Parse the JSON returned by pinboard and extract the posts
result = request.json()
posts = result['Defs']
return posts
def main(wf):
if wf.args and len(wf.args) > 1:
searchtype = wf.args[0]
query = wf.args[1]
searchtype = "info" if searchtype == 'i' else "def"
posts = get_posts(query)
wf.clear_data()
if not posts:
wf.add_item(title="No results found for \"%s\"" % query,
subtitle = "",
valid=False,
icon='images/sourcegraph-mark.png')
wf.send_feedback()
return
for post in posts:
try:
arg = post['Repo']
# for packages, the entire file path does not need to be generated
if post['File'] != "" and post['File'] != "." and post['Kind'] != "package":
arg += "/-/%s/%s/%s/-/%s" % (searchtype, post['UnitType'], post['Unit'], post['Path'])
icon = "images/icons/doc-code.png"
if post['FmtStrings']['Language'] in icon_mapping:
icon = icon_mapping[post['FmtStrings']['Language']]
title = post['FmtStrings']['Name']['ScopeQualified']
subtitle = "from %s" % post['FmtStrings']['Name']['LanguageWideQualified']
wf.add_item(title=title,
subtitle=subtitle,
arg= arg,
valid=True,
icon=icon)
except Exception as e:
sys.stderr.write(str(e))
# Send the results to Alfred as XML
wf.send_feedback()
if __name__ == u"__main__":
update_settings = {
'github_slug': 'sourcegraph/sourcegraph-alfred',
'frequency': 1
}
wf = Workflow(update_settings=update_settings)
if wf.update_available:
wf.start_update()
sys.exit(wf.run(main))