/usr/lib/python2.7/dist-packages/dosagelib/rss.py is in dosage 2.15-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | # -*- coding: iso-8859-1 -*-
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
# TODO: Not sure if this RSS output is "valid", should be though.
# Might also be nice categorise Comics under one Item
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', encoding="utf-8"):
"""Initialize RSS writer with given title, link and description."""
self.encoding = encoding
self.rss = xml.dom.minidom.Document()
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."""
elem = self.rss.createElement(tag)
node = self.rss.createTextNode(value)
return parent.appendChild(elem).appendChild(node)
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."""
with open(path, 'wb') as f:
f.write(self.getXML())
def getXML(self):
"""Get RSS content in XML format."""
return self.rss.toxml(self.encoding)
def parseFeed(filename, yesterday):
"""Parse an RSS feed and filter only entries that are newer than yesterday."""
dom = xml.dom.minidom.parse(filename)
getText = lambda node, tag: node.getElementsByTagName(tag)[0].childNodes[0].data
getNode = lambda tag: dom.getElementsByTagName(tag)
content = getNode('channel')[0] # Only one channel node
feedTitle = getText(content, 'title')
feedLink = getText(content, 'link')
feedDesc = getText(content, 'description')
feed = Feed(feedTitle, feedLink, feedDesc)
for item in getNode('item'):
itemDate = time.strptime(getText(item, 'pubDate'), '%a, %d %b %Y %H:%M:%S GMT')
if (itemDate > yesterday): # If newer than yesterday
feed.addItem(getText(item, 'title'),
getText(item, 'link'),
getText(item, 'description'),
getText(item, 'pubDate'))
return feed
|