This commit is contained in:
+45
-20
@@ -58,32 +58,57 @@ EDIT_LINKS_MAX_ATTEMPTS = 5
|
||||
EDIT_LINKS_RETRY_INTERVAL_SEC = 2
|
||||
|
||||
|
||||
def published_message_has_links(text: str, entities=None) -> bool:
|
||||
def extract_text_link_urls(entities) -> list:
|
||||
"""URL из entities типа text_link (ответ Telegram Bot API или python-telegram-bot)."""
|
||||
urls = []
|
||||
for ent in entities or []:
|
||||
if isinstance(ent, dict):
|
||||
if ent.get("type") == "text_link" and ent.get("url"):
|
||||
urls.append(ent["url"])
|
||||
elif getattr(ent, "type", None) == "text_link":
|
||||
url = getattr(ent, "url", None)
|
||||
if url:
|
||||
urls.append(url)
|
||||
return urls
|
||||
|
||||
|
||||
def published_message_has_expected_links(
|
||||
entities,
|
||||
expected_urls,
|
||||
*,
|
||||
result_text: str | None = None,
|
||||
min_length: int | None = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Проверка, что в опубликованном caption/text есть кликабельные ссылки.
|
||||
Проверка, что в сообщении есть именно footer-ссылки публикации.
|
||||
|
||||
Telegram в ответе editMessage* отдаёт plain text (без href=) и entities типа text_link.
|
||||
Нельзя проверять «любой text_link» или слова «Инфо»/«Подписка» в тексте:
|
||||
в теле события могут быть свои <a href> из replace_vk_links.
|
||||
"""
|
||||
if entities:
|
||||
for ent in entities:
|
||||
if isinstance(ent, dict):
|
||||
if ent.get("type") == "text_link" and ent.get("url"):
|
||||
return True
|
||||
elif getattr(ent, "type", None) == "text_link" and getattr(ent, "url", None):
|
||||
return True
|
||||
expected_urls = [u for u in (expected_urls or []) if u]
|
||||
if not expected_urls:
|
||||
return True
|
||||
|
||||
if not text:
|
||||
return False
|
||||
if min_length is not None and result_text is not None:
|
||||
if len(result_text) < min_length:
|
||||
return False
|
||||
|
||||
link_markers = (
|
||||
"Оригинал в ВК",
|
||||
"Подписка",
|
||||
"Инфо",
|
||||
"Иду",
|
||||
)
|
||||
return any(marker in text for marker in link_markers)
|
||||
found = set(extract_text_link_urls(entities))
|
||||
return all(url in found for url in expected_urls)
|
||||
|
||||
|
||||
def published_message_has_links(text: str, entities=None, expected_urls=None) -> bool:
|
||||
"""Проверка footer-ссылок; expected_urls обязателен при вызове из publisher."""
|
||||
if expected_urls is not None:
|
||||
return published_message_has_expected_links(
|
||||
entities,
|
||||
expected_urls,
|
||||
result_text=text,
|
||||
min_length=None,
|
||||
)
|
||||
return bool(extract_text_link_urls(entities))
|
||||
|
||||
|
||||
def published_text_has_links(text: str) -> bool:
|
||||
"""Обратная совместимость; предпочтительно published_message_has_links."""
|
||||
"""Устаревший вызов без expected_urls — только для совместимости."""
|
||||
return published_message_has_links(text)
|
||||
|
||||
Reference in New Issue
Block a user