-
Notifications
You must be signed in to change notification settings - Fork 30
/
splitfile.py
162 lines (127 loc) · 6.33 KB
/
splitfile.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
import datetime
import dateutil.parser
import json
import pytz # pip install pytz
import re
import os
import sys
import shutil
import time
# Set this as the location where your Journal.json file is located
root = r"D:\OneDrive\Documents\dayone"
icons = False # Set to true if you are using the Icons Plugin in Obsidian
tagPrefix = "#journal/" # This will append journal/ as part of the tag name for sub-tags ie. instead of #entry, it is #journal/entry. To exclude set to "". If you change journal to something else, make sure you keep the trailing /
journalFolder = os.path.join(root, "journal") #name of folder where journal entries will end up
fn = os.path.join( root, "Journal.json" )
# Clean out existing journal folder, otherwise each run creates new files
if os.path.isdir(journalFolder):
print ("Deleting existing folder: %s" % journalFolder)
shutil.rmtree(journalFolder)
time.sleep(2) # Give time for folder deletion to complete. Only a problem if you have the folder open when trying to run the script
if not os.path.isdir(journalFolder):
print( "Creating journal folder: %s" % journalFolder)
os.mkdir(journalFolder)
if icons:
print ("Icons are on")
dateIcon = "`fas:CalendarAlt` "
else:
print ("Icons are off")
dateIcon = "" #make 2nd level heading
print( "Begin processing entries")
count = 0
with open(fn, encoding='utf-8') as json_file:
data = json.load(json_file)
for entry in data['entries']:
newEntry = []
createDate = dateutil.parser.isoparse(entry['creationDate'])
localDate = createDate.astimezone(pytz.timezone(entry['timeZone'])) # It's natural to use our local date/time as reference point, not UTC
# Add location
location = ''
for locale in ['placeName', 'localityName', 'administrativeArea', 'country']:
try:
location = "%s, %s" % (location, entry['location'][locale] )
except KeyError:
pass
location = location[2:]
dateCreated = str(createDate)
coordinates = ''
frontmatter = '''---
- created: ''' + dateCreated + '''
'''
if 'location' in entry:
coordinates = str(entry['location']['latitude']) + ',' + str(entry['location']['longitude'])
frontmatter = frontmatter + '- location: [' + coordinates + ']'
frontmatter = frontmatter + '''
---
'''
newEntry.append(frontmatter)
# Add date as page header, removing time if it's 12 midday as time obviously not read
if sys.platform == "win32":
newEntry.append( '## %s%s\n' % (dateIcon, localDate.strftime( "%A, %#d %B %Y at %#I:%M %p").replace( " at 12:00 PM", "")))
else:
newEntry.append( '## %s%s\n' % (dateIcon, localDate.strftime( "%A, %-d %B %Y at %-I:%M %p").replace( " at 12:00 PM", ""))) #untested
# Add body text if it exists (can have the odd blank entry), after some tidying up
try:
newText = entry['text'].replace("\\", "")
newText = newText.replace( "\u2028", "\n")
newText = newText.replace( "\u1C6A", "\n\n")
if 'photos' in entry:
# Correct photo links. First we need to rename them. The filename is the md5 code, not the identifier
# subsequently used in the text. Then we can amend the text to match. Will only to rename on first run
# through as then, they are all renamed.
# Assuming all jpeg extensions.
for p in entry['photos']:
pfn = os.path.join( root, 'photos', '%s.jpeg' % p['md5'] )
if os.path.isfile( pfn ):
newfn = os.path.join( root, 'photos', '%s.jpeg' % p['identifier'] )
print ( 'Renaming %s to %s' % (pfn, newfn ))
os.rename( pfn, newfn )
# Now to replace the text to point to the file in obsidian
newText = re.sub(r"(\!\[\]\(dayone-moment:\/\/)([A-F0-9]+)(\))", r'![[\2.jpeg]]', newText)
newEntry.append( newText )
except KeyError:
pass
## Start Metadata section
newEntry.append( '\n\n---\n' )
if not location == '':
if coordinates == []:
locationString = location
else:
locationString = '[' + location + '](geo:' + coordinates + ')'
newEntry.append( locationString )
# Add GPS, not all entries have this
# try:
# newEntry.append( '- GPS: [%s, %s](https://www.google.com/maps/search/?api=1&query=%s,%s)\n' % ( entry['location']['latitude'], entry['location']['longitude'], entry['location']['latitude'], entry['location']['longitude'] ) )
# except KeyError:
# pass
tags = []
if 'tags' in entry:
tags = []
for t in entry['tags']:
tags.append( "%s%s" % (tagPrefix, t.replace(' ', '-').replace('---', '-') ) )
if entry['starred']:
tags.append( "%sstarred" % (tagPrefix) )
if len(tags) > 0:
newEntry.append( "- Tags: %s\n" % " ".join( tags ))
# Save entries organised by year, year-month, year-month-day.md
yearDir = os.path.join( journalFolder, str(createDate.year) )
monthDir = os.path.join( yearDir, createDate.strftime( '%Y-%m'))
if not os.path.isdir(yearDir):
os.mkdir(yearDir)
if not os.path.isdir(monthDir):
os.mkdir(monthDir)
# Target filename to save to. Will be modified if already exists
fnNew = os.path.join( monthDir, "%s.md" % localDate.strftime( '%Y-%m-%d'))
# Here is where we handle multiple entries on the same day. Each goes to it's own file
if os.path.isfile( fnNew):
# File exists, need to find the next in sequence and append alpha character marker
index = 97 #ASCII a
fnNew = os.path.join( monthDir, "%s%s.md" % (localDate.strftime( '%Y-%m-%d'), chr(index)))
while os.path.isfile(fnNew):
index += 1
fnNew = os.path.join( monthDir, "%s%s.md" % (localDate.strftime( '%Y-%m-%d'), chr(index)))
with open(fnNew, 'w', encoding='utf-8') as f:
for line in newEntry:
f.write(line)
count += 1
print ("Complete: %d entries processed." % count)