Mon, 27 Feb 2006
Adding new messages with e-mail
This new python module allows to write new messages in your mail program and send it to the blog
Following Items are needed to successful blog with mail:
- Subject for the title
- Passphrase to authenticate
- Path with category and filename (no trailing slash!)
- A empty line (as with RFC822 header/body
- The blog body, with markup
This script will do the work. It not finished yet, there are some problems to solve.
#!/usr/bin/python """ Script to check a IMAP folder for specific blog mails to upload to a weblog via xmlrpc """ import getpass import xmlrpclib import imaplib import email.Parser passphrase = "A good passphrase" USERNAME = 'yourname' class IMAPError(Exception): pass class BlogEntry(object): def __init__(self, data=None): self.title = None self.body = None self.path = None self.valid = False if data: self.parse(data) def __repr__(self): return self.title def parse(self, data): parser = email.Parser.Parser() em = parser.parsestr(data) if not em.has_key('Subject'): return content = em.get_payload() if em.is_multipart(): content = content[0] # only use first part content = parser.parsestr(content) # body is a pseudo RFC822 text if not content.get('Passphrase') or not content.get('Path'): return #if not em.get('Passphrase') == passphrase: # return self.title = em.get('Subject') self.path = content.get('Path') if not self.path.startswith('/'): self.path = '/' + self.path if not self.path.endswith('/'): self.path += '/' self.body = content.get_payload() self.valid = True def get_blog_mails(server, mailbox, username, password): m = imaplib.IMAP4(server) status, data = m.login(username, password) if not status.startswith('OK'): raise IMAPError, status status, data = m.select(mailbox) if not status.startswith('OK'): raise IMAPError, status status, data = m.search(None, 'ALL') if not status.startswith('OK'): raise IMAPError, status mail_id_list = data[0].split() result = [] for mailid in mail_id_list: status, data = m.fetch(mailid, '(RFC822)') result.append(data[0][1]) # TODO ast: mark mail as read m.logout() return result class BloggerAPI(object): APPKEY = "070F5B78CF8DF3450B732A53546CCFC6636A4547" def __init__(self, url, username, password): self.server = xmlrpclib.Server(url) self.username = username self.password = password def getUsersBlogs(self): return self.server.blogger.getUsersBlogs(self.APPKEY, self.username, self.password) def newPost(self, blogid, content, publish=1): self.server.blogger.newPost(self.APPKEY, blogid, self.username, self.password, content, publish) def getRecentPosts(self, blogid, numberOfPosts=5): return self.server.blogger.getRecentPosts(self.APPKEY, blogid, self.username, self.password, numberOfPosts) def getUserInfo(self): return self.server.blogger.getUserInfo(self.APPKEY, self.username, self.password) if __name__ == '__main__': password_mail = getpass.getpass("Your mailbox password: ") password_blog = getpass.getpass("Your blog password: ") blogger = BloggerAPI('http://sirius/blog/blog.py/RPC', USERNAME, password_blog) for mail in get_blog_mails("sirius.hogwarts.local", "INBOX.projects.weblog", USERNAME, password_mail): blog = BlogEntry(mail) print blog.title, blog.path try: blogger.newPost(blog.path, blog.title + "\n" + blog.body) except xmlrpclib.Fault, e: print e
posted at: 01:53 | path: /python | permanent link to this entry