refactor(stars_bot): tidy captcha handling
This commit is contained in:
+20
-23
@@ -146,11 +146,7 @@ class TelegramStarsBot:
|
||||
captcha_text = self.config._load_captcha()
|
||||
captcha_answer = self.config._load_captcha_answer()
|
||||
|
||||
if (
|
||||
not captcha_text
|
||||
or not self.config.enable_captcha
|
||||
or not captcha_answer
|
||||
):
|
||||
if not captcha_text or not self.config.enable_captcha or not captcha_answer:
|
||||
if not self.config.enable_captcha:
|
||||
logger.error("Капча выключена. Редактирование темы невозможно.")
|
||||
elif not captcha_answer:
|
||||
@@ -171,12 +167,16 @@ class TelegramStarsBot:
|
||||
thread_text = thread["first_post"].get("post_body", "")
|
||||
|
||||
# Pattern to match the captcha spoiler block
|
||||
captcha_pattern = r'\[spoiler=Капча[^\]]*\]\[CENTER\].*?\[/CENTER\]\[/spoiler\]'
|
||||
captcha_pattern = (
|
||||
r"\[spoiler=Капча[^\]]*\]\[CENTER\].*?\[/CENTER\]\[/spoiler\]"
|
||||
)
|
||||
|
||||
if re.search(captcha_pattern, thread_text, re.DOTALL):
|
||||
# Replace existing captcha text
|
||||
captcha_wrapper = f"[spoiler=Капча (обязательно);align=center][CENTER]{captcha_text}[/CENTER][/spoiler]"
|
||||
updated_text = re.sub(captcha_pattern, captcha_wrapper, thread_text, flags=re.DOTALL)
|
||||
updated_text = re.sub(
|
||||
captcha_pattern, captcha_wrapper, thread_text, flags=re.DOTALL
|
||||
)
|
||||
|
||||
# Check if text actually changed
|
||||
if updated_text == thread_text:
|
||||
@@ -186,24 +186,19 @@ class TelegramStarsBot:
|
||||
return True
|
||||
|
||||
thread_text = updated_text
|
||||
logger.info(
|
||||
f"Обновление капчи в теме {self.config.lolz_thread_id}..."
|
||||
)
|
||||
logger.info(f"Обновление капчи в теме {self.config.lolz_thread_id}...")
|
||||
|
||||
else:
|
||||
# Add new captcha block
|
||||
captcha_wrapper = f"\n\n[spoiler=Капча (обязательно);align=center][CENTER]{captcha_text}[/CENTER][/spoiler]"
|
||||
thread_text += captcha_wrapper
|
||||
logger.info(
|
||||
f"Добавление капчи в тему {self.config.lolz_thread_id}..."
|
||||
)
|
||||
logger.info(f"Добавление капчи в тему {self.config.lolz_thread_id}...")
|
||||
|
||||
await self.lolz_api.edit_thread(
|
||||
thread_id=self.config.lolz_thread_id, post_body=thread_text
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"Тема {self.config.lolz_thread_id} успешно отредактирована."
|
||||
)
|
||||
logger.info(f"Тема {self.config.lolz_thread_id} успешно отредактирована.")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
@@ -233,6 +228,12 @@ class TelegramStarsBot:
|
||||
f"Найден новый пост для обработки: ID {post_id} (от {poster_username})"
|
||||
)
|
||||
|
||||
post_content = post.get("post_body_html") or post.get("post_body")
|
||||
if not post_content:
|
||||
logger.warning(f"У поста {post_id} отсутствует содержимое. Пропускаем.")
|
||||
self.processed_manager.mark_processed(post_id)
|
||||
return
|
||||
|
||||
if self.config.skip_commented:
|
||||
if await self.lolz_api.has_comments(post=post):
|
||||
logger.info(
|
||||
@@ -241,12 +242,6 @@ class TelegramStarsBot:
|
||||
self.processed_manager.mark_processed(post_id)
|
||||
return
|
||||
|
||||
post_content = post.get("post_body_html") or post.get("post_body")
|
||||
if not post_content:
|
||||
logger.warning(f"У поста {post_id} отсутствует содержимое. Пропускаем.")
|
||||
self.processed_manager.mark_processed(post_id)
|
||||
return
|
||||
|
||||
if self.config.enable_captcha:
|
||||
captcha_answer = self.config._load_captcha_answer()
|
||||
if captcha_answer and str(captcha_answer) not in post_content:
|
||||
@@ -413,7 +408,9 @@ class TelegramStarsBot:
|
||||
|
||||
await self.update_captcha()
|
||||
|
||||
logger.info(f"Ожидание {self.config.captcha_check_interval} секунд до следующей проверки капчи...")
|
||||
logger.info(
|
||||
f"Ожидание {self.config.captcha_check_interval} секунд до следующей проверки капчи..."
|
||||
)
|
||||
await asyncio.sleep(self.config.captcha_check_interval)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
|
||||
Reference in New Issue
Block a user