Last active
April 30, 2025 23:41
-
-
Save marcus-crane/c9e62c4832540da09d11230c90715313 to your computer and use it in GitHub Desktop.
A small Python script to convert Diarium JSON export to the format I use in Obsidian
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prereqs: pip install pendulum markdownify | |
import json | |
from string import Template | |
from markdownify import markdownify as md | |
import pendulum | |
with open("diary.json", "r") as file: | |
data = json.loads(file.read()) | |
for entry in data: | |
date = pendulum.parse(entry.get('date')) | |
location = entry.get('location', False) | |
content = md(entry.get('html')) | |
template = """# $date | |
<< [[Private/Journal/$prevdate|$prevdate]] | [[Private/Journal/$nextdate|$nextdate]] >> | |
$content#journal""" | |
header = """--- | |
journal: diary | |
journal-start-date: $date | |
journal-end-date: $date | |
journal-section: day | |
""" | |
# I use MapView so retained location | |
if location: | |
header += f"""location: [$xloc,$yloc] | |
""" | |
header += """--- | |
""" | |
template = header + template | |
opts = { | |
'content': content, | |
'date': date.format("YYYY-MM-DD"), | |
'prevdate': date.add(days=-1).format("YYYY-MM-DD"), | |
'nextdate': date.add(days=1).format("YYYY-MM-DD") | |
} | |
if location: | |
opts['xloc'] = location[0] | |
opts['yloc'] = location[1] | |
src = Template(template) | |
result = src.substitute(opts) | |
with open(f'journal/{date.format("YYYY-MM-DD")}.md', 'w') as file: | |
file.write(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: I don't use this format currently but it's close enough