correct return values in some methods from database.py

This commit is contained in:
mitsuha_s 2022-06-12 13:02:18 +00:00
parent 94d77a42f4
commit 18f680a9e9
2 changed files with 17 additions and 15 deletions

View file

@ -98,27 +98,35 @@ class Database:
return 0 return 0
return int(row[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""" """Return feed subscribers"""
self.cur.execute('SELECT telegram_id FROM users WHERE id IN (SELECT user_id FROM subscriptions WHERE feed_id = ?)', [feed_id]) 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: def find_feeds(self) -> list:
"""Get a list of feeds.""" """Get a list of feeds."""
self.cur.execute('SELECT * FROM feeds') self.cur.execute('SELECT * FROM feeds')
return self.cur.fetchall() 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.""" """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 = ?)', self.cur.execute('SELECT * FROM feeds WHERE id IN (SELECT feed_id FROM subscriptions WHERE user_id = ?)',
[user_id]) [user_id])
return self.cur.fetchall() 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.""" """Get last feed items."""
self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = ?', [feed_id]) self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = ?', [feed_id])
return self.cur.fetchall() 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: def update_feed_items(self, feed_id: int, new_items: list[FeedItem]) -> None:
"""Replace last feed items with a list items that receive.""" """Replace last feed items with a list items that receive."""
for i in range(len(new_items)): for i in range(len(new_items)):

View file

@ -14,31 +14,25 @@ class UpdateManager:
for feed_id, feed_url in feeds: for feed_id, feed_url in feeds:
new_items = self.rss_reader.get_feed(feed_url).items 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: if not diff:
continue continue
chat_ids = self.database.find_feed_subscribers(feed_id) 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.notifier.send_updates(chat_ids, diff)
self.database.update_feed_items(feed_id, new_items) self.database.update_feed_items(feed_id, new_items)
def __calculate_difference(self, new_items: list[FeedItem], old_items: list[tuple]) -> list[FeedItem]: def __calculate_difference(self, new_items: list[FeedItem], old_items_url: list[str]) -> list[FeedItem]:
if not old_items: if not old_items_url:
return new_items return new_items
diff = [] diff = []
urls = list(map(lambda x: x[1], old_items))
for item in new_items: for item in new_items:
if item.url not in urls: if item.url not in old_items_url:
diff.append(item) diff.append(item)
return diff return diff