-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbear_api.py
106 lines (84 loc) · 3.18 KB
/
bear_api.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
import subprocess
import sqlite3
import re
from urllib.parse import urlparse, parse_qs, quote
from links import HeaderLink
from constants import BEAR_DB, WRITE_API_URL, OPEN_NOTE_API_URL, TEST, INSERT_OPTIONS, REPLACE_OPTIONS, ROOT_SECTION_TEXT
def notes():
with sqlite3.connect(BEAR_DB) as conn:
conn.row_factory = sqlite3.Row
result = conn.execute("SELECT * FROM `ZSFNOTE` WHERE `ZTRASHED` LIKE '0'")
# print(f"Database fields: {database_fields(result)}")
return result.fetchall()
def test_modify_note():
with sqlite3.connect(BEAR_DB) as conn:
#r = conn.execute("UPDATE ZSFNOTE SET ZTEXT='# Note B\nAnother test' WHERE `ZUNIQUEIDENTIFIER`='462D4FA7-C2AA-4150-AE2A-4C5D8BB74713-60300-00009B8DECC337BB'")
#conn.commit()
conn.row_factory = sqlite3.Row
result = conn.execute("SELECT * FROM `ZSFNOTE` WHERE `ZUNIQUEIDENTIFIER`='462D4FA7-C2AA-4150-AE2A-4C5D8BB74713-60300-00009B8DECC337BB'")
print(f"Database fields: {database_fields(result)}")
return result.fetchall()
def append_text_to_note(note, text):
x_call_text = f"{WRITE_API_URL}?{INSERT_OPTIONS}&text={encode(text)}&id={note.uid}&new_line=no"
if TEST:
print("")
print("The following would be added to your note: \n")
print('—'*40, end="\n\n")
print(text)
print("")
print('—'*40)
else:
return call(x_call_text)
def replace_note_text(note, text):
x_call_text = f"{WRITE_API_URL}?{REPLACE_OPTIONS}&text={encode(text)}&id={note.uid}"
if TEST:
print("")
print("The script would have replaced existing note with: \n")
print('—'*40, end="\n\n")
print(text)
print("")
print('—'*40)
else:
return call(x_call_text)
def note_link(text):
pattern = r'\[([^\]]*)\]\((bear:\/\/x-callback-url\/open-note[^\)]*)\)'
for link_title, link_url in re.findall(pattern, text):
parsed_url = urlparse(link_url)
try:
title = parse_qs(parsed_url.query)['title'][0]
except (KeyError, IndexError):
title = None
href_id = parse_qs(parsed_url.query)['id'][0]
try:
header = parse_qs(parsed_url.query)['header'][0]
except (KeyError, IndexError):
header = None
yield HeaderLink(href_id=href_id, title=link_title, header=header, open_note_title=title)
def markdown_link(link):
try:
if link.title == link.header:
text = f"{link.title}{ROOT_SECTION_TEXT}"
else:
text = f"{link.title}/{link.header}"
except AttributeError:
text = link.title
url = OPEN_NOTE_API_URL
url += f"?id={link.href_id}"
try:
url += f"&title={link.open_note_title}"
except AttributeError:
pass
try:
url += f"&header={link.header}"
except AttributeError:
pass
return f"[{text}]({url})"
def call(x_call_text):
return subprocess.call(["open", x_call_text])
def database_fields(result):
return ', '.join(d[0] for d in result.description)
def encode(text):
return quote(text)
if __name__ == "__main__":
for x in test_modify_note():
print(x['ZMODIFICATIONDATE'])