[ZILANT] отладка до-редактирования ссылок в пост
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
+24
-21
@@ -14,7 +14,7 @@ from telegram_relay import (
|
||||
EDIT_LINKS_INITIAL_DELAY_SEC,
|
||||
EDIT_LINKS_MAX_ATTEMPTS,
|
||||
EDIT_LINKS_RETRY_INTERVAL_SEC,
|
||||
published_text_has_links,
|
||||
published_message_has_links,
|
||||
)
|
||||
from season_links import subscription_start_link
|
||||
|
||||
@@ -133,10 +133,11 @@ async def download_image(image_url, httpx_client):
|
||||
|
||||
|
||||
async def _edit_event_add_links(httpx_client, has_image, message_id, final_text, entity_label):
|
||||
"""Edit caption/text со ссылками; повтор до появления href= в ответе API."""
|
||||
"""Edit caption/text со ссылками; повтор до подтверждения text_link в ответе API."""
|
||||
await asyncio.sleep(EDIT_LINKS_INITIAL_DELAY_SEC)
|
||||
method = "editMessageCaption" if has_image else "editMessageText"
|
||||
field = "caption" if has_image else "text"
|
||||
entities_field = "caption_entities" if has_image else "entities"
|
||||
api_url = bot_api_method_url(BOT_TOKEN, method)
|
||||
|
||||
logger.info(
|
||||
@@ -172,15 +173,17 @@ async def _edit_event_add_links(httpx_client, has_image, message_id, final_text,
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get('ok'):
|
||||
result_text = (data.get('result') or {}).get(field) or ""
|
||||
if published_text_has_links(result_text):
|
||||
result = data.get('result') or {}
|
||||
result_text = result.get(field) or ""
|
||||
entities = result.get(entities_field) or []
|
||||
if published_message_has_links(result_text, entities):
|
||||
logger.info(
|
||||
f"Ссылки добавлены для {entity_label} "
|
||||
f"(попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS})"
|
||||
)
|
||||
return True
|
||||
logger.warning(
|
||||
f"edit ok, но href= не найден (попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS}) "
|
||||
f"edit ok, но ссылки не обнаружены (попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS}) "
|
||||
f"для {entity_label}"
|
||||
)
|
||||
else:
|
||||
@@ -190,15 +193,15 @@ async def _edit_event_add_links(httpx_client, has_image, message_id, final_text,
|
||||
retry_after = data.get('parameters', {}).get('retry_after', 60)
|
||||
raise Exception(f"RetryAfter:{retry_after}")
|
||||
if error_code == 400 and "not modified" in error_description:
|
||||
logger.warning(
|
||||
f"message is not modified (попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS}) "
|
||||
f"для {entity_label} — повтор"
|
||||
)
|
||||
else:
|
||||
logger.warning(
|
||||
f"API error edit (попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS}) "
|
||||
f"для {entity_label}: {data.get('description')}"
|
||||
logger.info(
|
||||
f"Caption уже содержит ссылки для {entity_label} "
|
||||
f"(message is not modified, попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS})"
|
||||
)
|
||||
return True
|
||||
logger.warning(
|
||||
f"API error edit (попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS}) "
|
||||
f"для {entity_label}: {data.get('description')}"
|
||||
)
|
||||
elif response.status_code == 429:
|
||||
retry_after = 60
|
||||
try:
|
||||
@@ -210,15 +213,15 @@ async def _edit_event_add_links(httpx_client, has_image, message_id, final_text,
|
||||
try:
|
||||
error_description = (response.json().get('description') or '').lower()
|
||||
if "not modified" in error_description:
|
||||
logger.warning(
|
||||
f"message is not modified HTTP 400 (попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS}) "
|
||||
f"для {entity_label} — повтор"
|
||||
)
|
||||
else:
|
||||
logger.warning(
|
||||
f"HTTP 400 edit (попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS}) "
|
||||
f"для {entity_label}: {response.text[:300]}"
|
||||
logger.info(
|
||||
f"Caption уже содержит ссылки для {entity_label} "
|
||||
f"(message is not modified HTTP 400, попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS})"
|
||||
)
|
||||
return True
|
||||
logger.warning(
|
||||
f"HTTP 400 edit (попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS}) "
|
||||
f"для {entity_label}: {response.text[:300]}"
|
||||
)
|
||||
except Exception:
|
||||
logger.warning(
|
||||
f"HTTP 400 edit (попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS}) "
|
||||
|
||||
Reference in New Issue
Block a user