/
csv-to-readwise.py
executable file
·52 lines (39 loc) · 1.15 KB
/
csv-to-readwise.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
#!/usr/bin/env python3
import csv
import fileinput
import json
import utils
NAME_MAPPING = {
"Highlight": "text",
"Title": "title",
"Author": "author",
"URL": "source_url",
"Note": "note",
"Location": "location",
"Date": "highlighted_at",
}
def collect_highlights(lines):
# Create a CSV reader object
reader = csv.DictReader(lines)
fieldnames = reader.fieldnames
result = []
for row in reader:
article = {}
for field in fieldnames:
if row[field] != "":
article[NAME_MAPPING.get(field, field)] = row[field]
if "location" in article and "location_type" not in article:
article["location_type"] = "page"
result.append(article)
return result
def main(args):
dry_run = args[1] == "-n" if len(sys.argv) > 1 else False
input_args = args[1:] if not dry_run else args[2:]
highlights = collect_highlights(fileinput.input(input_args))
if dry_run:
print(json.dumps(highlights, indent=2, ensure_ascii=False))
return
utils.create_highlights(highlights)
if __name__ == "__main__":
import sys
main(sys.argv)