From 0badec72a9b7c831a9834912c3864720fbb51db8 Mon Sep 17 00:00:00 2001 From: pictuga Date: Sun, 22 Dec 2013 12:44:33 +0100 Subject: [PATCH] Allow iteration over RSS objects Useful for JSON --- feeds.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/feeds.py b/feeds.py index be7d5af..6f06518 100644 --- a/feeds.py +++ b/feeds.py @@ -76,6 +76,27 @@ class FeedBase(object): Base for xml-related classes, which provides simple wrappers around xpath selection and item creation """ + + def __getitem__(self, item): + return getattr(self, item) + + def __setitem__(self, item, value): + setattr(self, item, value) + + def __delitem__(self, item): + delattr(self, item) + + def __iter__(self): + for element in self.dic: + value = self[element] + + if isinstance(value, FeedList): + value = [dict(x) for x in value] + elif isinstance(value, datetime): + value = value.isoformat() + + yield element, value + def xpath(self, path): """ Test xpath rule on xml tree """ return self.root.xpath(path, namespaces=NSMAP) @@ -270,6 +291,7 @@ class FeedParser(FeedBase): itemsClass = 'FeedItem' mimetype = 'application/xml' base = '' + dic = ('title', 'desc', 'items') def __init__(self, xml=None, tag='atom:feed'): if xml is None: @@ -387,6 +409,7 @@ class FeedParserAtom(FeedParser): class FeedItem(FeedBase): timeFormat = '' + dic = ('title', 'link', 'isPermaLink', 'desc', 'content', 'id', 'time', 'updated') def __init__(self, xml=None, tag='atom:feed'): if xml is None: