-
Notifications
You must be signed in to change notification settings - Fork 0
/
zd_filter.py
executable file
·82 lines (67 loc) · 2.25 KB
/
zd_filter.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
#!/usr/bin/python
# encoding: utf-8
import os
import string
import sys
import workflow
import tempfile
import subprocess
import urlparse
import md5
from workflow.background import is_running, run_in_background
def getAppleScript(browser="Google Chrome"):
template = """
on run argv
tell application "%s"
set result to execute front window's active tab javascript "
attachments = this.document.getElementsByClassName(\\"attachment\\");
var thisPageAttachments = [];
for (var i=0; i < attachments.length; i++){
thisPageAttachments.push(attachments[i].href);
}
thisPageAttachments"
end tell
return result
end run
"""
return template % (browser)
def main(wf):
log = wf.logger
applescript = tempfile.NamedTemporaryFile(suffix=".applescript", delete=True)
applescript.write(getAppleScript())
applescript.flush()
proc = subprocess.Popen(['/usr/bin/osascript', applescript.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = proc.communicate()
attachments = out.split(", ")
cache_dir = wf.alfred_env['workflow_cache']
for attachment in attachments:
try:
qs = urlparse.urlparse(attachment).query
(qsKey, qsValue) = qs.split('=')
if qsKey == 'name':
name = qsValue
else:
name = attachment
digest = md5.md5(attachment).hexdigest()
cached_image_path = os.path.join(cache_dir, digest)
wf.add_item(
title=name,
subtitle=attachment,
arg="![%s](%s)" % (name, attachment),
quicklookurl=attachment,
valid=True
)
except Exception as e:
log.debug(e)
wf.send_feedback()
if __name__ == u"__main__":
wf = workflow.Workflow(
update_settings={
'github_slug': 'mikepkes/ZendeskHelpers',
'frequency': 1
}
)
if wf.update_available:
# Download new version and tell Alfred to install it
wf.start_update()
wf.run(main)