создание креатива постом
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user