#!/usr/bin/env python2.4 # -*-*- encoding:iso-8859-15 filetype:python expandtab:yes tabstop:4 shiftwidth:4 softtabstop:4 # autoindent:yes -*-*- # # havard@dahle.no GPL (C) 2005 __doc__ = "Usage: pipe the email message to this script to post to your blog" BLURL = "http://www.orakel.ntnu.no/~havardda/blogg/xmlrpc.php" BLOGG = "kibbitz" USRNM = "j_zuma" PSWRD = "drtyscrt" CTGRY = "10" DEBUG = 0 import sys try: import xmlrpclib, time, email from email.Header import decode_header from email.Iterators import typed_subpart_iterator from email.Utils import parsedate from string import join import html_entity_converter # local helper lib: http://www.peterbe.com/plog/html-entity-fixer except ImportError: print "Import Error" sys.exit(100) def striptags(s): # stolen from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440481 # this list is neccesarry because chk() would otherwise not know # that intag in stripTags() is ment, and not a new intag variable in chk(). intag = [False] def chk(c): if intag[0]: intag[0] = (c != '>') return False elif c == '<': intag[0] = True return False return True return ''.join(c for c in s if chk(c)) def post(msg, categories = None): t = decode_header(msg.get("Subject")) topic = join([unicode(i[0], i[1]) for i in t]) parts = typed_subpart_iterator(msg) # loops through "text/*" parts contents = None try: while not contents: p = parts.next() contents = unicode(p.get_payload(decode=1), p.get_content_charset()) if p.get_content_subtype() == "html": #convert from html contents = striptags(contents) except StopIteration: print "Crap! No text message part found" return 100 p = map(html_entity_converter.html_entity_fixer, (topic.encode("latin1"), contents.encode("latin1"))) post = "