Publishing to WordPress From Mercurial DVCS with XML-RPC

by Mike Levin on April 17, 2012

Well, it’s been quite awhile since I’ve been into this file (the journal file in the 360iTiger code repository). I moved my daily journal over to WordPress back in January, to some great effect. Unfortunately however, I think the price was getting too distant from this environment (SSH / vim). I’m going to past in what I’ve written so far today in Google Docs:

Moving my daily work journal onto Google Docs and WordPress, and OUT of the 360iTiger revision control system has had its benefits and disadvantages. The benefit is that my personal blog is getting built up much faster. There is less “friction” to publishing, in doing my free-form writing in Google Docs, and easily working at any computer I sit down at. The downside is that I don’t actually have the Tiger code loaded as often, and I am not as deeply immersed in the code.

It’s been appropriate during this code-freeze time of going public with Tiger, but it is most decidedly NOT good in the long-run for the code-base. I am also not getting as much daily practice on vim as I would like to be. Let me think through how to fix this. Where do I really WANT to do my writing? Which subtle side-benefits are best? Better proficiency at vim, and forcing myself into the code more often, versus friction-free publishing of my daily work journal to my blog?

And can I get the best of both worlds? Why is publishing my daily work journal to my blog not a super-easy process? Interesting! The solution is probably writing scripts against Mercurial to pull out the latest updates to the code repository specifically from the journal file, since the last commitment. I could see myself doing that. The script could actually even tie into WordPress, and submit a draft. Isn’t that the sort of thing I’d like to do? Or is that over-complicated, with moving parts?

QUICK exploratory investigation. WHAT am I investigating, anyway? Re-immersing myself into the SSH / Bash / vim environment. But I want to continue blogging as friction-free as I currently have been. Is it just habits? or is there a technology or copy-and-paste simplification issue here? Okay, screw simplification. Force it to be simplified. Figure out what you need to know as you go. You need to re-immerse yourself. There are two worlds: the clicking-around world of browsers and mice, and the down-to-business coding world of SSH / Bash and vim.

Okay, I pasted it into vim through an Ubuntu terminal window, without the nopaste option, so I got the 80-column line-wraps, which is how I have vim set up by default for prettier programming code. So, this now is the first thing to overcome in making my journal reside once again in vim and the Tiger program code–in order to get the best of BOTH worlds. I will not give up WordPress publishing now of my blog, but I must live in the SSH/vim world again.

The key to that is going to be a bit more Mercurial savvy. There must be a few commands I can trigger off to get exactly what I want. So, I am going to commit this journal entry to the revision control system, and by the time you can read it on my WordPress blog, it means I found the solution, and hopefully, I will document it in this same blog post.

Well, it’s just about 6:00 PM today, and I have the awesome ability to finish out this post, putting the finishing touches on it inside WordPress, but I flowed the above post in directly from the Linux command line, yanked out of the Mercurial distributed version control system, thanks to this amazing post by someone who needed to publish to WordPress from a Python script. Add to that, a few program modifications and a little bit of Linux command-line piping, and you’ve got the magic command:

hg diff -r789 | sed ‘s/^+//’ | fmt -w 2500 | python hg2wp.py

And the program code is:
[python]
import datetime, xmlrpclib, sys

wp_url = “http://somewordpressblog.com/xmlrpc.php”
wp_username = “someusername”
wp_password = “thepassword”
wp_blogid = “”

status_draft = 1
status_published = 0

server = xmlrpclib.ServerProxy(wp_url)

title = “Today’s Journal Entry”
content = sys.stdin.read()
date_created = xmlrpclib.DateTime(datetime.datetime.now())
categories = ["Daily Journal"]
tags = []
data = {‘title’: title, ‘description’: content, ‘dateCreated’: date_created, ‘categories’: categories, ‘mt_keywords’: tags}

post_id = server.metaWeblog.newPost(wp_blogid, wp_username, wp_password, data, status_published)
[/python]

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: