/rss1.1/convert.cgi

#!/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 ("""\
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>RSS Conversion Service: Error</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="service.css" />
</head>
<body>
<h1>RSS Conversion Service: Error</h1>
<p>Sorry, the following error occured: </p>

<blockquote>
<p>""" + msg + """</p>
</blockquote>

<p>If you believe this message to be in error, please submit feedback using 
the following form: </p>

<form action="/misc/feedback" method="POST">
  <div>Your email address: <input type="text" name="sender" size="35" /></div>
  <div><textarea cols="65" rows="8" name="body"></textarea></div>
  <div><input type="submit" value="Submit" /></div>
</form>

<address>{ Maintainer:
<a href="http://inamidst.com/sbp/">Sean B. Palmer</a>
}</address>
</body>
</html>
""")
   sys.exit()

def rssuri(uri): 
   uri = cgi.escape(urllib.quote(uri))
   print "Content-Type: text/html; charset=utf-8"
   print 
   print ("""\
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>RSS Conversion Service: RSS 1.0 to RSS 1.1</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="service.css" />
</head>
<body>
<div><a href="./"><img src="./logo" alt="[RSS 1.1]" 
width="87" height="50" class="logo" /></a></div>

<h1>RSS Conversion: RSS 1.0 to RSS 1.1</h1>
<p>Your feed is available in RSS 1.1 at the following address: </p>

<p style="margin-left:2em;"><a 
href="./convert?uri=""" + uri + """&amp;to=RSS1.1"
>http://inamidst.com/rss1.1/convert?uri=""" + uri + """&amp;to=RSS1.1</a>
</p>

<p>Please download it and save it locally so that the bandwidth for this 
service can be used for other people. Thanks.</p>

<address>{ Maintainer:
<a href="http://inamidst.com/sbp/">Sean B. Palmer</a>
}</address>
</body>
</html>
""")
   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 <a 
            href="./validator">the RSS 1.1 Validator</a> 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 """\
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>RSS Feed Conversion Service</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="service.css" />
<script type="text/javascript">
<!-- 
function sf() { 
  document.f.uri.focus(); 
} 
// -->
</script>
</head>
<body onload="sf()">
<h1>RSS Conversion Service</h1>
<p>This service can currently convert from <a 
href="http://purl.org/rss/1.0">RSS 1.0</a> to <a href="./">RSS 1.1</a>, and 
from <a href="./">RSS 1.1</a> to <a href="http://www.w3.org/MarkUp/">XHTML</a>. 
It's based on a series of <a href="http://www.python.org/">Python</a> and <a 
hrref="http://www.w3.org/TR/xslt">XSLT</a> scripts, and is free to use.</p>

<fieldset>
<legend>Convert a Feed</legend>
<form action="./convert" method="GET" name="f">
<div>URI: <input type="text" name="uri" size="35" /> 
   <select name="to">
      <option value="RSS1.1URI">RSS 1.0 to RSS 1.1</option>
      <option value="XHTML">RSS 1.1 to XHTML</option>
   </select>
<input type="submit" value="Convert" />
</form>
</fieldset>

<p>More information: </p>

<ul>
<li>Python source: <a href="/inside/rss1.1/convert">convert.cgi</a></li>
<li>XSLT: <a href="./rss1to1.1.xsl">rss1to1.1.xsl</a>, 
          <a href="./toxhtml.xsl">toxhtml.xsl</a></li>
</ul>

<p>Complementary service: <a href="./validator">RSS 1.1 Feed 
Validator</a>.</p>

<address>{ Maintainer: 
<a href="http://inamidst.com/sbp/">Sean B. Palmer</a>
}</address>
</body>
</html>
"""

def main(): 
   if form.has_key('uri'): 
      convert()
   else: homepage()

if __name__=="__main__": 
   main()