Skip to content

Instantly share code, notes, and snippets.

@tzangms
Last active December 18, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzangms/5719334 to your computer and use it in GitHub Desktop.
Save tzangms/5719334 to your computer and use it in GitHub Desktop.
I wrote this script to convert my octopress posts back into wordpress, by using xmlrpc.
"""
Requirements:
* A Wordpress Blog
* Python Packages: Markdown, PyYAML, python-wordpress-xmlrpc
>>> pip install Markdown python-wordpress-xmlrpc PyYAML
WARNING:
USE THIS AT YOUR OWN RISK.
If your have any questions, please comment here below.
"""
import os
import yaml
import codecs
from time import strptime
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
import markdown
xmlrpc_endpoint = 'http://yourdomain.com/xmlrpc.php'
username = ''
password = ''
wp = Client(xmlrpc_endpoint, username, password)
for dirname, dirnames, filenames in os.walk('octopress/source/_posts/'):
for filename in filenames[10:]:
print filename
name, ext = os.path.splitext(filename)
if ext != '.markdown':
continue
with codecs.open(dirname + filename, 'r', 'utf8') as f:
content = f.read()
sep = content.index('---', 1)
meta = yaml.load(content[4: sep - 1])
content = markdown.markdown(content[sep + 4:])
# Post ID
post_id = None
if meta.get('guid'):
try:
post_id = meta['guid'].split('?p=', 1)[1]
except IndexError:
pass
# Datetime handle
if len(meta['date']) == 16:
meta['date'] += ':00'
if meta['date'].count('/'):
pattern = '%Y/%m/%d %H:%M:%S'
else:
pattern = '%Y-%m-%d %H:%M:%S'
date = strptime(meta['date'], pattern)
# Permalink
permalink = name[11:]
# Category
tags = meta.get('tags', None)
categories = meta['categories'] if isinstance(meta['categories'], list) else [meta['categories']]
terms_names = {'category': meta['categories']}
post = WordPressPost()
post.id = post_id
post.slug = permalink
post.content = content
post.title = meta['title']
post.post_status = 'publish'
post.date = date
post.terms_names = terms_names
wp.call(NewPost(post))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment