From 18f680a9e97f6c20d1e4a1d2c6d8733d790224ae Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 12 Jun 2022 13:02:18 +0000 Subject: [PATCH] correct return values in some methods from database.py --- database.py | 16 ++++++++++++---- update_manager.py | 16 +++++----------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/database.py b/database.py index 234d88e..c08c6b6 100644 --- a/database.py +++ b/database.py @@ -98,27 +98,35 @@ class Database: return 0 return int(row[0]) - def find_feed_subscribers(self, feed_id: int) -> list: + def find_feed_subscribers(self, feed_id: int) -> list[int]: """Return feed subscribers""" self.cur.execute('SELECT telegram_id FROM users WHERE id IN (SELECT user_id FROM subscriptions WHERE feed_id = ?)', [feed_id]) - return self.cur.fetchall() + subscribers = self.cur.fetchall() + return list(map(lambda x: x[0], subscribers)) def find_feeds(self) -> list: """Get a list of feeds.""" self.cur.execute('SELECT * FROM feeds') return self.cur.fetchall() - def find_user_feeds(self, user_id: int) -> list: + def find_user_feeds(self, user_id: int) -> list[tuple]: """Return a list of feeds the user is subscribed to.""" self.cur.execute('SELECT * FROM feeds WHERE id IN (SELECT feed_id FROM subscriptions WHERE user_id = ?)', [user_id]) return self.cur.fetchall() - def find_feed_items(self, feed_id: int) -> list: + def find_feed_items(self, feed_id: int) -> list[tuple]: """Get last feed items.""" 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]: + """Return urls last feed items""" + items = self.find_feed_items(feed_id) + if not items: + return items + return list(map(lambda x: x[1], items)) + def update_feed_items(self, feed_id: int, new_items: list[FeedItem]) -> None: """Replace last feed items with a list items that receive.""" for i in range(len(new_items)): diff --git a/update_manager.py b/update_manager.py index 3cae06a..0589290 100644 --- a/update_manager.py +++ b/update_manager.py @@ -14,31 +14,25 @@ class UpdateManager: for feed_id, feed_url in feeds: new_items = self.rss_reader.get_feed(feed_url).items - old_items = self.database.find_feed_items(feed_id) + old_items_url = self.database.find_feed_items_url(feed_id) - diff = self.__calculate_difference(new_items, old_items) + diff = self.__calculate_difference(new_items, old_items_url) if not diff: continue chat_ids = self.database.find_feed_subscribers(feed_id) - chat_ids = list(map(lambda x: x[0], chat_ids)) 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: list[tuple]) -> list[FeedItem]: - if not old_items: + def __calculate_difference(self, new_items: list[FeedItem], old_items_url: list[str]) -> list[FeedItem]: + if not old_items_url: return new_items diff = [] - urls = list(map(lambda x: x[1], old_items)) for item in new_items: - if item.url not in urls: + if item.url not in old_items_url: diff.append(item) return diff - - - -