#!/usr/bin/python ############################################################################## ## Python module for posting to a MovableType 4.0 blog. This should work for ## other blogging servers that support the metaWeblog API. ## ## By Andy Reitz , February 7th, 2008. ## URL: http://redefine.dyndns.org/~andyr/blog/archives/2008/02/delish2mt.html ############################################################################## import sys import re import xmlrpclib class MTBlog: s = "" blogID = "" username = "" password = "" __categoryList = None __debug = False def __init__(self, xmlrpcURL, bid, u, p): self.s = xmlrpclib.Server(xmlrpcURL) self.blogID = bid self.username = u self.password = p def setDebug(self, d): self.__debug = d # 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 # Fetches the list of categories from the given blog. This method # pretty-much exists as a helper for setCategory(). def getCategoryList(self): # This returns an array of dictionaries, each with two keys: # 'categoryId' and 'categoryName'. I think it's better to return a # single dictionary, with the category names as keys, and the # corresponding ID as their value. catlist = self.s.metaWeblog.getCategoryList (self.blogID, self.username, self.password) catDict = {} for item in catlist: catDict[item['categoryName']] = item['categoryId'] return (catDict) # This method will change the category of an existing post. Requires the # post ID, and the category name. Note while Movable Type supports # multiple blog categories for a post, this method can only set one # category. def setCategory(self, postID, category): if self.__categoryList == None: # We're doing this lazy style - fetch the category list if we haven't already. self.__categoryList = self.getCategoryList() categoryID = 0 if self.__categoryList.has_key(category): categoryID = self.__categoryList[category] else: return "ERROR: Could not find category '%s' in target blog." % category # Construct a dictionary to send for this call. catDict = {} catDict['categoryName'] = category catDict['categoryId'] = categoryID catDict['isPrimary'] = 1 categories = [] categories.append (catDict) co = self.s.metaWeblog.setPostCategories (postID, self.username, self.password, categories) return co