-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
48 lines (39 loc) · 1.27 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
import re
import os
from requests.exceptions import HTTPError
from joppy.api import Api
regex = re.compile(r'\[\[(.+)\]\]')
def main(api: Api):
notes = api.get_all_notes(fields='id,title,body')
titles = [note.title for note in notes]
if len(set(titles)) < len(titles):
print('Unable to handle duplicate titles. Aborting...')
return
title_to_id = { note.title: note.id for note in notes }
def get_link_markdown(match: re.Match[str]) -> str:
title = match.group(1)
link_id = title_to_id[title]
return f'[{title}](:/{link_id})'
for note in notes:
assert note.body is not None
if not regex.search(note.body):
continue
assert note.id is not None
print(f'Updating "{note.title}"')
api.modify_note(
note.id,
body=regex.sub(get_link_markdown, note.body)
)
if __name__ == '__main__':
token = os.getenv("API_TOKEN")
if not token:
print('Please provide an API token using the API_TOKEN environment variable')
exit(1)
api = Api(token=token)
try:
main(api)
except HTTPError as error:
if error.response.status_code == 403:
print('API token is wrong')
else:
raise error