This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbear.py
169 lines (130 loc) · 4.54 KB
/
bear.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import json
from config import get_bear_api_token
from xcall import xcall
def _wiki_link_escaped(value: str) -> str:
return value.replace('/', '\\/')
def _backlink_search_terms(note):
search_title = note.title
search_title = _wiki_link_escaped(search_title)
if '"' in search_title or '“' in search_title or '”' in search_title:
search_title = search_title.replace('“', '"')
search_title = search_title.replace('”', '"')
parts = search_title.split('"')
search_title = ""
for p in parts:
p = p.strip()
if len(p) == 0:
continue
search_title = search_title + '"' + p + '"' + ' '
search_title = search_title.strip()
else:
search_title = '"[[' + search_title + '"'
search_id = '"'+note.id+'"'
return [search_title, search_id]
class Note(object):
"""
Note is a complete note as returned by bear://x-callback-url/open-note
"""
def __init__(self, xcall_dict):
"""
Args:
xcall_dict: note dict as returned from xcall, including handling any weird escaping
"""
self.title = xcall_dict['title']
self.id = xcall_dict['identifier']
self.content = xcall_dict['note']
if xcall_dict['is_trashed'] == 'yes':
self.trashed = True
else:
self.trashed = False
def __hash__(self):
return hash(self.id) ^ hash(self.content)
def __repr__(self):
return f'<bear.Note {self.id} "{self.title[:30]}">'
@property
def title_escaped_for_wiki_link(self):
"""
Returns: the title of this note, properly escaped to put inside [[...]]
for a link.
Notes:
- Bear does not escape backticks in these links (`), but links including
are not actually clickable, probably because the editor doesn't generally
handle nested Markdown.
"""
return _wiki_link_escaped(self.title)
@property
def backlink_search_terms(self):
return _backlink_search_terms(self)
class NoteStub(object):
"""
NoteStub is a "stub" note as returned by bear://x-callback-url/search
"""
def __init__(self, xcall_dict):
"""
Args:
xcall_dict: note dict as returned from xcall, including handling any weird escaping
"""
self.title = xcall_dict['title']
self.id = xcall_dict['identifier']
def __hash__(self):
return hash(self.id) ^ hash(self.title)
def __repr__(self):
return f'<bear.NoteStub {self.id} "{self.title[:30]}">'
def to_note(self) -> Note:
return get_note(self.id)
@property
def title_escaped_for_wiki_link(self):
"""
Returns: the title of this note, properly escaped to put inside [[...]]
for a link.
Notes:
- Bear does not escape backticks in these links (`), but links including
are not actually clickable, probably because the editor doesn't generally
handle nested Markdown.
"""
return _wiki_link_escaped(self.title)
@property
def backlink_search_terms(self):
return _backlink_search_terms(self)
class SearchResults(object):
"""
SearchResults is an iterable collection of note stubs returned from a Bear search.
It handles the weird double-JSON-encoding we're getting back from xcall.
"""
def __init__(self, xcall_dict):
self.note_stubs = [NoteStub(s) for s in json.loads(xcall_dict['notes'])]
def __iter__(self):
return iter(self.note_stubs)
def __len__(self):
return len(self.note_stubs)
def __getitem__(self, i):
return self.note_stubs[i]
def search_term(term: str) -> SearchResults:
"""
Search Bear for the given term.
See Also:
https://bear.app/faq/X-callback-url%20Scheme%20documentation/#search
Args:
term: Term to search for.
Returns:
SearchResults
"""
return SearchResults(xcall_dict=xcall('bear', 'search', {
'term': term,
'token': get_bear_api_token(),
'show_window': 'no',
}))
def get_note(note_id: str) -> Note:
return Note(xcall_dict=xcall('bear', 'open-note', {
'id': note_id,
'show_window': 'no',
'open_note': 'no',
}))
def replace_note_contents(note_id: str, new_contents: str):
_ = xcall('bear', 'add-text', {
'id': note_id,
'mode': 'replace_all',
'text': new_contents,
'open_note': 'no',
'show_window': 'no',
})