forked from ze-kel/DayOne-JSON-to-MD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
203 lines (158 loc) · 7.6 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import io
import json
import os
import zipfile
import fnmatch
import shutil
from pytz import timezone
from datetime import datetime
# 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/")
yesterday = datetime.now().strftime('%Y-%m-%d')
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", " ")
myTimezone = timezone(entry['timeZone'].replace("\\", ""))
myDate = datetime.strptime(entry['creationDate'],'%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=timezone('UTC')).astimezone(myTimezone)
date = myDate.strftime('%Y-%m-%d %H-%M-%S')
if myDate.strftime('%Y-%m-%d') == yesterday:
print("Duplicated date:",yesterday)
yesterday = myDate.strftime('%Y-%m-%d')
# If first line starts with "# " — treat it as entry title
if text.split("\n")[0][:2] == "# ":
title = text.split("\n")[0].replace("# ", "").replace("#", "")
text = text[text.find("\n")+2:]
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
newName = datetime.strptime(momentItem['date'], '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=timezone('UTC')).astimezone(myTimezone).strftime('%Y-%m-%d %H-%M-%S') + " " + momentIdentifier
else:
# newName = date + " id " + momentIdentifier
newName = date + " " + 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:
newName = newName.replace(' ','%20')
text = text.replace(moment, f"![](/{folder}/{newName}.{momentFormat})")
rawTags = entry.get('tags')
location = entry.get('location')
filteredTags = []
if rawTags:
# We only need to append tags that aren't set in text
for tag in rawTags:
if "#"+ tag not in text:
filteredTags.append(tag.replace(" ", ""))
else:
print('This tag was ignored as it was in text: ',tag)
text = cleanup(text)
title = cleanup(title)
title = cleanFilename(title)
yamlString = "---\n"+"title: "+title+"\n"
yamlString += "date: " + datetime.strptime(entry['creationDate'], '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=timezone('UTC')).astimezone(myTimezone).strftime('%Y-%m-%d %H:%M:%S') + "\n"
if location:
yamlString += "latitude: " + str(location['latitude']) + "\n"
yamlString += "longitude: " + str(location['longitude']) + "\n"
if location.get('localityName'):
yamlString += "localityName: " + str(location['localityName']) + "\n"
if location.get('country'):
yamlString += "country: " + str(location['country']) + "\n"
yamlString+="---\n\n"
# newfilename = date +" — " + title + ".md"
newfilename = date[:-9] + ".md"
newfile = io.open(folderpath + "/" + newfilename , mode="a", encoding="utf-8")
newfile.write(yamlString)
if len(filteredTags)>0:
# add metadata for tags
# yamlString+="tags:\n- "+ "\n- ".join(filteredTags) + "\n"
tagsString = "#" + " #".join(filteredTags) + "\n\n"
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")