forked from teake/ainspire
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessscript.py
130 lines (108 loc) · 3.5 KB
/
processscript.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
123
124
125
126
127
128
129
130
import json
import base64
def main(q):
# Decode the arguments passed by Alfred.
# The result is a dictionary with keys 'type', 'value', and 'notification'.
args = json.loads(base64.b64decode(q))
t = args['type']
v = args['value']
n = args['notification']
#
# Act on the various types.
#
# Open an URL in the default browser.
if t == 'url':
import webbrowser
webbrowser.open(v)
# elif t == 'ads':
# import scriptfilter.get_token_setting as get_token
# import ads.sandbox as ads
#
# ads.config.token = get_token()
# pp = ads.SearchQuery(q=v)
# Past to clipboard.
elif t == 'clipboard':
import os
import alp
import subprocess
# Paste to the clipboard via the command line util 'pbcopy'.
# First, write the data to a file which 'pbcopy' will read.
cpf = os.path.join(alp.cache(),"clipboard.txt")
with open(cpf, "w") as f:
f.write(v)
# Now call 'pbcopy'.
subprocess.call('pbcopy < "' + cpf + '"',shell=True)
# Lookup Inspire record.
elif t == 'inspirerecord':
import urllib
import webbrowser
import xml.etree.ElementTree as ET
# First, get the URL for the record by querying Inspire.
# Get XML data from Inspire.
url = "http://inspirehep.net/rss?" + urllib.urlencode({'ln':'en','p':v})
try:
f = urllib.urlopen(url)
xml = f.read()
f.close()
except:
return
# Parse the XML.
e = ET.fromstring(xml)
for item in e.iter('item'):
for link in item.iter('link'):
recordurl = link.text
break
break
# Now open the URL.
webbrowser.open(recordurl)
elif t == 'clearcache':
import os
import alp
# Remove cache files from storage folder.
for f in os.listdir(alp.storage()):
file_path = os.path.join(alp.storage(), f)
try:
if os.path.isfile(file_path):
if os.path.splitext(f)[-1] == ".cache":
os.remove(file_path)
except Exception, e:
pass
elif t == 'setting':
import alp
settings = alp.Settings()
settings.set(**v)
elif t == 'open':
import os
os.system("open '" + v + "'")
elif t == 'getpdf':
import urllib
import os
if not os.path.isfile(v[1]):
urllib.urlretrieve(v[0],v[1])
os.system("open '" + v[1] + "'")
elif t == 'inspiresearch':
import sys
import os
import plistlib
# Get the keyword for the INSPIRE search. This is pretty fragile ...
info = plistlib.readPlist(os.path.abspath("./info.plist"))
kw = info["objects"][0]["config"]["keyword"]
# Print to stdout because 'print' prints a newline (which we don't want).
sys.stdout.write(kw + ' ' + v)
# If the notification is not empty, issue it.
if n != {}:
from alp.notification import Notification
if 'title' in n:
title = n['title']
else:
title = ""
if 'subtitle' in n:
subtitle = n['subtitle']
else:
subtitle = ""
if 'text' in n:
text = n['text']
else:
text = ""
notification = alp.notification.Notification()
notification.notify(title, subtitle, text)