-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
134 lines (101 loc) · 3.74 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
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
import argparse
import json
import configparser
import pathlib
import re
from string import Template
import urllib.parse
import bs4
import pandoc
from pyzotero.zotero import Zotero
def make_filename(title):
""" convert pubication title to safe filename for obsidian """
title = title.replace(":", " - ")
return title + ".md"
def zotfile_to_markdown(note):
parsed = pandoc.read(note, format="html")
output = pandoc.write(parsed, format="markdown", options=["--wrap=none"])
output = re.sub(r'\\"(.*)\\"', r"> \1", output)
return output
def native_to_markdown(item_key, note):
# highlights have data-annotations with link
# data-citation-items seems redundnat with context from full item
new_soup = bs4.BeautifulSoup("<html><body></body></html", "html5lib")
soup = bs4.BeautifulSoup(note, "html5lib")
for citation in soup.find_all("p"):
highlight = citation.find(class_="highlight")
# remove curly quotes
quoted_text = highlight.text[1:-2]
if annotation := citation.find(class_="citation").nextSibling:
annotation_text = annotation.text
else:
annotation_text = ""
annotation_data = json.loads(urllib.parse.unquote(highlight.attrs['data-annotation']))
page = annotation_data['pageLabel']
annotation_key = annotation_data['annotationKey']
uri = f"zotero://open-pdf/library/items/{item_key}?page={page}&annotations={annotation_key}"
quote = soup.new_tag("blockquote")
quote.string = quoted_text
cite = soup.new_tag("cite")
cite.append(soup.new_tag("a", href=uri))
cite.a.string = "pdf"
quote.append(cite)
wrapper = soup.new_tag("p")
wrapper.append(quote)
wrapper.append(annotation_text)
new_soup.body.append(wrapper)
return pandoc.write(pandoc.read(new_soup.prettify(), format="html"), format="markdown")
def pick(items):
if not items:
raise Exception("no item found")
elif len(items) > 1:
for index, item in enumerate(items):
print(index, item["data"]["itemType"], item["data"]["title"])
return items[int(input("Which is the main item?\n"))]
else:
return items[0]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("search_string")
parser.add_argument(
"--dry-run",
action="store_true"
)
parser.add_argument(
"--config",
metavar="config.cfg",
type=argparse.FileType("r"),
)
args = parser.parse_args()
config = configparser.ConfigParser()
config.read_file(args.config)
zot = Zotero(
config["zotero"]["library"],
config["zotero"]["library_type"],
config["zotero"]["api_key"],
)
items = zot.items(q=args.search_string)
items = [
item for item in items if item["data"]["itemType"] not in {"note", "attachment"}
]
item = pick(items)
filename = make_filename(item["data"]["title"])
attachments = zot.children(item["key"], itemType="note")
attachment = pick(attachments)
if 'data-annotation' in attachment['data']['note']:
markdown = native_to_markdown(item['key'], attachment["data"]["note"])
else:
markdown = zotfile_to_markdown(attachment["data"]["note"])
context = item["data"].copy()
context["annotations"] = markdown
output = Template(config["obsidian"]["template"]).substitute(context)
destination_file = pathlib.Path(config["obsidian"]["vault"]) / filename
if args.dry_run:
print("would write", destination_file)
print(output)
else:
print("writing", destination_file)
with open(destination_file, "x") as fp:
fp.write(output)
if __name__ == "__main__":
main()