#!/usr/bin/python
"""
ChangeLog to RSS service
l.m.orchard (deus_x@pobox.com) http://www.decafbad.com/blog
Based on http://people.redhat.com/jrb/files/changelog2.py
Share and Enjoy.
"""
import cgi, md5, os, sys, time, urlparse, urllib
import changelog2
from xml.sax.saxutils import escape
from stat import *
from urlparse import urlparse, urlunparse
# Configuation variables
cache_dir='/home/deusx/www/www.decafbad.com/data/cl2rss'
cache_time=3*60*60 # 3 hours
def cl2rss(**kwargs):
clAddr = urlunparse(urlparse(kwargs['cl']))
fin = urllib.urlopen(clAddr)
cl = changelog2.parse(fin)
out = "Content-Type: application/rss+xml\n\n"
out = out + """
%s
%s
%s%s
""" % ('ChangeLog at '+clAddr,
clAddr,
'Conversion of ChangeLog to RSS',
'en/us')
for i in cl:
(when, who, what) = i
title = who
link = ""
desc = "
"+escape(" ".join(what))+"
"
out = out + """
%s
%s
%s
""" % (escape(title), escape(link), escape(desc))
out = out + """
"""
return out
def cacheFunc(func, **kwargs):
# Come up with a cache file name based on the hash of argument values
keys = kwargs.keys()
keys.sort()
m = md5.new()
for k in keys:
# Skip the cache argument
if k != 'cache':
m.update(str(kwargs[k]))
fn = os.path.join(cache_dir, m.hexdigest())
if kwargs['cache'] == '0' or \
not os.path.isfile(fn) or \
(time.time() - os.stat(fn)[ST_MTIME]) > cache_time:
# Caching is disabled, there's no cached output, or the cache
# has expired, so call the function.
out = func(**kwargs)
fout = open(fn, 'w')
fout.write(out)
fout.close()
else:
# The cache file exists and has not expired, so serve it up.
fin = open(fn, 'r')
out = fin.read()
fin.close()
return out
def serveRequest():
fields = cgi.FieldStorage()
if not fields.has_key('cl'):
# Serve up a simple form interface
print "Content-Type: text/html"
print
print """
ChangeLog to RSS service
ChangeLog to RSS
"""
else:
print cacheFunc(cl2rss,
cl=fields.getvalue('cl', ''),
cache=fields.getvalue('cache', '1'))
if __name__ == '__main__':
if os.environ.has_key('SCRIPT_NAME'): serveRequest()