-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote_to_markdown.py
95 lines (72 loc) · 2.15 KB
/
note_to_markdown.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
from youversion.utils import Client
import os
import json
import time
import random
import re
HEADER = """
---
title: "{0}"
date: {1}
book: {2}
draft: false
---
"""
template = """
## {0}
{1}
**Related verses**: {2}. See [notes](https://my.bible.com/notes/{3})
"""
has_data = True
index = 1
USERNAME = os.getenv("BIBLE_USERNAME")
PASSWORD = os.getenv("BIBLE_PASSWORD")
b = Client(USERNAME, PASSWORD)
while has_data:
notes = b.notes(index)
if type(notes) == dict and notes.get("error"):
has_data = False
for note in notes:
_obj = note["object"]
text = _obj["content"]
dt = _obj.get("updated_dt") or _obj.get("created_dt")
dt = re.sub(r'\.\d+', " ", dt).replace("T", " ")
id = _obj.get("id")
_refs = _obj.get("references")
locs = []
human_references = []
for ref in _refs:
human_references.append(ref["human"])
locs += ref["usfm"]
locs = set(locs)
human_references = ", ".join(human_references)
# quit()
for loc in locs:
book, chapter, verse = loc.split(".")
folder = f"notes/{book.lower()}"
if not os.path.exists(folder):
os.makedirs(folder)
md_file = f"{folder}/{chapter}.md"
mode = "w"
HEAD = HEADER.format(f"{book} {chapter}", dt, book)
content = ""
if os.path.exists(md_file):
f = open(md_file, "r")
content = f.read()
f.close()
mode = "a"
if "draft: true" in content or "draft: false" in content:
HEAD = ""
with open(md_file, mode, encoding="ascii", errors='replace') as f:
f.write(HEAD)
if id in content:
print(f"Skipping note with id: {id} in {loc}")
else:
f.write(template.format(
f"{book} {chapter}:{verse}",
text,
human_references, id,
dt
))
print(f"Page {index} complete")
index += 1