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:
- cmdline: Added the --continue option.
- output: Add encoding, inline images and guid tags to RSS output.
Fixes:
- comics: Fixed Gunnerkrigcourt comic strip.

View file

@ -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 = '<img src="%s"/><br/><a href="%s">View Comic</a>' % (url, url)
args = (
'%s - %s' % (comic, os.path.basename(filename)),
title,
url,
'<a href="%s">View Comic</a>' % 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."""

View file

@ -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)
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):