refactor(bot): add logging and flow tweaks
This commit is contained in:
+26
-7
@@ -114,7 +114,9 @@ class TelegramStarsBot:
|
||||
|
||||
except FloodWait as e:
|
||||
wait_time = getattr(e, "value", getattr(e, "x", 10)) + 2
|
||||
logger.warning(f"FloodWait: необходимо подождать {wait_time} секунд.")
|
||||
logger.warning(
|
||||
f"FloodWait: необходимо подождать {wait_time} секунд."
|
||||
)
|
||||
await asyncio.sleep(wait_time)
|
||||
|
||||
except Exception as e:
|
||||
@@ -125,7 +127,10 @@ class TelegramStarsBot:
|
||||
if attempt < self.config.max_retries - 1:
|
||||
await asyncio.sleep(3 * (attempt + 1))
|
||||
|
||||
return False
|
||||
logger.error(
|
||||
f"Все {self.config.max_retries} попытки отправки звезд в https://t.me/{channel} исчерпаны."
|
||||
)
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Критическая ошибка при отправке звезд: {e}")
|
||||
@@ -136,6 +141,10 @@ class TelegramStarsBot:
|
||||
poster_user_id = post.get("poster_user_id")
|
||||
|
||||
if not post_id or self.processed_manager.is_processed(post_id):
|
||||
if post_id:
|
||||
logger.debug(f"Пост {post_id} уже обработан, пропускаю.")
|
||||
else:
|
||||
logger.warning("Пост без post_id, пропускаю.")
|
||||
return
|
||||
|
||||
logger.info(f"Найден новый пост для обработки: ID {post_id}")
|
||||
@@ -160,20 +169,23 @@ class TelegramStarsBot:
|
||||
self.processed_manager.mark_processed(post_id)
|
||||
return
|
||||
|
||||
logger.info(f"В посте {post_id} найдено ссылок: {len(links)}.")
|
||||
|
||||
successful_reactions = 0
|
||||
for link in links:
|
||||
parsed_link = TelegramLinkExtractor.parse(link)
|
||||
if parsed_link:
|
||||
channel, message_id = parsed_link
|
||||
|
||||
if await self.send_star(channel, message_id):
|
||||
successful_reactions += 1
|
||||
break # stop after first successful reaction
|
||||
|
||||
await asyncio.sleep(1)
|
||||
|
||||
if successful_reactions > 0 and self.config.enable_reply:
|
||||
await asyncio.sleep(self.config.api_delay)
|
||||
|
||||
# Теперь можно отправлять больше 1 сообщения
|
||||
# см. README.md
|
||||
if poster_user_id:
|
||||
replies = self.config.reply_templates
|
||||
|
||||
@@ -182,6 +194,14 @@ class TelegramStarsBot:
|
||||
|
||||
reply_message = f"[userids={poster_user_id};align=left]{random.choice(reply_options)}[/userids]"
|
||||
await self.lolz_api.create_comment(post_id, reply_message)
|
||||
else:
|
||||
logger.warning(
|
||||
f"Пост {post_id}: poster_user_id отсутствует, комментарий не оставлен."
|
||||
)
|
||||
elif successful_reactions == 0:
|
||||
logger.warning(
|
||||
f"Пост {post_id}: ни одна звезда не была отправлена успешно ({len(links)} ссылок)."
|
||||
)
|
||||
|
||||
logger.info(f"Пост {post_id} полностью обработан.")
|
||||
self.processed_manager.mark_processed(post_id)
|
||||
@@ -199,9 +219,8 @@ class TelegramStarsBot:
|
||||
if posts:
|
||||
for post in posts:
|
||||
await self._process_single_post(post)
|
||||
# Next cycle starts from the last page that had posts —
|
||||
# new posts always appear there or on a new page beyond it,
|
||||
# never on earlier pages.
|
||||
if last_page > self.start_page:
|
||||
logger.info(f"Мониторинг продвинулся до страницы {last_page}.")
|
||||
self.start_page = last_page
|
||||
else:
|
||||
logger.info("Новых постов для обработки не найдено.")
|
||||
|
||||
Reference in New Issue
Block a user