2013-07-14 16:25:49 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from lxml import etree
|
2013-10-08 22:48:05 +00:00
|
|
|
from datetime import datetime
|
|
|
|
import dateutil.parser
|
|
|
|
from dateutil import tz
|
2013-07-14 16:25:49 +00:00
|
|
|
import re
|
|
|
|
|
2013-09-16 22:44:10 +00:00
|
|
|
Element = etree.Element
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
NSMAP = {'atom': 'http://www.w3.org/2005/Atom',
|
|
|
|
'atom03': 'http://purl.org/atom/ns#',
|
|
|
|
'media': 'http://search.yahoo.com/mrss/',
|
|
|
|
'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
|
|
|
|
'slash': 'http://purl.org/rss/1.0/modules/slash/',
|
|
|
|
'dc': 'http://purl.org/dc/elements/1.1/',
|
|
|
|
'content': 'http://purl.org/rss/1.0/modules/content/',
|
|
|
|
'rssfake': 'http://purl.org/rss/1.0/'}
|
|
|
|
|
|
|
|
def load(url):
|
|
|
|
import urllib2
|
|
|
|
d = urllib2.urlopen(url).read()
|
|
|
|
return parse(d)
|
|
|
|
|
|
|
|
def tagNS(tag, nsmap=NSMAP):
|
|
|
|
match = re.search(r'^\{([^\}]+)\}(.*)$', tag)
|
|
|
|
if match:
|
|
|
|
match = match.groups()
|
|
|
|
for (key, url) in nsmap.iteritems():
|
|
|
|
if url == match[0]:
|
|
|
|
return "%s:%s" % (key, match[1].lower())
|
|
|
|
else:
|
|
|
|
match = re.search(r'^([^:]+):([^:]+)$', tag)
|
|
|
|
if match:
|
|
|
|
match = match.groups()
|
|
|
|
if match[0] in nsmap:
|
|
|
|
return "{%s}%s" % (nsmap[match[0]], match[1].lower())
|
|
|
|
return tag
|
|
|
|
|
|
|
|
def innerHTML(xml):
|
|
|
|
return (xml.text or '') + ''.join([etree.tostring(child) for child in xml.iterchildren()])
|
|
|
|
|
|
|
|
def cleanNode(xml):
|
|
|
|
[xml.remove(child) for child in xml.iterchildren()]
|
|
|
|
|
|
|
|
class FeedException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def parse(data):
|
2013-09-15 12:57:37 +00:00
|
|
|
# encoding
|
|
|
|
match = re.search('encoding=["\']?([0-9a-zA-Z-]+)', data[:100])
|
|
|
|
if match:
|
|
|
|
enc = match.groups()[0].lower()
|
|
|
|
data = data.decode(enc, 'ignore').encode(enc)
|
|
|
|
|
|
|
|
# parse
|
2013-09-08 13:48:34 +00:00
|
|
|
parser = etree.XMLParser(recover=True)
|
|
|
|
doc = etree.fromstring(data, parser)
|
|
|
|
|
2013-09-15 12:57:37 +00:00
|
|
|
# rss
|
2013-07-14 16:25:49 +00:00
|
|
|
match = doc.xpath("//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF", namespaces=NSMAP)
|
|
|
|
if len(match):
|
|
|
|
mtable = { 'rdf:rdf': FeedParserRSS, 'channel': FeedParserRSS,
|
|
|
|
'atom03:feed': FeedParserAtom, 'atom:feed': FeedParserAtom }
|
|
|
|
match = match[0]
|
|
|
|
tag = tagNS(match.tag)
|
|
|
|
if tag in mtable:
|
|
|
|
return mtable[tag](doc, tag)
|
|
|
|
|
|
|
|
raise FeedException('unknow feed type')
|
|
|
|
|
|
|
|
class FeedBase(object):
|
2013-09-01 17:00:22 +00:00
|
|
|
"""
|
|
|
|
Base for xml-related classes, which provides simple wrappers around xpath
|
|
|
|
selection and item creation
|
|
|
|
"""
|
2013-07-14 16:25:49 +00:00
|
|
|
def xpath(self, path):
|
|
|
|
""" Test xpath rule on xml tree """
|
|
|
|
return self.root.xpath(path, namespaces=NSMAP)
|
|
|
|
|
|
|
|
def xget(self, path):
|
|
|
|
""" Returns the 1st xpath match """
|
|
|
|
match = self.xpath(path)
|
|
|
|
if len(match):
|
|
|
|
return match[0]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def xval(self, path):
|
|
|
|
""" Returns the .text of the 1st match """
|
|
|
|
match = self.xget(path)
|
|
|
|
if match is not None:
|
2013-10-12 21:36:43 +00:00
|
|
|
return match.text or ""
|
2013-07-14 16:25:49 +00:00
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def xgetCreate(self, table):
|
|
|
|
""" Returns an element, and creates it when not present """
|
|
|
|
tag = table[self.tag]
|
|
|
|
match = self.xget(tag)
|
|
|
|
if match is not None:
|
|
|
|
return match
|
|
|
|
else:
|
|
|
|
element = etree.Element(tagNS(tag))
|
|
|
|
self.root.append(element)
|
|
|
|
return element
|
|
|
|
|
2013-10-01 17:52:21 +00:00
|
|
|
def xdel(self, path):
|
|
|
|
match = self.xget(path)
|
|
|
|
if match is not None:
|
|
|
|
return match.getparent().remove(match)
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
def tostring(self, **k):
|
|
|
|
""" Returns string using lxml. Arguments passed to tostring """
|
|
|
|
return etree.tostring(self.xml, pretty_print=True, **k)
|
|
|
|
|
2013-09-01 16:52:07 +00:00
|
|
|
class FeedDescriptor(object):
|
2013-09-01 17:00:22 +00:00
|
|
|
"""
|
|
|
|
Descriptor which gives off elements based on "self.getName" and
|
|
|
|
"self.setName" as getter/setters. Looks far better, and avoids duplicates
|
|
|
|
"""
|
2013-09-01 16:52:07 +00:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
2013-11-16 18:05:57 +00:00
|
|
|
self.nname = name[0].upper() + name[1:]
|
2013-09-01 16:52:07 +00:00
|
|
|
|
|
|
|
def __get__(self, instance, owner):
|
2013-11-16 18:05:57 +00:00
|
|
|
getter = getattr(instance, 'get%s' % self.nname)
|
2013-09-01 16:52:07 +00:00
|
|
|
return getter()
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2013-11-16 18:05:57 +00:00
|
|
|
setter = getattr(instance, 'set%s' % self.nname)
|
2013-09-01 16:52:07 +00:00
|
|
|
return setter(value)
|
|
|
|
|
2013-10-01 17:52:21 +00:00
|
|
|
def __delete__(self, instance):
|
2013-11-16 18:05:57 +00:00
|
|
|
deleter = getattr(instance, 'del%s' % self.nname)
|
2013-10-01 17:52:21 +00:00
|
|
|
return deleter()
|
|
|
|
|
2013-10-08 22:48:05 +00:00
|
|
|
class FeedTime(FeedDescriptor):
|
|
|
|
def __get__(self, instance, owner):
|
2013-11-16 18:05:57 +00:00
|
|
|
getter = getattr(instance, 'get%s' % self.nname)
|
2013-10-08 22:48:05 +00:00
|
|
|
raw = getter()
|
|
|
|
try:
|
|
|
|
time = parseTime(raw)
|
|
|
|
return time
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
|
|
|
try:
|
|
|
|
time = parseTime(value)
|
|
|
|
raw = time.strftime(instance.timeFormat)
|
2013-11-16 18:05:57 +00:00
|
|
|
setter = getattr(instance, 'set%s' % self.nname)
|
2013-10-08 22:48:05 +00:00
|
|
|
return setter(raw)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def parseTime(value):
|
|
|
|
if isinstance(value, basestring):
|
|
|
|
if re.match(r'^[0-9]+$', value):
|
|
|
|
return datetime.fromtimestamp(int(value), tz.tzutc())
|
|
|
|
else:
|
|
|
|
return dateutil.parser.parse(value, tzinfos=tz.tzutc)
|
|
|
|
elif isinstance(value, int):
|
|
|
|
return datetime.fromtimestamp(value, tz.tzutc())
|
|
|
|
elif isinstance(value, datetime):
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2013-09-01 16:52:07 +00:00
|
|
|
class FeedList(object):
|
2013-09-01 17:00:22 +00:00
|
|
|
"""
|
|
|
|
Class to map a list of xml elements against a list of matching objects,
|
|
|
|
while avoiding to recreate the same matching object over and over again. So
|
|
|
|
as to avoid extra confusion, list's elements are called "children" here, so
|
|
|
|
as not to use "items", which is already in use in RSS/Atom related code.
|
|
|
|
|
|
|
|
Comes with its very own descriptor.
|
|
|
|
"""
|
2013-09-16 22:44:10 +00:00
|
|
|
def __init__(self, parent, getter, tag, childClass):
|
|
|
|
self.parent = parent
|
2013-09-01 16:52:07 +00:00
|
|
|
self.getter = getter
|
|
|
|
self.childClass = childClass
|
|
|
|
self.tag = tag
|
|
|
|
self._children = {} # id(xml) => FeedItem
|
|
|
|
|
|
|
|
def getChildren(self):
|
|
|
|
children = self.getter()
|
|
|
|
out = []
|
|
|
|
for child in children:
|
|
|
|
if id(child) in self._children:
|
|
|
|
out.append(self._children[id(child)])
|
|
|
|
else:
|
|
|
|
new = self.childClass(child, self.tag)
|
|
|
|
self._children[id(child)] = new
|
|
|
|
out.append(new)
|
|
|
|
return out
|
|
|
|
|
2013-09-16 22:44:10 +00:00
|
|
|
def append(self, cousin=None):
|
|
|
|
new = self.childClass(tag=self.tag)
|
|
|
|
self.parent.root.append(new.xml)
|
|
|
|
self._children[id(new.xml)] = new
|
|
|
|
|
2013-10-12 21:36:43 +00:00
|
|
|
if cousin is None:
|
|
|
|
return new
|
|
|
|
|
2013-09-16 22:44:10 +00:00
|
|
|
for key in self.childClass.__dict__:
|
|
|
|
if key[:3] == 'set':
|
|
|
|
attr = key[3:].lower()
|
|
|
|
if hasattr(cousin, attr):
|
|
|
|
setattr(new, attr, getattr(cousin, attr))
|
|
|
|
elif attr in cousin:
|
|
|
|
setattr(new, attr, cousin[attr])
|
|
|
|
|
|
|
|
return new
|
|
|
|
|
2013-09-01 16:52:07 +00:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.getChildren()[key]
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
child = self.getter()[key]
|
|
|
|
if id(child) in self._children:
|
|
|
|
self._children[id(child)].remove()
|
|
|
|
del self._children[id(child)]
|
|
|
|
else:
|
|
|
|
child.getparent().remove(child)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.getter())
|
|
|
|
|
|
|
|
class FeedListDescriptor(object):
|
2013-09-01 17:00:22 +00:00
|
|
|
"""
|
|
|
|
Descriptor for FeedList
|
|
|
|
"""
|
2013-09-01 16:52:07 +00:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
self.items = {} # id(instance) => FeedList
|
|
|
|
|
2013-09-16 22:44:10 +00:00
|
|
|
def __get__(self, instance, owner=None):
|
2013-09-01 16:52:07 +00:00
|
|
|
key = id(instance)
|
|
|
|
if key in self.items:
|
|
|
|
return self.items[key]
|
|
|
|
else:
|
|
|
|
getter = getattr(instance, 'get%s' % self.name.title())
|
2013-09-16 22:44:10 +00:00
|
|
|
className = globals()[getattr(instance, '%sClass' % self.name)]
|
|
|
|
self.items[key] = FeedList(instance, getter, instance.tag, className)
|
2013-09-01 16:52:07 +00:00
|
|
|
return self.items[key]
|
|
|
|
|
2013-09-16 22:44:10 +00:00
|
|
|
def __set__(self, instance, value):
|
|
|
|
feedlist = self.__get__(instance)
|
|
|
|
[x.remove() for x in [x for x in f.items]]
|
|
|
|
[feedlist.append(x) for x in value]
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
class FeedParser(FeedBase):
|
2013-09-16 22:44:10 +00:00
|
|
|
itemsClass = 'FeedItem'
|
2013-07-14 16:25:49 +00:00
|
|
|
mimetype = 'application/xml'
|
2013-09-16 22:44:10 +00:00
|
|
|
base = '<?xml?>'
|
2013-07-14 16:25:49 +00:00
|
|
|
|
2013-09-16 22:44:10 +00:00
|
|
|
def __init__(self, xml=None, tag='atom:feed'):
|
|
|
|
if xml is None:
|
|
|
|
xml = etree.fromstring(self.base[tag])
|
2013-07-14 16:25:49 +00:00
|
|
|
self.xml = xml
|
|
|
|
self.root = self.xml.xpath("//atom03:feed|//atom:feed|//channel|//rssfake:channel", namespaces=NSMAP)[0]
|
|
|
|
self.tag = tag
|
|
|
|
|
|
|
|
def getTitle(self):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def setTitle(self, value):
|
|
|
|
pass
|
|
|
|
|
2013-10-01 17:52:21 +00:00
|
|
|
def delTitle(self):
|
|
|
|
self.title = ""
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
|
|
|
|
def getDesc(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setDesc(self, value):
|
|
|
|
pass
|
|
|
|
|
2013-10-01 17:52:21 +00:00
|
|
|
def delDesc(self):
|
|
|
|
self.desc = ""
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
|
|
|
|
def getItems(self):
|
|
|
|
return []
|
|
|
|
|
2013-09-01 16:52:07 +00:00
|
|
|
title = FeedDescriptor('title')
|
|
|
|
description = desc = FeedDescriptor('desc')
|
|
|
|
items = FeedListDescriptor('items')
|
2013-07-14 16:25:49 +00:00
|
|
|
|
|
|
|
class FeedParserRSS(FeedParser):
|
|
|
|
"""
|
|
|
|
RSS Parser
|
|
|
|
"""
|
2013-09-16 22:44:10 +00:00
|
|
|
itemsClass = 'FeedItemRSS'
|
2013-07-14 16:25:49 +00:00
|
|
|
mimetype = 'application/rss+xml'
|
2013-09-16 22:44:10 +00:00
|
|
|
base = { 'rdf:rdf': '<?xml version="1.0" encoding="utf-8"?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/"><channel rdf:about="http://example.org/rss.rdf"></channel></rdf:RDF>',
|
|
|
|
'channel': '<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel></channel></rss>'}
|
2013-07-14 16:25:49 +00:00
|
|
|
|
|
|
|
def getTitle(self):
|
|
|
|
return self.xval('rssfake:title|title')
|
|
|
|
|
|
|
|
def setTitle(self, value):
|
2013-10-01 17:52:21 +00:00
|
|
|
if not value:
|
|
|
|
return self.xdel('rssfake:title|title')
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
table = { 'rdf:rdf': 'rssfake:title',
|
|
|
|
'channel': 'title'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
|
|
|
def getDesc(self):
|
2013-10-01 17:52:21 +00:00
|
|
|
print 'YEAH'
|
2013-07-14 16:25:49 +00:00
|
|
|
return self.xval('rssfake:description|description')
|
|
|
|
|
|
|
|
def setDesc(self, value):
|
2013-10-01 17:52:21 +00:00
|
|
|
if not value:
|
|
|
|
return self.xdel('rssfake:description|description')
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
table = { 'rdf:rdf': 'rssfake:description',
|
|
|
|
'channel': 'description'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
|
|
|
def getItems(self):
|
|
|
|
return self.xpath('rssfake:item|item')
|
|
|
|
|
|
|
|
class FeedParserAtom(FeedParser):
|
|
|
|
"""
|
|
|
|
Atom Parser
|
|
|
|
"""
|
2013-09-16 22:44:10 +00:00
|
|
|
itemsClass = 'FeedItemAtom'
|
2013-07-14 16:25:49 +00:00
|
|
|
mimetype = 'application/atom+xml'
|
2013-09-16 22:44:10 +00:00
|
|
|
base = { 'atom:feed': '<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"></feed>',
|
|
|
|
'atom03:feed': '<?xml version="1.0" encoding="utf-8"?><feed version="0.3" xmlns="http://purl.org/atom/ns#"></feed>'}
|
2013-07-14 16:25:49 +00:00
|
|
|
|
|
|
|
def getTitle(self):
|
|
|
|
return self.xval('atom:title|atom03:title')
|
|
|
|
|
|
|
|
def setTitle(self, value):
|
2013-10-01 17:52:21 +00:00
|
|
|
if not value:
|
|
|
|
return self.xval('atom:title|atom03:title')
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
table = { 'atom:feed': 'atom:title',
|
|
|
|
'atom03:feed': 'atom03:title'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
|
|
|
def getDesc(self):
|
|
|
|
return self.xval('atom:subtitle|atom03:subtitle')
|
|
|
|
|
|
|
|
def setDesc(self, value):
|
2013-10-01 17:52:21 +00:00
|
|
|
if not value:
|
|
|
|
return self.xdel('atom:subtitle|atom03:subtitle')
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
table = { 'atom:feed': 'atom:subtitle',
|
|
|
|
'atom03:feed': 'atom03:subtitle'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
|
|
|
def getItems(self):
|
|
|
|
return self.xpath('atom:entry|atom03:entry')
|
|
|
|
|
|
|
|
class FeedItem(FeedBase):
|
2013-10-08 22:48:05 +00:00
|
|
|
timeFormat = ''
|
|
|
|
|
2013-09-16 22:44:10 +00:00
|
|
|
def __init__(self, xml=None, tag='atom:feed'):
|
|
|
|
if xml is None:
|
|
|
|
xml = Element(tagNS(self.base[tag]))
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
self.root = self.xml = xml
|
|
|
|
self.tag = tag
|
|
|
|
|
|
|
|
def getTitle(self):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def setTitle(self):
|
|
|
|
pass
|
|
|
|
|
2013-10-01 17:52:21 +00:00
|
|
|
def delTitle(self):
|
|
|
|
self.title = ""
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
|
2013-09-16 22:45:15 +00:00
|
|
|
def getLink(self):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def setLink(self, value):
|
|
|
|
pass
|
|
|
|
|
2013-10-01 17:52:21 +00:00
|
|
|
def delLink(self):
|
|
|
|
pass
|
|
|
|
|
2013-09-16 22:45:15 +00:00
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
def getDesc(self):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def setDesc(self, value):
|
|
|
|
pass
|
|
|
|
|
2013-10-01 17:52:21 +00:00
|
|
|
def delDesc(self):
|
|
|
|
self.desc = ""
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
|
|
|
|
def getContent(self):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def setContent(self, value):
|
|
|
|
pass
|
|
|
|
|
2013-10-01 17:52:21 +00:00
|
|
|
def delContent(self):
|
|
|
|
self.content = ""
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
|
2013-10-12 21:36:00 +00:00
|
|
|
def getId(self):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def setId(self, value):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def delId(self):
|
|
|
|
self.id = ""
|
|
|
|
|
|
|
|
|
2013-10-08 22:48:05 +00:00
|
|
|
def getTime(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def setTime(self, value):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def delTime(self):
|
|
|
|
self.time = None
|
|
|
|
|
|
|
|
|
|
|
|
def getUpdated(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def setUpdated(self, value):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def delUpdated(self):
|
|
|
|
self.updated = None
|
|
|
|
|
2013-09-01 16:52:07 +00:00
|
|
|
title = FeedDescriptor('title')
|
|
|
|
link = FeedDescriptor('link')
|
|
|
|
description = desc = FeedDescriptor('desc')
|
|
|
|
content = FeedDescriptor('content')
|
2013-10-12 21:36:00 +00:00
|
|
|
id = FeedDescriptor('id')
|
2013-10-08 22:48:05 +00:00
|
|
|
time = FeedTime('time')
|
|
|
|
updated = FeedTime('updated')
|
2013-07-14 16:25:49 +00:00
|
|
|
|
2013-10-01 17:45:54 +00:00
|
|
|
def pushContent(self, value):
|
2013-09-25 10:18:22 +00:00
|
|
|
if not self.desc and self.content:
|
|
|
|
self.desc = self.content
|
|
|
|
|
2013-10-01 17:45:54 +00:00
|
|
|
self.content = value
|
2013-09-25 10:18:22 +00:00
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
def remove(self):
|
|
|
|
self.xml.getparent().remove(self.xml)
|
|
|
|
|
|
|
|
class FeedItemRSS(FeedItem):
|
2013-10-08 22:48:05 +00:00
|
|
|
timeFormat = '%a, %d %b %Y %H:%M:%S %Z'
|
2013-09-16 22:44:10 +00:00
|
|
|
base = { 'rdf:rdf': 'rssfake:item',
|
|
|
|
'channel': 'item'}
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
def getTitle(self):
|
|
|
|
return self.xval('rssfake:title|title')
|
|
|
|
|
|
|
|
def setTitle(self, value):
|
2013-10-01 17:52:21 +00:00
|
|
|
if not value:
|
|
|
|
return self.xdel('rssfake:title|title')
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
table = { 'rdf:rdf': 'rssfake:title',
|
|
|
|
'channel': 'title'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
|
|
|
def getLink(self):
|
|
|
|
return self.xval('rssfake:link|link')
|
|
|
|
|
|
|
|
def setLink(self, value):
|
|
|
|
table = { 'rdf:rdf': 'rssfake:link',
|
|
|
|
'channel': 'link'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
|
|
|
def getDesc(self):
|
|
|
|
return self.xval('rssfake:description|description')
|
|
|
|
|
|
|
|
def setDesc(self, value):
|
2013-10-01 17:52:21 +00:00
|
|
|
if not value:
|
|
|
|
return self.xdel('rssfake:description|description')
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
table = { 'rdf:rdf': 'rssfake:description',
|
|
|
|
'channel': 'description'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
|
|
|
def getContent(self):
|
|
|
|
return self.xval('content:encoded')
|
|
|
|
|
|
|
|
def setContent(self, value):
|
2013-10-01 17:52:21 +00:00
|
|
|
if not value:
|
2013-10-02 09:33:40 +00:00
|
|
|
return self.xdel('content:encoded')
|
2013-10-01 17:52:21 +00:00
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
table = { 'rdf:rdf': 'content:encoded',
|
|
|
|
'channel': 'content:encoded'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
2013-10-08 22:48:05 +00:00
|
|
|
|
2013-10-12 21:36:00 +00:00
|
|
|
def getId(self):
|
|
|
|
return self.xval('rssfake:guid|guid')
|
|
|
|
|
|
|
|
def setId(self, value):
|
|
|
|
if not value:
|
|
|
|
return self.xdel('rssfake:guid|guid')
|
|
|
|
|
|
|
|
table = { 'rdf:rdf': 'rssfake:guid',
|
|
|
|
'channel': 'guid'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
2013-10-08 22:48:05 +00:00
|
|
|
def getTime(self):
|
|
|
|
return self.xval('rssfake:pubDate|pubDate')
|
|
|
|
|
|
|
|
def setTime(self, value):
|
|
|
|
if not value:
|
|
|
|
return self.xdel('rssfake:pubDate|pubDate')
|
|
|
|
|
|
|
|
table = { 'rdf:rdf': 'rssfake:pubDate',
|
|
|
|
'channel': 'pubDate'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
class FeedItemAtom(FeedItem):
|
2013-10-08 22:48:05 +00:00
|
|
|
timeFormat = '%Y-%m-%dT%H:%M:%SZ'
|
2013-09-16 22:44:10 +00:00
|
|
|
base = { 'atom:feed': 'atom:entry',
|
|
|
|
'atom03:feed': 'atom03:entry'}
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
def getTitle(self):
|
|
|
|
return self.xval('atom:title|atom03:title')
|
|
|
|
|
|
|
|
def setTitle(self, value):
|
2013-10-01 17:52:21 +00:00
|
|
|
if not value:
|
|
|
|
return self.xdel('atom:title|atom03:title')
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
table = { 'atom:feed': 'atom:title',
|
|
|
|
'atom03:feed': 'atom03:title'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
|
|
|
def getLink(self):
|
2013-11-03 12:38:56 +00:00
|
|
|
return self.xget('atom:link/@href|atom03:link/@href')
|
2013-07-14 16:25:49 +00:00
|
|
|
|
|
|
|
def setLink(self, value):
|
|
|
|
table = { 'atom:feed': 'atom:link',
|
|
|
|
'atom03:feed': 'atom03:link'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.attrib['href'] = value
|
|
|
|
|
|
|
|
|
|
|
|
def getDesc(self):
|
|
|
|
# default "type" is "text"
|
|
|
|
element = self.xget('atom:summary|atom03:summary')
|
|
|
|
if element is not None:
|
|
|
|
return innerHTML(element)
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def setDesc(self, value):
|
2013-10-01 17:52:21 +00:00
|
|
|
if not value:
|
|
|
|
return self.xdel('atom:summary|atom03:summary')
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
table = { 'atom:feed': 'atom:summary',
|
|
|
|
'atom03:feed': 'atom03:summary'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
if element.attrib.get('type', '') == 'xhtml':
|
|
|
|
cleanNode(element)
|
|
|
|
element.attrib['type'] = 'html'
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
|
|
|
def getContent(self):
|
|
|
|
element = self.xget('atom:content|atom03:content')
|
|
|
|
if element is not None:
|
|
|
|
return innerHTML(element)
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def setContent(self, value):
|
2013-10-01 17:52:21 +00:00
|
|
|
if not value:
|
|
|
|
return self.xdel('atom:content|atom03:content')
|
|
|
|
|
2013-07-14 16:25:49 +00:00
|
|
|
table = { 'atom:feed': 'atom:content',
|
|
|
|
'atom03:feed': 'atom03:content'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
if element.attrib.get('type', '') == 'xhtml':
|
|
|
|
cleanNode(element)
|
|
|
|
element.attrib['type'] = 'html'
|
|
|
|
element.text = value
|
2013-10-08 22:48:05 +00:00
|
|
|
|
2013-10-12 21:36:00 +00:00
|
|
|
|
|
|
|
def getId(self):
|
|
|
|
return self.xval('atom:id|atom03:id')
|
|
|
|
|
|
|
|
def setId(self, value):
|
|
|
|
if not value:
|
|
|
|
return self.xdel('atom:id|atom03:id')
|
|
|
|
|
|
|
|
table = { 'atom:feed': 'atom:id',
|
|
|
|
'atom03:feed': 'atom03:id'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
2013-10-08 22:48:05 +00:00
|
|
|
def getTime(self):
|
|
|
|
return self.xval('atom:published|atom03:published')
|
|
|
|
|
|
|
|
def setTime(self, value):
|
|
|
|
if not value:
|
|
|
|
return self.xdel('atom:published|atom03:published')
|
|
|
|
|
|
|
|
table = { 'atom:feed': 'atom:published',
|
|
|
|
'atom03:feed': 'atom03:published'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|
|
|
|
|
|
|
|
|
|
|
|
def getUpdated(self):
|
|
|
|
return self.xval('atom:updated|atom03:updated')
|
|
|
|
|
|
|
|
def setUpdated(self, value):
|
|
|
|
if not value:
|
|
|
|
return self.xdel('atom:updated|atom03:updated')
|
|
|
|
|
|
|
|
table = { 'atom:feed': 'atom:updated',
|
|
|
|
'atom03:feed': 'atom03:updated'}
|
|
|
|
element = self.xgetCreate(table)
|
|
|
|
element.text = value
|