-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyperlink.py
78 lines (63 loc) · 2.42 KB
/
hyperlink.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
#!/usr/bin/env python2
from __future__ import print_function
import argparse
import sys
import json
import urllib2
from workflow import Workflow, ICON_WEB, ICON_WARNING, web, PasswordNotFound
def main(wf):
parser = argparse.ArgumentParser()
# add an optional (nargs='?') --apikey argument and save its
# value to 'apikey' (dest). This will be called from a separate "Run Script"
# action with the API key
parser.add_argument('--token', dest='token', nargs='?', default=None)
# add an optional query and save it to 'query'
parser.add_argument('query', nargs='?', default=None)
# parse the script's arguments
args = parser.parse_args(wf.args)
# decide what to do based on arguments
if args.token: # Script was passed an API key
# save the key
wf.save_password('hyperlink_token', args.token)
return 0 # 0 means script exited cleanly
# get token or exit
try:
token = wf.get_password('hyperlink_token')
except PasswordNotFound: # API key has not yet been set
wf.add_item('No Hyperlink token is set.',
'Please use hitoken to set your Hyperlink token.',
valid=False,
icon=ICON_WARNING)
wf.send_feedback()
return 0
query = wf.args[0]
opener = urllib2.build_opener()
opener.addheaders.append(('Cookie', 'session=%s' % token))
try:
content = opener.open("https://hyperlinkapp.com/api/search/?types=file&query=%s" % query).read()
except urllib2.HTTPError:
wf.add_item('Your Hyperlink token is not valid.',
'Please use hitoken to reset your Hyperlink token.',
valid=False,
icon=ICON_WARNING)
wf.send_feedback()
return 0
data = json.loads(content)
# default suggestion
wf.add_item(title=query,
subtitle="search on Hyperlink website",
arg='https://hyperlinkapp.com/search/?q=%s' % query,
valid=True,
icon=ICON_WEB)
nodes = []
for node in data['nodes']:
wf.add_item(title=node['filename'],
subtitle=node['path'],
arg=node['provider_link'],
valid=True,
icon=ICON_WEB)
# Send the results to Alfred as XML
wf.send_feedback()
if __name__ == u"__main__":
wf = Workflow()
sys.exit(wf.run(main))