This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
101 lines (74 loc) · 3.24 KB
/
main.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
import json
import requests
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent, SystemExitEvent, PreferencesUpdateEvent, \
PreferencesEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
# Using the REST-Api of Joplin: https://joplinapp.org/api/references/rest_api/
class JoplinExtension(Extension):
def __init__(self):
super(JoplinExtension, self).__init__()
# Number of notebooks that should be suggested
self.limit = None
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(SystemExitEvent, SystemExitEventListener())
self.subscribe(PreferencesEvent, PreferencesEventListener())
self.subscribe(PreferencesUpdateEvent, PreferencesUpdateEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
def add_note(self, note):
requests.post('http://localhost:41184/notes', json.dumps({
'title': note['text'],
'is_todo': note['type'] == 'todo',
'parent_id': note['notebookId']
}))
def get_notebooks(self):
response = requests.get('http://localhost:41184/folders')
return json.loads(response.content)
class ItemEnterEventListener(EventListener):
def on_event(self, event, extension):
extension.add_note(event.get_data())
class PreferencesEventListener(EventListener):
def on_event(self, event, extension):
try:
n = int(event.preferences['limit'])
except:
n = 10
extension.limit = n
class PreferencesUpdateEventListener(EventListener):
def on_event(self, event, extension):
if not event.id == 'limit':
pass
try:
n = int(event.new_value)
extension.limit = n
except:
pass
class SystemExitEventListener(EventListener):
def on_event(self, event, extension):
extension.close()
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
keyword = event.get_keyword()
query = event.get_argument()
if query == None:
return None
notebooks = extension.get_notebooks()
# Sort by note count. The notebook with the most notes should be on top.
notebooks.sort(key=lambda x: x['note_count'], reverse=True)
results = []
print(extension.limit)
for notebook in notebooks[:extension.limit]:
data = {
'text': query,
'type': keyword,
'notebookId': notebook['id']
}
results.append(ExtensionResultItem(icon='images/icon.png',
name="Add %s to notebook %s" % (keyword, notebook['title']),
on_enter=ExtensionCustomAction(data, keep_app_open=False)))
return RenderResultListAction(results)
if __name__ == '__main__':
JoplinExtension().run()