#!/usr/bin/env python """ Convert Between Versions of RSS Author: Sean B. Palmer, inamidst.com License: GPL 2; share and enjoy! """ import cgitb; cgitb.enable() import sys, os, cgi, urllib import tempfile, subprocess form = cgi.FieldStorage() form.__call__ = lambda s: form[s].value class TransformationError(Exception): pass def mktemp(s=None, pfx=None, sfx=None): if pfx is None: pfx = 'xml' if sfx is None: sfx = '.tmp' fd, fn = tempfile.mkstemp(prefix=pfx, suffix=sfx) if s: f = os.fdopen(fd, 'w') f.write(s) f.close() return fn def transform(s, xsltfn): xmlfn = mktemp(s) args = ['--novalid', '-o', '/dev/stdout', xsltfn, xmlfn] kargs = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE} proc = subprocess.Popen(['/usr/bin/xsltproc'] + args, **kargs) stdout = proc.stdout.read() stderr = proc.stderr.read() status = abs(proc.wait()) os.unlink(xmlfn) if not (status > 0): return stdout else: raise TransformationError, "Got error: %r" % stderr def barf(msg): print "Content-Type: text/html" print print ("""\
Sorry, the following error occured:
""" + msg + """
If you believe this message to be in error, please submit feedback using the following form:
{ Maintainer: Sean B. Palmer } """) sys.exit() def rssuri(uri): uri = cgi.escape(urllib.quote(uri)) print "Content-Type: text/html; charset=utf-8" print print ("""\Your feed is available in RSS 1.1 at the following address:
http://inamidst.com/rss1.1/convert?uri=""" + uri + """&to=RSS1.1
Please download it and save it locally so that the bandwidth for this service can be used for other people. Thanks.
{ Maintainer: Sean B. Palmer } """) sys.exit() def convert(): uri = form('uri') if ':' not in uri: raise Exception, "Sorry, only absolute URIs are allowed." if form.has_key('to'): output = form('to') else: output = 'XHTML' if (output == 'RSS1.1URI'): rssuri(uri) if (output == 'RSS1.1'): xsltfn = 'rss1to1.1.xsl' elif (output == 'XHTML'): import validator xsltfn = 'toxhtml.xsl' report = validator.pyreport(uri) validity = report['Summary'] if validity != 'Valid': barf(""" Sorry, your feed is not valid RSS 1.1. Try running it through the RSS 1.1 Validator first. """) else: barf("Can't convert to %s." % output) u = urllib.urlopen(uri) s = u.read() u.close() try: result = transform(s, xsltfn) except TransformationError, e: barf(cgi.escape(str(e))) if form.has_key('mime'): print "Content-Type: %s" % form('mime') elif (output == 'XHTML'): print "Content-Type: text/html" else: print "Content-Type: application/rss+xml" print print result def homepage(): print "Content-Type: text/html; charset=utf-8" print print """\This service can currently convert from RSS 1.0 to RSS 1.1, and from RSS 1.1 to XHTML. It's based on a series of Python and XSLT scripts, and is free to use.