This repository has been archived by the owner on Jul 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquip.py
executable file
·102 lines (84 loc) · 3.63 KB
/
quip.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
# encoding: utf-8
import argparse
import sys
from workflow import Workflow, ICON_WARNING, ICON_INFO, notify, PasswordNotFound
from workflow.workflow import ICON_ROOT
import os
import webbrowser
from workflow.background import run_in_background, is_running
parser = argparse.ArgumentParser()
parser.add_argument('--setkey', dest='apikey', action='store_true')
parser.add_argument('query', nargs='?', default=None)
import sqlite3
ICON_DOCUMENT = os.path.join(ICON_ROOT, 'ClippingText.icns')
def main(wf):
if wf.update_available:
# Add a notification to top of Script Filter results
wf.add_item('New version available',
'Action this item to install the update',
autocomplete='workflow:update',
icon=ICON_INFO)
args = parser.parse_args(wf.args)
if args.apikey:
if not args.query:
webbrowser.open('https://quip.com/dev/token')
return 0
wf.save_password('quip_api_key', args.query)
notify.notify('Quip API key set')
return 0
try:
wf.get_password('quip_api_key')
except PasswordNotFound: # API key has not yet been set
wf.add_item('No API key set.',
'Please use set-quip-key to set your Quip API key.',
valid=False,
icon=ICON_WARNING)
wf.send_feedback()
return 0
cached_threads = wf.cached_data('documents', None, max_age=0)
if not wf.cached_data_fresh('documents', max_age=60 * 60):
cmd = ['/usr/bin/python', wf.workflowfile('quip-update.py')]
run_in_background('update', cmd)
if is_running('update'):
wf.add_item('Updating documents from Quip',
valid=False,
icon=ICON_INFO)
if args.query:
# Yes, this really shouldn't be re-creating the database each time, but hey-ho. Seems fast enough
query = args.query.lower().strip() + '*'
db = sqlite3.connect(':memory:')
cursor = db.cursor()
cursor.execute('CREATE VIRTUAL TABLE threads USING fts5(id, title, link, text)')
cursor.executemany('INSERT INTO threads (id, title, link, text) VALUES (?, ?, ?, ?)',
[
(result['id'], result['title'], result['link'], result['text'])
for result in (cached_threads or [])
])
cursor.execute('''SELECT id, title, link,
snippet(threads, 3, '', '', '', 10) as text_highlight
FROM threads
WHERE threads MATCH ?
ORDER BY bm25(threads, 0, 1.0, 0, 0.5)
LIMIT 15''', (query,))
threads = cursor.fetchall()
# wf.logger.info(threads2)
# threads = wf.filter(query, threads, key=lambda item: item['title'], min_score=20)
if not threads: # we have no data to show, so show a warning and stop
wf.add_item('No documents found', icon=ICON_WARNING)
wf.send_feedback()
return 0
for (id, title, link, snippet) in threads:
wf.add_item(title=title, arg=link,
quicklookurl=link,
subtitle=snippet,
icon=ICON_DOCUMENT,
uid=id, valid=True)
else:
wf.add_item('Enter a search term', valid=False, icon=ICON_INFO)
wf.send_feedback()
if __name__ == u"__main__":
wf = Workflow(normalization='NFD', update_settings={
'github_slug': 'orf/alfred-quip-workflow',
'frequency': 1,
})
sys.exit(wf.run(main))