-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
177 lines (133 loc) · 5.86 KB
/
app.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import io
import json
import os
import glob
import zipfile
import fnmatch
import shutil
import sys
# If you use Obsidian.md you don't have to specifically point to media file as long as they are somewhere in "embedded media" folder.
# If true link will be ![[file]] else ![](/folder/file)
relativeMediaLinking = True
# Cleans up text from "\special character"
def cleanup(input):
def quickreplace(a,b):
nonlocal input
input = input.replace(a,b)
quickreplace("\.", ".")
quickreplace("\)", ")")
quickreplace("\(", "(")
quickreplace(r"\\", r"\\"[:-1])
quickreplace("\+", "+")
quickreplace("\!", "!")
quickreplace("\-", "-")
return input
# Removes everything that can't be in file name
def cleanFilename(input):
def quickreplace(a,b):
nonlocal input
input = input.replace(a,b)
quickreplace('"', "")
quickreplace("*", "")
quickreplace("\\", " ")
quickreplace(r"/", " ")
quickreplace("<", "")
quickreplace(">", "")
quickreplace(":", "")
quickreplace("|", " ")
quickreplace("?","")
quickreplace(".", "")
return input
def processJson(readFrom, subfolder, tempsubfolder, outpath):
with io.open(readFrom, encoding='utf-8') as read_file:
data = json.load(read_file)
# Setting and creating output folder structure
folderpath = outpath + "/" + subfolder + "/"
if not os.path.exists(folderpath):
os.makedirs(folderpath)
if not os.path.exists(folderpath + "/audios/"):
os.makedirs(folderpath + "/audios/")
if not os.path.exists(folderpath + "/photos/"):
os.makedirs(folderpath + "/photos/")
for entry in data['entries']:
text = entry['text']
# 2020-01-31T09:44:02Z => 2020.01.31 09-44
date = entry['creationDate'][:-4].replace("-", ".").replace(":", "-").replace("T", " ")
# If first line starts with "# " — treat it as entry title
if text.split("\n")[0][:2] == "# ":
title = text.split("\n")[0].replace("# ", "").replace("#", "")
else:
title = ""
# Process all "dayone-moments"(photos and audios)
splitted = text.split("\n")
filtered = fnmatch.filter(splitted, '![](dayone-moment:*)')
for moment in filtered:
# ![](dayone-moment:\/\/F7EEC3394BC0455FA513D9CAA0557C7E) => F7EEC3394BC0455FA513D9CAA0557C7E) = > F7EEC3394BC0455FA513D9CAA0557C7E
momentIdentifier = moment.split("/")[-1]
momentIdentifier = momentIdentifier[:-1]
# Iterate over all media attached to entry and find filename and format of the right one
if "audio" in moment:
momentIn = entry['audios']
folder = 'audios'
else:
momentIn = entry['photos']
folder = 'photos'
for momentItem in momentIn:
if momentIdentifier == momentItem['identifier']:
if "type" in momentItem:
momentFormat = momentItem['type']
else:
momentFormat = momentItem['format']
# For some reason "format" is codec not container — "aac" corresponds to "m4a" files. This is a stupid fix but still.
if momentFormat == "aac":
momentFormat = "m4a"
# Jpegs have filenames. Audios don't
if 'filename' in momentItem:
# Filename has file extension in it, we only need the name.
newName = momentItem['filename'].split('.')[0]
else:
if 'date' in momentItem:
newName = momentItem['date'][:-4].replace("-", ".").replace(":", "-").replace("T", " ") + " " + momentIdentifier
else:
newName = date + " id " + momentIdentifier
momentFile = momentItem['md5']
shutil.copy2(f'temp/{tempsubfolder}/{folder}/{momentFile}.{momentFormat}', f'./out/{subfolder}/{folder}/{newName}.{momentFormat}')
if relativeMediaLinking:
text = text.replace(moment, f"![[{newName}.{momentFormat}]]")
else:
text = text.replace(moment, f"![](/{folder}/{newName}.{momentFormat})")
rawtags = entry.get('tags')
writetags = False
if rawtags:
# We only need to append tags that aren't set in text
filteredtags = []
for tag in rawtags:
if "#"+ tag not in text:
filteredtags.append(tag.replace(" ", ""))
if len(filteredtags)>0:
tagsString = "#" + " #".join(filteredtags) + "\n"
writetags = True
text = cleanup(text)
title = cleanup(title)
title = cleanFilename(title)
newfilename = date +" — " + title + ".md"
newfile = io.open(folderpath + "/" + newfilename , mode="a", encoding="utf-8")
if writetags:
newfile.write(tagsString)
newfile.write(text)
def ProcessZips(inpath, outpath):
for zipname in os.listdir(inpath + "/"):
if zipname.endswith(".zip"):
zipnameClean = zipname.split(".")[0]
with zipfile.ZipFile( inpath + "/" + zipname, 'r') as zip_ref:
zip_ref.extractall("temp/" + zipnameClean)
for jsonname in os.listdir("temp/" + zipnameClean):
jsonnameClean = jsonname.split(".")[0]
if jsonname.endswith(".json"):
processJson("temp/" + zipnameClean + "/" + jsonname ,jsonnameClean, zipnameClean, outpath)
shutil.rmtree('temp')
if __name__ == "__main__":
inpath = "in"
outpath = "out"
ProcessZips(inpath, outpath)
print(f"Finished, check {outpath} folder")