#!/usr/bin/python ############################################################################## ## Sample Python script for posting to a MovableType 4.0 blog. This should ## work for other blogging servers that support the metaWeblog API. ## ## By Andy Reitz , January 22nd, 2008. ## URL: http://redefine.dyndns.org/~andyr/blog/archives/2008/01/posting-to-mova.html ############################################################################## import sys import re import xmlrpclib ## ## Edit these variables to suit your particular blog details. ## # This is the URL on your blog where you can make XML-RPC calls. xmlrpcURL = "http://foo.bar.com/cgi-bin/mt/mt-xmlrpc.cgi" # This is the ID number of your blog. blogID = 1 # The username you use to login to your blog. username = "joeuser" # The password that you use to login to your blog. password = "joepassword" ## ## No more editing should be necessary below this line (but tinkering is ## welcome!) ## class MTBlog: s = "" blogID = "" username = "" password = "" def __init__(self, xmlrpcURL, bid, u, p): self.s = xmlrpclib.Server(xmlrpcURL) self.blogID = bid self.username = u self.password = p # This method prints the title of N recent posts on your blog, where N is # a parameter that you supply. def getRecent(self, numPostsToGet): # Last param is # of posts to get. This returns a list of dictionaries. recentPosts = self.s.metaWeblog.getRecentPosts (self.blogID, self.username, self.password, numPostsToGet) # # On MovableType 4.0, the dictionary returned by getRecentPosts contains # the following keys: # # 'mt_keywords' # 'permaLink' # 'mt_convert_breaks' # 'mt_allow_pings' # 'title' # 'userid' # 'mt_tags' # 'mt_basename' # 'link' # 'mt_text_more' # 'mt_allow_comments' # 'dateCreated' # 'postid' # 'mt_excerpt' # 'description' # # I am only printing the title, as a proof of concept. # print "===================" c = 0 for x in recentPosts: print "Item #%d: %s" % (c, x['title']) c = c + 1 print "===================" # This method makes a new blog post on your blog. It takes as a parameter # a dictionary, which at a minimum should contain keys for 'title' and # 'description'. You can add additional keys (and values) as you see fit. def postToBlog(self, newPost): # This is some additional metadata that I saw MarsEdit passing to my # MovableType install. YMMV. newPost['mt_allow_comments'] = 1 newPost['mt_keywords'] = '' newPost['mt_convert_breaks'] = 1 # The number at the end is boolean - true (1) means that the new post # should be published. False (0) means that the post should be # submitted, but in an unpublished state. # # On MovableType 4.0, this call returns the post ID. np = self.s.metaWeblog.newPost (self.blogID, self.username, self.password, newPost, 1) # This is the ID # of the new post. return np def editBlogPost(self, postID, editedPost): ep = self.s.metaWeblog.editPost (postID, self.username, self.password, editedPost, 1) return ep ### ## Main Program starts here. ### # Create a new instance of a MTBlog object. bh = MTBlog (xmlrpcURL, blogID, username, password) # Create a new blog post dictionary. You should change the data here, to # whatever you want to appear in your blog post. post = {} post['title'] = "Posting from Python #1" post['description'] = "This is the body of the message. The message is the body, the body is the message!\nThis is a second linel\n" # Post this thing to the specified blog. mtPostID = bh.postToBlog (post) print "Post ID: %s" % mtPostID # For some reason, MarsEdit immediately edits the post after the initial # posting. So, this is an example of how post editing works. In my testing, # I found that if I just wanted to change the title, I also had to submit # the description field again - otherwise the body of my blog post would get # wiped out! post['title'] = "Posting from Python #%s" % mtPostID editedPostResult = bh.editBlogPost (mtPostID, post) print "Edited Post Result: %s" % editedPostResult