From 7f941d29b84afcf131fb82fea82b19435f65e7a2 Mon Sep 17 00:00:00 2001 From: kjwon15 Date: Sun, 6 Oct 2013 00:53:45 +0900 Subject: [PATCH] add function 'get_all_feeds' to FeedList, FeedCategory --- libearth/feedlist.py | 13 +++++++++++++ tests/feedlist_test.py | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/libearth/feedlist.py b/libearth/feedlist.py index c413fb5..0be5247 100644 --- a/libearth/feedlist.py +++ b/libearth/feedlist.py @@ -174,6 +174,16 @@ def __init__(self, title, type=None, text=None, category=None, self.children = [] self.urls = [] # to avoid duplication of feeds for the same category + def get_all_feeds(self): + res = [] + for obj in self.children: + if isinstance(obj, FeedCategory): + res += obj.get_all_feeds() + else: + res.append(obj) + + return list(set(res)) + def insert(self, index, value): if not isinstance(value, FeedTree): raise TypeError('expected an instance of {0.__module__}.' @@ -473,6 +483,9 @@ def make_feed(self, type, title, xml_url, html_url=None, text=None, return feed + def get_all_feeds(self): + return self.feedlist.get_all_feeds() + def convert_from_outline(self, outline_obj): if outline_obj.children: title = outline_obj.title or outline_obj.text diff --git a/tests/feedlist_test.py b/tests/feedlist_test.py index 777eb63..4c6b5e6 100644 --- a/tests/feedlist_test.py +++ b/tests/feedlist_test.py @@ -224,3 +224,30 @@ def test_feedCategory(): #Or valid rfc822 string. feedCategory = FeedCategory('title', 'Mon, 23 Sep 2013 11:49:28 +0900') + + +def test_get_all_feeds(): + feed1 = Feed('rss2', 'feed1', 'http://xmlurl1.com/rss') + feed2 = Feed('rss2', 'feed2', 'http://xmlurl2.com/rss') + feed3 = Feed('rss2', 'feed3', 'http://xmlurl3.com/rss') + + cate1 = FeedCategory('category1') + cate2 = FeedCategory('category2') + + #feeds can be placed on multi category + cate1.append(feed1) + cate1.append(feed2) + cate2.append(feed2) + cate2.append(feed3) + + feedlist = FeedList() + feedlist.append(cate1) + feedlist.append(cate2) + feedlist.append(feed3) + + all_feeds = feedlist.get_all_feeds() + + assert feed1 in all_feeds + assert feed2 in all_feeds + assert feed3 in all_feeds + assert len(all_feeds) == 3