#!/usr/bin/env python # # Script to get mail and post to a wordpress account # (C) 2008 Kushal Das. This script is licensed under the # GNU General Public License, version 2 or any later version. # # # ChangeLog: # * 0.1 Kushal Das # - Initial version import email import imaplib import xmlrpclib from ConfigParser import ConfigParser config = ConfigParser() config.readfp(open('.somedata.ini')) class Wordpress: """Implementation for Wordpress server""" def __init__(self, server, username, password): self.server = xmlrpclib.Server(server) self.username = username self.password = password def post(self, content, publish=True): """Post the content""" postid = self.server.metaWeblog.newPost(1, self.username, self.password, content, publish) server = Wordpress(config.get('Details','bloghost'),config.get('Details','bloguser'), config.get('Details','blogpassword')) conn = imaplib.IMAP4_SSL(host=config.get('Details','emailhost')) conn.login(config.get('Details','emailuser'),config.get('Details','emailpassword')) conn.select(readonly=1) (retcode, messages) = conn.search(None, '(UNSEEN)') if retcode == 'OK': for message in messages[0].split(" "): if message != '': mail = conn.fetch(message, 'RFC822') throwaway = conn.store(message,'+FLAGS', '\\Seen') mes = email.message_from_string(mail[1][0][1]) allmessage = mes.get_payload() text = "" if type(allmessage) == type([1,2]): for part in allmessage: smallparts = part.get_payload() if type(smallparts) == type([1,2]): for smallpart in smallparts: if type(smallpart) == type(mes): tmptext = smallpart.get_payload() if tmptext.find("-BEGIN PGP SIGNATURE") == -1: text += tmptext else: if str(smallpart).find("-BEGIN PGP SIGNATURE") == -1: text += str(smallpart) else: if str(smallpart).find("-BEGIN PGP SIGNATURE") == -1: text+=str(smallpart) elif type(allmessage) == type("string"): text = allmessage text = text.replace('\r\n', '\n') content = {'title':unicode(mes['subject']),'description':unicode(text)} server.post(content) conn.close()