Update manager #23

Merged
Miroslavsckaya merged 20 commits from feature_rss_update into master 2022-07-08 19:23:12 +00:00
2 changed files with 17 additions and 15 deletions
Showing only changes of commit 18f680a9e9 - Show all commits

View file

@ -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)):

View file

@ -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)
skobkin marked this conversation as resolved
Review

Why would you do that instead of just returning ready-for-use list of ID's from the Database?

Why would you do that instead of just returning ready-for-use list of ID's from the `Database`?
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