Add encoding, inline images and guid tags to RSS output.

This commit is contained in:
Bastian Kleineidam 2013-02-10 08:00:32 +01:00
parent 77f3d152c0
commit e9b63210f9
3 changed files with 29 additions and 32 deletions

View file

@ -10,6 +10,7 @@ Features:
Changes: Changes:
- cmdline: Added the --continue option. - cmdline: Added the --continue option.
- output: Add encoding, inline images and guid tags to RSS output.
Fixes: Fixes:
- comics: Fixed Gunnerkrigcourt comic strip. - comics: Fixed Gunnerkrigcourt comic strip.

View file

@ -70,10 +70,12 @@ class RSSEventHandler(EventHandler):
def comicDownloaded(self, comic, filename): def comicDownloaded(self, comic, filename):
"""Write RSS entry for downloaded comic.""" """Write RSS entry for downloaded comic."""
url = self.getUrlFromFilename(filename) url = self.getUrlFromFilename(filename)
title = '%s - %s' % (comic, os.path.basename(filename))
description = '<img src="%s"/><br/><a href="%s">View Comic</a>' % (url, url)
args = ( args = (
'%s - %s' % (comic, os.path.basename(filename)), title,
url, url,
'<a href="%s">View Comic</a>' % url, description,
util.rfc822date(time.time()) util.rfc822date(time.time())
) )
@ -81,7 +83,7 @@ class RSSEventHandler(EventHandler):
self.newfile = False self.newfile = False
self.rss.addItem(*args) self.rss.addItem(*args)
else: else:
self.rss.insertHead(*args) self.rss.addItem(*args, append=False)
def end(self): def end(self):
"""Write RSS data to file.""" """Write RSS data to file."""

View file

@ -5,63 +5,57 @@
import xml.dom.minidom import xml.dom.minidom
import time import time
from .configuration import App
class Feed(object): class Feed(object):
"""Write an RSS feed with comic strip images.""" """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.""" """Initialize RSS writer with given title, link and description."""
self.encoding = encoding
self.rss = xml.dom.minidom.Document() self.rss = xml.dom.minidom.Document()
root = self.rss.appendChild(self.rss.createElement('rss'))
rss_root = self.rss.appendChild(self.rss.createElement('rss')) root.setAttribute('version', '2.0')
rss_root.setAttribute('version', '2.0') self.channel = root.appendChild(self.rss.createElement('channel'))
self.channel = rss_root.appendChild(self.rss.createElement('channel'))
self.addElement(self.channel, 'title', title) self.addElement(self.channel, 'title', title)
self.addElement(self.channel, 'link', link) self.addElement(self.channel, 'link', link)
self.addElement(self.channel, 'language', lang) self.addElement(self.channel, 'language', lang)
self.addElement(self.channel, 'description', description) self.addElement(self.channel, 'description', description)
self.addElement(self.channel, 'generator', App)
def addElement(self, parent, tag, value): def addElement(self, parent, tag, value):
"""Add an RSS item.""" """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): def addItem(self, title, link, description, date, append=True):
"""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):
"""Insert an item.""" """Insert an item."""
item = self.rss.createElement('item') item = self.rss.createElement('item')
self.addElement(item, 'title', title) self.addElement(item, 'title', title)
self.addElement(item, 'link', link) self.addElement(item, 'link', link)
self.addElement(item, 'description', description) self.addElement(item, 'description', description)
self.addElement(item, 'guid', link)
self.addElement(item, 'pubDate', date) self.addElement(item, 'pubDate', date)
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) self.channel.appendChild(item)
def write(self, path): def write(self, path):
"""Write RSS content to file.""" """Write RSS content to file."""
file = open(path, 'w') with open(path, 'w') as f:
file.write(self.getXML()) f.write(self.getXML())
file.close()
def getXML(self): def getXML(self):
"""Get RSS content in XML format.""" """Get RSS content in XML format."""
return self.rss.toxml() return self.rss.toxml(self.encoding)
def parseFeed(filename, yesterday): def parseFeed(filename, yesterday):