Skip to content

Instantly share code, notes, and snippets.

@jmahmood
Created March 7, 2011 08:48
Show Gist options
  • Save jmahmood/858240 to your computer and use it in GitHub Desktop.
Save jmahmood/858240 to your computer and use it in GitHub Desktop.
A concept class that extends PyRSS2Gen to permit you to include unescaped CDATA characters in RSS feeds.
# This is insecure, and only here for a proof of concept. Your mileage may vary. Et cetra.
import PyRSS2Gen
import datetime
class NoOutput:
def __init__(self):
pass
def publish(self, handler):
pass
class media_thumbnail:
"""Publish a media:thumbnail element"""
def __init__(self, url):
self.url = url
def publish(self, handler):
PyRSS2Gen._element(handler, "media:thumbnail", None, {"url": self.url })
class IPhoneRSS2(PyRSS2Gen.RSSItem):
def __init__(self, **kwargs):
if 'media_thumbnail' in kwargs:
self.media_thumbnail = kwargs['media_thumbnail']
del kwargs['media_thumbnail']
else:
self.media_thumbnail = None
PyRSS2Gen.RSSItem.__init__(self, **kwargs)
def publish(self, handler):
self.d1 = self.description
self.description = NoOutput() #You need to do this since the original class checks for the presence of description
PyRSS2Gen.RSSItem.publish(self, handler)
def publish_extensions(self, handler):
def characters(handler, key, description):
handler._out.write('<%s>< ![CDATA[%s]]></%s>' % (key, description, key))
PyRSS2Gen._opt_element(handler, "media_thumbnail", media_thumbnail(self.media_thumbnail))
characters(handler, "description", self.d1)
# How to use:
rss = PyRSS2Gen.RSS2(
title = "Example Title",
link="http://example.com",
description="Example RSS Output",
lastBuildDate=datetime.datetime.utcnow(),
pubDate=datetime.datetime.utcnow(),
items=[
IPhoneRSS2(
title="Item Title",
description="""<p><b>example</b>text<p><br/>
<p>Where are you going today?</p>
""",
media_thumbnail = "http://example.com/image.jpg",
link="http://example.com",
guid="random_guid_x9129319",
pubDate=datetime.datetime.now()),
]
)
rss.rss_attrs["xmlns:media"] = "http://search.yahoo.com/mrss/"
rss.write_xml(open("test.xml", "w"), "utf-8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment