From 94c1447093df4e179216934c2ed74a9f8c92b7fc Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 12 Jun 2022 17:13:58 +0000 Subject: [PATCH] refactor a bit in rss.py --- database.py | 2 +- rss.py | 18 +++++++----------- update_manager.py | 10 +++++----- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/database.py b/database.py index c08c6b6..66b45e8 100644 --- a/database.py +++ b/database.py @@ -120,7 +120,7 @@ class Database: self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = ?', [feed_id]) return self.cur.fetchall() - def find_feed_items_url(self, feed_id: int) -> list[str]: + def find_feed_items_urls(self, feed_id: int) -> list[str]: """Return urls last feed items""" items = self.find_feed_items(feed_id) if not items: diff --git a/rss.py b/rss.py index c889d98..f2ca995 100644 --- a/rss.py +++ b/rss.py @@ -1,24 +1,20 @@ -import feedparser +from feedparser import FeedParserDict, parse class FeedItem: - def __init__(self, item: feedparser.util.FeedParserDict) -> None: + def __init__(self, item: FeedParserDict) -> None: self.url = item.get('link', '') self.title = item.get('title', '') self.description = item.get('summary', '') self.date = item.get('published', '') class Feed: - def __init__(self, url: str, feed: feedparser.util.FeedParserDict) -> None: + def __init__(self, url: str, feed: FeedParserDict) -> None: self.url = url - self.items = self.__get_items(feed.entries) - - def __get_items(self, items: list[feedparser.util.FeedParserDict]) -> list[FeedItem]: - list_items = [] - for item in items: - list_items.append(FeedItem(item)) - return list_items + self.items = [] + for item in feed: + self.items.append(FeedItem(item)) class RssReader: def get_feed(self, url: str) -> Feed: - return Feed(url, feedparser.parse(url)) + return Feed(url, parse(url)) diff --git a/update_manager.py b/update_manager.py index 0589290..81ac61e 100644 --- a/update_manager.py +++ b/update_manager.py @@ -14,9 +14,9 @@ class UpdateManager: for feed_id, feed_url in feeds: new_items = self.rss_reader.get_feed(feed_url).items - old_items_url = self.database.find_feed_items_url(feed_id) + old_items_urls = self.database.find_feed_items_urls(feed_id) - diff = self.__calculate_difference(new_items, old_items_url) + diff = self.__calculate_difference(new_items, old_items_urls) if not diff: continue @@ -25,14 +25,14 @@ class UpdateManager: self.notifier.send_updates(chat_ids, diff) self.database.update_feed_items(feed_id, new_items) - def __calculate_difference(self, new_items: list[FeedItem], old_items_url: list[str]) -> list[FeedItem]: - if not old_items_url: + def __calculate_difference(self, new_items: list[FeedItem], old_items_urls: list[str]) -> list[FeedItem]: + if not old_items_urls: return new_items diff = [] for item in new_items: - if item.url not in old_items_url: + if item.url not in old_items_urls: diff.append(item) return diff