Python Logo

I was searching for some way(read script) to automatically transfer/add post to my WordPress blog from another site. The main mission was to transfer around 500 posts to wordpress and no way I was going to do it manually. On googling a little bit I got to know wordpress has it’s own api which uses xlrpc interface for communicating to blog and I can use that to automate my job. Below is the script I used. I just set the vars and loops and started it in terminal and voila! within ~30 min the new wordpress was up and running proudly with 500 new posts.

Find the script below.

It uses xmlpclib, read about it here.

For using it you need to:

  1. Enable XML-RPC functionality in your blog: go to Settings > Writing > Remote Publishing and check the checkbox.
  2. Install python and xmlpc.
  3. You are all set to go. You may also add custom date and time to your posts.
 1 import datetime, xmlrpclib
 2 
 3 wp_url = "http://www.yourblog.com/xmlrpc.php"
 4 wp_username = "USERNAME"
 5 wp_password = "PASSWD"
 6 wp_blogid = ""
 7 status_draft = 0
 8 status_published = 1
 9 
10 server = xmlrpclib.ServerProxy(wp_url)
11 
12 title = "Title with spaces"
13 content = "Body with lots of content"
14 date_created = xmlrpclib.DateTime(datetime.datetime.strptime("2011-10-20 21:08", "%Y-%m-%d %H:%M"))
15 categories = ["category here"]
16 tags = ["sometag", "othertag"] 
17 data = {'title': title, 'description': content, 'dateCreated': date_created, 'categories': categories, 'mt_keywords': tags}
18 
19 post_id = server.metaWeblog.newPost(wp_blogid, wp_username, wp_password, data, status_published)