import htmlentitydefs
from xml.sax.saxutils import escape
def valueOrExec(value):
if hasattr(value, 'im_func'):
return value()
return value
def convert_to_ents(txt):
buf = ''
for c in txt:
mat = ''
for x in htmlentitydefs.entitydefs:
a = htmlentitydefs.entitydefs[x].decode('iso-8859-1')
if c == a:
mat = x
continue
if mat: buf += '&%s;' % mat
else: buf += c
return escape(buf)
def RSS(entries, conf={}):
""" package results as RSS """
formatted = """
\t%s
\t%s
\t%s
\t%s
\t%s\n""" % ( conf.get('channel_title', 'Aggregated feed'),
conf.get('channel_link', ''),
conf.get('channel_desc', ''),
conf.get('channel_generator', 'Feedmoot'),
conf.get('channel_lang', '')
)
for x in entries:
title = x.get('title').encode('utf-8')
link = x.get('link').encode('utf-8')
updated = x.get('updated').encode('utf-8')
comments = ''
guid = ''
if x.has_key('guid'): guid = x.get('guid')
elif x.has_key('id'): guid = x.get('id')
elif x.has_key('link'): guid = x.get('link')
guid.encode('utf-8')
summary = ''
if x.has_key('summary'):
summary = x.get('summary')
content = ''
if x.has_key('content'):
content = x.get('content')[0].get('value').encode('utf-8')
author = ''
if x.has_key('author'):
author = x.get('author').encode('utf-8')
cat = ''
if x.has_key('category'):
cat = x.get('category')
formatted += '\t- \n'
formatted += '\t\t%s\n' % convert_to_ents(title)
formatted += '\t\t%s\n' % link
formatted += '\t\t%s\n' % comments
formatted += '\t\t%s\n' % updated
if author:
formatted += '\t\t%s' % author
if cat:
formatted += '\t\t%s\n' % cat
formatted += '\t\t%s\n' % convert_to_ents(guid)
formatted += '\t\t\n' % summary
if content:
formatted += '\t\t%s
]]>\n' % content
formatted += '\t\n'
formatted += '\n'
return formatted