-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriteTextFile.py
More file actions
61 lines (54 loc) · 2.24 KB
/
writeTextFile.py
File metadata and controls
61 lines (54 loc) · 2.24 KB
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
import os.path
import json
current_file_dir = os.path.dirname(os.path.abspath(__file__))
json_file_path = os.path.join(current_file_dir, "amountNumber.json")
def checkFileExist(path_vault_references, title):
path = path_vault_references + title+".md"
if not os.path.exists(path):
f = open(path, "w")
f.close()
with open (json_file_path, "r")as jsonF:
data = json.load(jsonF)
newData = {
"title": f"{title}",
"amountRef": 1
}
with open(json_file_path, "w") as jsonF:
data["books"].append(newData)
json.dump(data, jsonF)
def checkHighlightExist(path, highlight):
#unique identifier should be the timestamp
exist = False
currContent = open(path, "r")
for line in currContent:
if highlight.timestamp in line.strip():
exist = True
return exist
def writeToFile(path_vault_references, list):
counter = 0
title = list[0].title
# index 0 because the list are all the highlights of one book and 0 to get one highlights book title
path = path_vault_references + title+".md"
f = open(path, "a")
with open(json_file_path, "r") as jsonF:
data = json.load(jsonF)['books']
for i in range(len(data)):
if data[i]['title'] == title:
index = i
# read value of json amountRef to be able to numberize the highlights
# solution with json due to future added highlights and be able to access the amount of highlights with minimal error handling
f.write(f"---\nauthor: [[{list[0].author}]]\ndiscussed: false\n---\n")
for highlight in list:
num = data[index]['amountRef']
if not checkHighlightExist(path, highlight):
#file structure or its design in obsidian
f.write(f"#### reference no.{num}\n")
f.write(f"- {highlight.message}\n")
f.write(f"- {highlight.timestamp}\n")
f.write(f"- {highlight.title} - {highlight.author} {highlight.page}\n\n")
data[index]['amountRef'] += 1
with open(json_file_path, "w") as jsonF:
json.dump({'books': data}, jsonF, indent=4)
counter= counter +1
print(f"{counter} highlights were send to {path}")
f.close()