создание креатива постом

This commit is contained in:
Artem Tsyrulnikov
2026-01-06 17:47:31 +03:00
parent 52e65dff05
commit 61f7ed093c
6 changed files with 366 additions and 93 deletions

View File

@@ -47,20 +47,32 @@ MAX_CREATIVE_MEDIA_BYTES = 20 * 1024 * 1024
_INVITE_LINK_HTML = re.compile(r'<a\s+href=["\']https?://t\.me/\+[a-zA-Z0-9_-]+["\']>([^<]*)</a>', re.IGNORECASE)
_INVITE_LINK_PLAIN = re.compile(r'https?://t\.me/\+[a-zA-Z0-9_-]+', re.IGNORECASE)
_INVITE_LINK_REPLACED = re.compile(r'<a\s+class=["\']tg-link["\']>([^<]*)</a>', re.IGNORECASE)
def replace_invite_link_with_tag(text: str) -> str:
"""Replace Telegram invite link (t.me/+xxx) with tracking tag."""
# Проверяем, есть ли уже замененная ссылка
replaced_count = len(_INVITE_LINK_REPLACED.findall(text))
html_count = len(_INVITE_LINK_HTML.findall(text))
plain_count = len(_INVITE_LINK_PLAIN.findall(text))
total = html_count + plain_count
# Сначала удаляем HTML-ссылки из текста, чтобы не считать их дважды
text_without_html_links = _INVITE_LINK_HTML.sub('', text)
plain_count = len(_INVITE_LINK_PLAIN.findall(text_without_html_links))
total = replaced_count + html_count + plain_count
if total == 0:
raise CreativeInviteLinkNotFound()
if total > 1:
raise CreativeMultipleInviteLinks()
# Если ссылка уже заменена, возвращаем текст как есть
if replaced_count == 1:
return text
if html_count == 1:
return _INVITE_LINK_HTML.sub(r'<a class="tg-link">\1</a>', text)
return _INVITE_LINK_PLAIN.sub('<a class="tg-link"></a>', text)