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 13 additions and 8 deletions
Showing only changes of commit eae90190e1 - Show all commits

View file

@ -58,8 +58,8 @@ class CommandProcessor:
feeds = self.database.find_user_feeds(data['user_id'])
feed_list = ''
for feed in feeds:
feed_list += '* ' + str(feed[0]) + ': ' + feed[1] + '\n'
for count, feed in enumerate(feeds, start=1):
feed_list += '* ' + str(count) + ': ' + feed['url'] + '\n'
self.bot.reply_to(message, 'Your feeds:\n' + feed_list)
@ -97,9 +97,13 @@ class Notifier:
def __init__(self, token: str):
self.bot: TeleBot = TeleBot(token)
def send_updates(self, chat_ids: list[int], updates: list[FeedItem]):
def send_updates(self, chat_ids: list[int], updates: list[FeedItem], feed_title: str):
"""Send notification about new items to the user"""
for chat_id in chat_ids:
self.bot.send_message(
chat_id=chat_id,
text=f'Updates from the {feed_title} feed:'
)
for update in updates:
self.__send_update(chat_id, update)
self.sent_counter += 1

View file

@ -5,15 +5,16 @@ from telegram import Notifier
class UpdateManager:
def __init__(self, database: Database, notifier: Notifier, rss_reader: RssReader) -> None:
self.database = database
self.notifier = notifier
self.rss_reader = rss_reader
self.database: Database = database
self.notifier: Notifier = notifier
self.rss_reader: RssReader = rss_reader
def update(self):
feeds = self.database.find_feeds()
for feed_id, feed_url in feeds:
new_items = self.rss_reader.get_feed(feed_url).items
feed = self.rss_reader.get_feed(feed_url)
new_items = feed.items
old_items_urls = self.database.find_feed_items_urls(feed_id)
diff = self.__calculate_difference(new_items, old_items_urls)
@ -22,7 +23,7 @@ class UpdateManager:
continue
chat_ids = self.database.find_feed_subscribers(feed_id)
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.notifier.send_updates(chat_ids, diff)
self.notifier.send_updates(chat_ids, diff, feed.title)
self.database.update_feed_items(feed_id, new_items)
def __calculate_difference(self, new_items: list[FeedItem], old_items_urls: list[str]) -> list[FeedItem]: