Created
January 20, 2019 15:49
-
-
Save keichi/26d212049795892e5dfa293d7257ec47 to your computer and use it in GitHub Desktop.
Converts Day One JSON file to TextBundle
This file contains 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
import glob | |
import os | |
import json | |
import shutil | |
import sys | |
import re | |
def unescape(s): | |
s = s.replace(r"\n", "\n") | |
s = s.replace(r"\t", "\t") | |
for c in "\\/+-_!*:.()[]": | |
s = s.replace("\\" + c, c) | |
return s | |
def convert_entry(entry): | |
uuid = entry["uuid"] | |
print("Converting entry {}".format(uuid)) | |
destdir = uuid + ".textbundle" | |
os.makedirs(destdir, exist_ok=True) | |
os.makedirs(destdir + "/assets", exist_ok=True) | |
text = unescape(entry["text"]) | |
ids = re.findall(r"dayone-moment://([0-9A-F]+)", text) | |
for i in ids: | |
for photo in entry["photos"]: | |
if i == photo["identifier"]: | |
md5 = photo["md5"] | |
break | |
if not md5: | |
print("MD5 for {} not found".format(i)) | |
continue | |
files = glob.glob("photos/{}*".format(md5)) | |
if not files: | |
print("Linked file for {} not found".format(i)) | |
continue | |
shutil.copy(files[0], destdir + "/assets/") | |
fname = files[0].replace("photos/", "assets/") | |
text = text.replace("dayone-moment://" + i, fname) | |
with open(destdir + "/text.md", mode="w") as f: | |
f.write(text) | |
with open(destdir + "/info.json", mode="w") as f: | |
metadata = { | |
"version": 2, | |
"type": "net.daringfireball.markdown" | |
} | |
json.dump(metadata, f) | |
def main(): | |
with open(sys.argv[1], encoding="utf-8") as f: | |
j = json.load(f) | |
for entry in j["entries"]: | |
if "text" in entry: | |
convert_entry(entry) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment