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