From 6afe8161cddb57513d7da37888842b71fd17a342 Mon Sep 17 00:00:00 2001 From: kotolex Date: Sat, 21 Dec 2024 11:55:17 +0500 Subject: [PATCH] FIX: check for emoji --- requirements/common.in | 3 ++- src/helpers.py | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/requirements/common.in b/requirements/common.in index 5b6a94c..a503768 100644 --- a/requirements/common.in +++ b/requirements/common.in @@ -2,4 +2,5 @@ aiohttp python-dotenv pyTelegramBotAPI schedule -SQLAlchemy \ No newline at end of file +SQLAlchemy +emoji \ No newline at end of file diff --git a/src/helpers.py b/src/helpers.py index 53e0c79..0de35ce 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -3,6 +3,7 @@ from urllib import parse from urllib.error import URLError from urllib.request import urlopen +from emoji import is_emoji from telebot import types from .constants import ( @@ -34,11 +35,22 @@ def is_url_reachable(url: str) -> bool: return False +def is_too_much_emojis(text: str) -> bool: + """ + Проверяет, что первые 15 символов текста содержат много (5) эмодзи, или текст длиннее 8 символов и половина - это + эмодзи + """ + if len(text) < 8: + return False + limit = 5 if len(text) > 15 else len(text) // 2 + return len([1 for e in text[:15] if is_emoji(e)]) >= limit + + def is_spam(message_text: str) -> bool: """ Проверяет текст на спам - сначала проверка смешанного алфавита, потом на константный набор спам-слов """ - if is_mixed(message_text): + if is_mixed(message_text) or is_too_much_emojis(message_text): return True return any(bad_word.word.casefold() in message_text.casefold() for bad_word in session.query(BadWord).all()) @@ -136,8 +148,8 @@ def detect_args(message: types.Message): # Есть цитата, есть аргументы: ищем текст из аргументов. return " ".join(message.text.split()[1:]) elif len(message.text.split()) > 1: - # Нет цитаты, есть аргументы: ищем текст из аргументов. - return " ".join(message.text.split()[1:]) + # Нет цитаты, есть аргументы: ищем текст из аргументов. + return " ".join(message.text.split()[1:]) def fetch_rule(index: int) -> str: