Skip to content

Instantly share code, notes, and snippets.

@yeonhoyoon
Last active December 16, 2015 03:59
Show Gist options
  • Save yeonhoyoon/5373979 to your computer and use it in GitHub Desktop.
Save yeonhoyoon/5373979 to your computer and use it in GitHub Desktop.
A script to upload Tistory backup XML file to Evernote. Developer token from Evernote required.
import re
import sys
from lxml import etree
from StringIO import StringIO
from evernote.edam.type import ttypes
from evernote.api.client import EvernoteClient
parser = etree.HTMLParser()
def convert_to_edml(text):
tree = etree.parse(StringIO(text), parser)
content = etree.tostring(tree.find('body'), encoding='unicode', method="xml")
content = re.sub(r'(\<\/?)body(\>)', r'\1en-note\2', content)
return content
def make_note(noteStore, noteTitle, noteBody, created, category, parentNotebookGuid):
noteBody = convert_to_edml(noteBody)
nBody = u"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
nBody += u"<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">"
nBody += noteBody
ourNote = ttypes.Note()
ourNote.title = noteTitle.encode('utf-8')
ourNote.content = nBody.encode('utf-8')
ourNote.created = int(created) * 1000
ourNote.updated = ourNote.created
ourNote.tagNames = ['tistory']
if category:
ourNote.tagNames.append(category.encode('utf-8'))
if parentNotebookGuid:
ourNote.notebookGuid = parentNotebookGuid
note = noteStore.createNote(ourNote)
return note
dev_token = "developer token"
notebook_name = 'notebook_name'
def upload_blog(tistory_blog_xml):
client = EvernoteClient(token=dev_token, sandbox=False)
noteStore = client.get_note_store()
notebooks = noteStore.listNotebooks()
parent_notebook = [notebook for notebook in notebooks if notebook.name == notebook_name][0]
post_id = 0
try:
for post in blog_xml.findall('post'):
post_id = post.find('id').text
title = post.find('title').text
content = post.find('content').text
created = post.find('published').text
category = post.find('category').text
make_note(noteStore, title, content, created, category, parent_notebook.guid)
sys.stdout.write('.')
except:
print("Error: " + str(post_id))
raise
else:
print("Success!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment