Skip to content

Instantly share code, notes, and snippets.

@leontiy
Last active December 26, 2021 18:24
Show Gist options
  • Save leontiy/6001095 to your computer and use it in GitHub Desktop.
Save leontiy/6001095 to your computer and use it in GitHub Desktop.
A (not so) simple python script to import Live Journal's XML export files to Day One for Mac.
#!/usr/bin/python
# HOW TO USE THIS SCRIPT:
# 0. Install [Day One CLI](http://dayoneapp.com/tools/)
# 1. Get your XML files using [Live Journal's export form](http://www.livejournal.com/export.bml)
# 2. Run this script against every exported file (yes, you get tons of XML files — it's a good idea to automate this step). The shell command should look like this
# `ls -1 *.xml | xargs -n1 lj-to-dayone -u username`
# assuming you have all your XMLs in current directory and lj-to-dayone on your PATH (or in the same directory).
# Author: Leonty Deriglazov, 14/07/2013.
import sys
import argparse
import subprocess
import xml.etree.ElementTree as ET
import datetime
try:
import pytz
except ImportError:
print "This script requires pytz library to work correctly with time stamps."
cmd = ["sudo", "easy_install", "--upgrade", "pytz"]
print "Running \"" + ' '.join(cmd) + "\"..."
installation_status = subprocess.call(cmd)
if installation_status == 0:
import pytz
else:
print "Could not install pytz. Can't proceed."
exit(1)
LJ_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
def process_entry(entry, user_name, timezone):
lj_link_format = "http://" + user_name + ".livejournal.com/%s.html"
ljTime = datetime.datetime.strptime(entry.find("eventtime").text, LJ_DATE_FORMAT) # naive / no-timezone
localTime = timezone.localize(ljTime) # timezone-aware
date = localTime.astimezone(pytz.timezone("UTC")).strftime(LJ_DATE_FORMAT)
tags = ["subject", "event", "current_mood", "current_music", "itemid"]
formats = [
"# %s\n\n",
"%s\n",
"\n**Mood:** %s",
"\n**Music:** %s",
"\n\nOriginal #LiveJournal [post](%s)" % lj_link_format
]
values = [entry.find(tag).text for tag in tags]
formattedValues = [format % value if value is not None else '' for value, format in zip(values, formats)]
entry = ''.join(formattedValues)
dayone = subprocess.Popen(["dayone", "-d=%s" % (date,), "new"], stdin = subprocess.PIPE)
dayone.communicate(entry.encode('utf-8'))
if dayone.returncode == 0:
print "New entry on %s" % (date,)
else:
raise
def convert(xmlPath, user_name, timezone):
tree = ET.parse(xmlPath)
root = tree.getroot()
entries = root.iterfind("entry")
for entry in entries:
process_entry(entry, user_name, timezone)
def local_timzone():
sys_setup = subprocess.Popen(['systemsetup', '-gettimezone'], stdout = subprocess.PIPE)
output = sys_setup.communicate()[0]
start_index = output.find(': ')
tz_name = output[start_index + 2:-1]
return tz_name
def main():
parser = argparse.ArgumentParser(description = 'Convert Live Journal XML export file to Day One entries.')
parser.add_argument('-u', '--user', type = str, dest = 'user', required = True, metavar = '<lj-username>')
parser.add_argument('-t', '--timezone', type = str, dest = 'tz_name', metavar = '<timezone-name>', help = 'Timezone of your Live Journal. Defaults to current system timezone.')
parser.add_argument('xml_file_name', type = str, metavar = '<lj-export.xml>')
args = parser.parse_args(sys.argv[1:])
tz = pytz.timezone(args.tz_name or local_timzone())
convert(args.xml_file_name, args.user, tz)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment