refactor send_updates method in Notifier class and add __register_request_and_wait method
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
mitsuha_s 2022-07-08 21:49:13 +00:00
parent 5bb83c2a09
commit 8a4bec01c1

View file

@ -90,7 +90,7 @@ class CommandProcessor:
class Notifier: class Notifier:
"""Sends notifications to users about new RSS feed items.""" """Sends notifications to users about new RSS feed items."""
BATCH_LIMIT: int = 29 BATCH_LIMIT: int = 30
sent_counter: int = 0 sent_counter: int = 0
@ -99,7 +99,11 @@ class Notifier:
def send_updates(self, chat_ids: list[int], updates: list[FeedItem], feed_title: str): 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"""
if not updates:
return
for chat_id in chat_ids: for chat_id in chat_ids:
self.__register_request_and_wait()
self.bot.send_message( self.bot.send_message(
chat_id=chat_id, chat_id=chat_id,
text=f'Updates from the {feed_title} feed:' text=f'Updates from the {feed_title} feed:'
@ -107,12 +111,9 @@ class Notifier:
self.sent_counter += 1 self.sent_counter += 1
for update in updates: for update in updates:
self.__register_request_and_wait()
self.__send_update(chat_id, update) self.__send_update(chat_id, update)
self.sent_counter += 1 self.sent_counter += 1
if self.sent_counter >= self.BATCH_LIMIT:
# TODO: probably implement better later
time.sleep(1)
self.sent_counter = 0
def __send_update(self, chat_id: int, update: FeedItem): def __send_update(self, chat_id: int, update: FeedItem):
self.bot.send_message( self.bot.send_message(
@ -121,6 +122,12 @@ class Notifier:
parse_mode='HTML' parse_mode='HTML'
) )
def __register_request_and_wait(self):
if self.sent_counter >= self.BATCH_LIMIT:
# TODO: probably implement better later
time.sleep(1)
self.sent_counter = 0
@staticmethod @staticmethod
def __format_message(item: FeedItem) -> str: def __format_message(item: FeedItem) -> str:
return ( return (