Skip to content

Instantly share code, notes, and snippets.

@sychou
Created October 11, 2023 11:19
Show Gist options
  • Save sychou/0bf385ae121256d7ccd6073edf170ea0 to your computer and use it in GitHub Desktop.
Save sychou/0bf385ae121256d7ccd6073edf170ea0 to your computer and use it in GitHub Desktop.
Import Day One to Obsidian
import json
from datetime import datetime
from os import path
DATA_DIR = 'data'
VAULT_DIR = 'vault'
JOURNAL = 'World'
def parse_date(s: str):
datestr = s[0:10]
timestr = s[11:16]
date = datetime.strptime(datestr, '%Y-%m-%d')
weekday = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
][date.weekday()]
return datestr, timestr, weekday
def clean_text(text, photos):
text = text.replace('\\', '')
text = text.replace('#', '')
# TODO Need to swap out internal picture references
# Get text such as ![](dayone-moment://A7FB2C521C7E47E8AAF92308CB1F0B89)
# Look up the ID in the entry['photos'] list as 'identifier'
# Get the md5 field which is the name of the file
i = text.find('![](dayone-moment:')
while i > -1:
pre = text[:i]
post = text[i+53:]
id = text[i+20:i+52]
file_id = None
if photos is not None:
for photo in photos:
if photo['identifier'] == id:
file_id = f"{photo['md5']}.{photo['type']}"
break
text = pre + f'![[{file_id}]]' + post
i = text.find('![](dayone-moment:')
return text
def create_tags(tags):
text = ''
for tag in tags:
text += f'#{tag.lower()} '
return text.strip()
def make_entry(entry):
if 'text' not in entry:
print(f'ERROR: Nothing to do in entry {entry["uuid"]}')
return
datestr, timestr, weekday = parse_date(entry['creationDate'])
photos = entry['photos'] if 'photos' in entry else None
text = clean_text(entry['text'], photos)
tags = create_tags(entry['tags']) if 'tags' in entry else ''
if 'starred' in entry and entry['starred'] == 'true':
tags += ' #starred'
# print('******')
# print(f'{datestr}')
# print(f'{text}')
# print(f'{tags}')
fn = f'{VAULT_DIR}/{datestr}.md'
if path.exists(fn):
print(f'Appending to {fn}')
with open(fn, 'a') as f:
f.write(f'\n{JOURNAL} at {timestr}\n\n')
f.write(f'{text}\n\n')
f.write(f'{tags}\n')
else:
print(f'Writing to {fn}')
with open(fn, 'w') as f:
f.write(f'# {datestr} / {weekday}\n\n')
f.write(f'\n{JOURNAL} at {timestr}\n\n')
f.write(f'{text}\n\n')
f.write(f'{tags}\n')
def main():
fn = f'{DATA_DIR}/{JOURNAL}.json'
with open(fn) as f:
j = json.load(f)
for entry in j['entries']:
make_entry(entry)
if __name__ == "__main__":
main()
from os import path
from glob import glob
from shutil import copyfile
TARGET_DIR='/Users/sean/Vaults/Main/Daily/2022'
SOURCE_DIR='/Users/sean/Vaults/Day One'
files = glob(f'{SOURCE_DIR}/2022*')
for file in files:
source_fn = file[27:]
target_fn = f'{TARGET_DIR}/{source_fn}'
if path.isfile(target_fn):
print(f'Appending to => {target_fn}')
f = open(file)
next(f)
text = f.readlines()
tf = open(target_fn, 'a')
tf.writelines(text)
else:
print(f'Copying to => {target_fn}')
copyfile(file, target_fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment