From e9b63210f902269b1505726b1f0fb05864c68511 Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Sun, 10 Feb 2013 08:00:32 +0100 Subject: [PATCH] Add encoding, inline images and guid tags to RSS output. --- doc/changelog.txt | 1 + dosagelib/events.py | 8 ++++--- dosagelib/rss.py | 52 ++++++++++++++++++++------------------------- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/doc/changelog.txt b/doc/changelog.txt index b6db2e2b0..647dc3eda 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -10,6 +10,7 @@ Features: Changes: - cmdline: Added the --continue option. +- output: Add encoding, inline images and guid tags to RSS output. Fixes: - comics: Fixed Gunnerkrigcourt comic strip. diff --git a/dosagelib/events.py b/dosagelib/events.py index 0612a9f14..a464840d0 100644 --- a/dosagelib/events.py +++ b/dosagelib/events.py @@ -70,10 +70,12 @@ class RSSEventHandler(EventHandler): def comicDownloaded(self, comic, filename): """Write RSS entry for downloaded comic.""" url = self.getUrlFromFilename(filename) + title = '%s - %s' % (comic, os.path.basename(filename)) + description = '
View Comic' % (url, url) args = ( - '%s - %s' % (comic, os.path.basename(filename)), + title, url, - 'View Comic' % url, + description, util.rfc822date(time.time()) ) @@ -81,7 +83,7 @@ class RSSEventHandler(EventHandler): self.newfile = False self.rss.addItem(*args) else: - self.rss.insertHead(*args) + self.rss.addItem(*args, append=False) def end(self): """Write RSS data to file.""" diff --git a/dosagelib/rss.py b/dosagelib/rss.py index 2fd255113..f813b5361 100644 --- a/dosagelib/rss.py +++ b/dosagelib/rss.py @@ -5,63 +5,57 @@ import xml.dom.minidom import time +from .configuration import App class Feed(object): """Write an RSS feed with comic strip images.""" - def __init__(self, title, link, description, lang='en-us'): + def __init__(self, title, link, description, lang='en-us', encoding="utf-8"): """Initialize RSS writer with given title, link and description.""" + self.encoding = encoding self.rss = xml.dom.minidom.Document() - - rss_root = self.rss.appendChild(self.rss.createElement('rss')) - rss_root.setAttribute('version', '2.0') - - self.channel = rss_root.appendChild(self.rss.createElement('channel')) - + root = self.rss.appendChild(self.rss.createElement('rss')) + root.setAttribute('version', '2.0') + self.channel = root.appendChild(self.rss.createElement('channel')) self.addElement(self.channel, 'title', title) self.addElement(self.channel, 'link', link) self.addElement(self.channel, 'language', lang) self.addElement(self.channel, 'description', description) + self.addElement(self.channel, 'generator', App) def addElement(self, parent, tag, value): """Add an RSS item.""" - return parent.appendChild(self.rss.createElement(tag)).appendChild(self.rss.createTextNode(value)) + elem = self.rss.createElement(tag) + node = self.rss.createTextNode(value) + return parent.appendChild(elem).appendChild(node) - def insertHead(self, title, link, description, date): - """Insert an item head.""" - item = self.rss.createElement('item') - - self.addElement(item, 'title', title) - self.addElement(item, 'link', link) - self.addElement(item, 'description', description) - self.addElement(item, 'pubDate', date) - - elems = self.rss.getElementsByTagName('item') - if elems: - self.channel.insertBefore(item, elems[0]) - else: - self.channel.appendChild(item) - - def addItem(self, title, link, description, date): + def addItem(self, title, link, description, date, append=True): """Insert an item.""" item = self.rss.createElement('item') self.addElement(item, 'title', title) self.addElement(item, 'link', link) self.addElement(item, 'description', description) + self.addElement(item, 'guid', link) self.addElement(item, 'pubDate', date) - self.channel.appendChild(item) + if append: + self.channel.appendChild(item) + else: + elems = self.rss.getElementsByTagName('item') + if elems: + self.channel.insertBefore(item, elems[0]) + else: + self.channel.appendChild(item) def write(self, path): """Write RSS content to file.""" - file = open(path, 'w') - file.write(self.getXML()) - file.close() + with open(path, 'w') as f: + f.write(self.getXML()) def getXML(self): """Get RSS content in XML format.""" - return self.rss.toxml() + return self.rss.toxml(self.encoding) def parseFeed(filename, yesterday):