From 7361e449b30dbbf8ea8bf0b243bb9315099b7637 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 20:17:03 +0000 Subject: [PATCH 1/8] create rss module, add RssReader class --- requirements.txt | 1 + rss.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 rss.py diff --git a/requirements.txt b/requirements.txt index 8b2e6e8..5a8b4cb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ idna==3.3 pyTelegramBotAPI==4.5.0 requests==2.27.1 urllib3==1.26.9 +feedparser==6.0.2 \ No newline at end of file diff --git a/rss.py b/rss.py new file mode 100644 index 0000000..b309f20 --- /dev/null +++ b/rss.py @@ -0,0 +1,20 @@ +import feedparser + + +class FeedItem(): + pass + +class Feed(): + pass + +class RssReader(): + def convert_to_FeedItem(self, item: dict) -> FeedItem: + pass + + def get_items(self, items: list) -> list: + pass + + def get_feed(self, url: str) -> Feed: + pass + + From 7e96992c7fef8f71a30b59d58f66335ba5c3a9cb Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 20:18:12 +0000 Subject: [PATCH 2/8] add Feed class --- rss.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rss.py b/rss.py index b309f20..3aa4060 100644 --- a/rss.py +++ b/rss.py @@ -5,7 +5,10 @@ class FeedItem(): pass class Feed(): - pass + def __init__(self, title: str, url: str, items: list) -> None: + self.title = title + self.url = url + self.items = items class RssReader(): def convert_to_FeedItem(self, item: dict) -> FeedItem: From 1dd2ccfc6eda4b2251049c7e1132f81198799432 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 20:25:07 +0000 Subject: [PATCH 3/8] add get_feed method --- rss.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/rss.py b/rss.py index 3aa4060..0dc074e 100644 --- a/rss.py +++ b/rss.py @@ -5,8 +5,7 @@ class FeedItem(): pass class Feed(): - def __init__(self, title: str, url: str, items: list) -> None: - self.title = title + def __init__(self, url: str, items: list) -> None: self.url = url self.items = items @@ -18,6 +17,6 @@ class RssReader(): pass def get_feed(self, url: str) -> Feed: - pass - - + f = feedparser.parse(url) + items = self.get_items(f.entries) + return Feed(url, items) From 669bc48a377e9a1e6337156d86b29fe11a471958 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 20:28:48 +0000 Subject: [PATCH 4/8] add get_items method --- rss.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rss.py b/rss.py index 0dc074e..8f747ce 100644 --- a/rss.py +++ b/rss.py @@ -14,7 +14,10 @@ class RssReader(): pass def get_items(self, items: list) -> list: - pass + list_items = [] + for item in items: + list_items.append(self.convert_to_FeedItem(item)) + return list_items def get_feed(self, url: str) -> Feed: f = feedparser.parse(url) From 51c9754ad88bac1c6d2106fdd56bacb88346db75 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 20:29:51 +0000 Subject: [PATCH 5/8] add FeedItem class --- rss.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rss.py b/rss.py index 8f747ce..f54f9c0 100644 --- a/rss.py +++ b/rss.py @@ -2,7 +2,10 @@ import feedparser class FeedItem(): - pass + def __init__(self, url: str, title: str, description: str) -> None: + self.url = url + self.title = title + self.description = description class Feed(): def __init__(self, url: str, items: list) -> None: From ba2ff845d372500e0178e8473ef812a9edb25377 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 20:38:49 +0000 Subject: [PATCH 6/8] add convert_to_FeedItem method --- rss.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rss.py b/rss.py index f54f9c0..375ea82 100644 --- a/rss.py +++ b/rss.py @@ -14,7 +14,13 @@ class Feed(): class RssReader(): def convert_to_FeedItem(self, item: dict) -> FeedItem: - pass + if 'title' in item: + title = item['title'] + if 'link' in item: + url = item['link'] + if 'summary' in item: + description = item['summary'] + return FeedItem(url, title, description) def get_items(self, items: list) -> list: list_items = [] From fa0632e84df6c7b4b307d4996e6a4ba8d539ddcb Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 23:00:59 +0000 Subject: [PATCH 7/8] make get_items and convert_to_feed_item methods private --- rss.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rss.py b/rss.py index 375ea82..0f1d762 100644 --- a/rss.py +++ b/rss.py @@ -8,12 +8,12 @@ class FeedItem(): self.description = description class Feed(): - def __init__(self, url: str, items: list) -> None: + def __init__(self, url: str, items: list[FeedItem]) -> None: self.url = url self.items = items class RssReader(): - def convert_to_FeedItem(self, item: dict) -> FeedItem: + def __convert_to_feed_item(self, item: dict) -> FeedItem: if 'title' in item: title = item['title'] if 'link' in item: @@ -22,13 +22,13 @@ class RssReader(): description = item['summary'] return FeedItem(url, title, description) - def get_items(self, items: list) -> list: + def __get_items(self, items: list) -> list: list_items = [] for item in items: - list_items.append(self.convert_to_FeedItem(item)) + list_items.append(self.__convert_to_feed_item(item)) return list_items def get_feed(self, url: str) -> Feed: f = feedparser.parse(url) - items = self.get_items(f.entries) + items = self.__get_items(f.entries) return Feed(url, items) From d7d7838eb5964aa9e79efed712bfcf800912c425 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 23:11:23 +0000 Subject: [PATCH 8/8] little codestyle changes --- rss.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rss.py b/rss.py index 0f1d762..8389972 100644 --- a/rss.py +++ b/rss.py @@ -13,6 +13,11 @@ class Feed(): self.items = items class RssReader(): + def get_feed(self, url: str) -> Feed: + f = feedparser.parse(url) + items = self.__get_items(f.entries) + return Feed(url, items) + def __convert_to_feed_item(self, item: dict) -> FeedItem: if 'title' in item: title = item['title'] @@ -28,7 +33,3 @@ class RssReader(): list_items.append(self.__convert_to_feed_item(item)) return list_items - def get_feed(self, url: str) -> Feed: - f = feedparser.parse(url) - items = self.__get_items(f.entries) - return Feed(url, items)