add sending the feed title to the user

This commit is contained in:
mitsuha_s 2022-06-12 22:13:30 +00:00
parent e9a311ac67
commit eae90190e1
2 changed files with 13 additions and 8 deletions

View file

@ -58,8 +58,8 @@ class CommandProcessor:
feeds = self.database.find_user_feeds(data['user_id']) feeds = self.database.find_user_feeds(data['user_id'])
feed_list = '' feed_list = ''
for feed in feeds: for count, feed in enumerate(feeds, start=1):
feed_list += '* ' + str(feed[0]) + ': ' + feed[1] + '\n' feed_list += '* ' + str(count) + ': ' + feed['url'] + '\n'
self.bot.reply_to(message, 'Your feeds:\n' + feed_list) self.bot.reply_to(message, 'Your feeds:\n' + feed_list)
@ -97,9 +97,13 @@ class Notifier:
def __init__(self, token: str): def __init__(self, token: str):
self.bot: TeleBot = TeleBot(token) 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""" """Send notification about new items to the user"""
for chat_id in chat_ids: 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: for update in updates:
self.__send_update(chat_id, update) self.__send_update(chat_id, update)
self.sent_counter += 1 self.sent_counter += 1

View file

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