Skip to content

Instantly share code, notes, and snippets.

@emanuele-f
Created May 15, 2022 12:56
Show Gist options
  • Save emanuele-f/a17bd759cb597351b046555f21812120 to your computer and use it in GitHub Desktop.
Save emanuele-f/a17bd759cb597351b046555f21812120 to your computer and use it in GitHub Desktop.
Convert standardnotes backup to notally
#!/usr/bin/env python3
import os
import sys
import json
from dateutil import parser
import xml.etree.cElementTree as ET
from xml.dom import minidom
def dt_to_ms(ds):
return int(parser.parse(ds).timestamp() * 1000)
def json_to_xml(data):
root = ET.Element("exported-notes")
notes = ET.SubElement(root, "notes")
for item in data["items"]:
if item["content_type"] != "Note":
continue
text = item["content"]["text"]
title = item["content"]["title"]
created = dt_to_ms(item["created_at"])
note = ET.SubElement(notes, "note")
ET.SubElement(note, "date-created").text = str(created)
ET.SubElement(note, "pinned").text = "false"
ET.SubElement(note, "title").text = title
ET.SubElement(note, "body").text = text
return root
def main():
if len(sys.argv) != 2:
print("Usage: %s input.txt" % os.path.basename(sys.argv[0]))
exit(1)
data = None
with open(sys.argv[1], "r") as f:
data = json.load(f)
if not data or not data["items"]:
print("Invalid input format")
exit(1)
xml = json_to_xml(data)
print(minidom.parseString(ET.tostring(xml)).toprettyxml(indent=" "))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment