#!/usr/bin/env python # # autopylot v1.0 # # This software is released under the GPL. # # Alex Chiang # 27 October 2005 # import os import re import sys import time import xmlrpclib from stat import * blogid = "" # blog id user = "" # blog username passwd = "" # blog password URI = "" # URI to your XML-RPC location posts = "" # Path to your pre-recorded posts class Blog: def should_post(self): ret = False # Get date of most recent post (first arg is blogId) posts = self.server.mt.getRecentPostTitles(blogid, user, passwd, 1) post = posts[0] dateCreated = post['dateCreated'] date = time.strptime(str(dateCreated), "%Y%m%dT%H:%M:%S") # Get today today = time.localtime() # Check the year, date, and month. If any of the three # do not match, then we have not yet posted today, and # so we should continue with our autopost. for i in range(0,3): if date[i] != today[i]: ret = True return ret def get_categories(self): return self.server.mt.getCategoryList(blogid, user, passwd) def post(self, post): ret = False # Post the content content = {'title':post['title'], 'description':post['description']} postid = self.server.metaWeblog.newPost(blogid, user, passwd, content, True) # Modify the post so it has the correct category cats = [] cats.append({'categoryId':post['categories'], 'isPrimary':1}) ret = self.server.mt.setPostCategories(postid, user, passwd, cats) return ret def __init__(self): self.server = xmlrpclib.Server(URI) class Posts: def get_files(self): return self.files def get_post(self, blog): file = self._get_earliest() post = self._parse_post(file, blog) return post def delete(self, post): file = post['file'] os.unlink(file) def _get_earliest(self): # Initialization path = os.path.join(self.path, self.files[0]) self.earliest = path early_mtime = os.stat(path)[ST_MTIME] for file in self.files: path = os.path.join(self.path, file) if os.stat(path)[ST_MTIME] < early_mtime: self.earliest = path early_mtime = os.stat(path)[ST_MTIME] return self.earliest def _parse_post(self, file, blog): f = open(file, 'r') text = f.read() f.close() # Get the post's title title = re.match('title:.*\n?', text) title = title.group() # get text of match title = re.sub('title:\s*', '', title) # strip whitespace title = re.sub('\n*', '', title) # strip trailing newlines text = re.sub('^title:.*\n?', '', text) # remove title line from text # Get the post's category as a text string cat = re.match('category:.*\n?', text) cat = cat.group() cat = re.sub('category:\s*', '', cat) cat = re.sub('\n*', '', cat) text = re.sub('^category:.*\n?', '', text) # Now match the category to the numerical ID expected by the blog cat_id = None blog_cats = blog.get_categories() for blog_cat in blog_cats: if cat == blog_cat['categoryName']: cat_id = blog_cat['categoryId'] break # If we didn't find a match above, we'll just use a default of 1 if cat_id == None: cat_id = 1 # Strip all leading whitespace text = re.sub('^\s*', '', text) # Temporarily keep track of paragraph breaks text = re.sub('\n\n', '

', text) # Get rid of all other newlines text = re.sub('\n', ' ', text) # Turn the paragraph breaks back into newlines text = re.sub('

', '\n\n', text) # Add in a tagline text = text + '\n\nposted by autopylot' post = {} post['file'] = file post['title'] = title post['categories'] = cat_id post['description'] = text return post def __init__(self): self.path = posts self.files = os.listdir(self.path) if __name__ == "__main__": blog = Blog() posts = Posts() # If there are no pre-written posts, exit if len(posts.get_files()) == 0: sys.exit() # If we already posted today, exit if blog.should_post() == False: sys.exit() post = posts.get_post(blog) # If successful, delete the old post if blog.post(post) == True: posts.delete(post)