Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text links filter #6

Merged
merged 7 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[settings]
line_length = 119
line_length = 160
skip=migrations/*.py, node_modules, venv
known_standard_library=typing
multi_line_output=4
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ lint:
flake8 *.py
mypy

fmt:
isort .

dev:
watchmedo auto-restart --patterns '*.py' python bot.py
4 changes: 1 addition & 3 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

import rekognition
import text
from filters import (
ContainsLink, ContainsTelegramContact, ContainsThreeOrMoreEmojies, IsMedia, IsMessageOnBehalfOfChat,
with_default_filters)
from filters import ContainsLink, ContainsTelegramContact, ContainsThreeOrMoreEmojies, IsMedia, IsMessageOnBehalfOfChat, with_default_filters
from helpers import DB_ENABLED, enable_logging, in_production, init_sentry


Expand Down
5 changes: 2 additions & 3 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is autogenerated by pip-compile with python 3.8
# To update, run:
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile dev-requirements.in
#
Expand Down Expand Up @@ -143,7 +143,6 @@ types-emoji==1.2.6
# via -r dev-requirements.in
typing-extensions==3.10.0.0
# via
# boto3-stubs
# flake8-pie
# mypy
watchdog[watchmedo]==2.1.6
Expand Down
29 changes: 25 additions & 4 deletions filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,30 @@
from functools import reduce
from telegram import Message
from telegram.ext import BaseFilter, MessageFilter
from urlextract import URLExtract

import text
from helpers import DB_ENABLED


class HasValidPreviousMessages(MessageFilter):
da-maltsev marked this conversation as resolved.
Show resolved Hide resolved
MIN_PREVIOUS_MESSAGES_COUNT = 3

def filter(self, message: Message) -> bool:
if not DB_ENABLED():
return False
da-maltsev marked this conversation as resolved.
Show resolved Hide resolved

return self.has_no_valid_previous_messages(user_id=message.from_user.id, chat_id=message.chat_id)

@classmethod
def has_no_valid_previous_messages(cls, user_id: int, chat_id: int) -> bool:
from models import LogEntry

messages_count = LogEntry.select().where(
(LogEntry.user_id == user_id),
(LogEntry.chat_id == chat_id),
(LogEntry.action != 'delete'),
).count()
return messages_count < cls.MIN_PREVIOUS_MESSAGES_COUNT


class ChatMessageOnly(MessageFilter):
Expand All @@ -16,6 +37,7 @@ def with_default_filters(*filters: BaseFilter) -> BaseFilter:
"""Apply default filters to the given filter classes"""
default_filters = [
ChatMessageOnly(),
HasValidPreviousMessages(),
]
return reduce(operator.and_, [*default_filters, *filters]) # МАМА Я УМЕЮ ФУНКЦИОНАЛЬНО ПРОГРАММИРОВАТЬ

Expand All @@ -34,14 +56,13 @@ def filter(self, message: Message) -> bool:


class ContainsLink(MessageFilter):
def __init__(self) -> None:
self.extractor = URLExtract()

def filter(self, message: Message) -> bool:
if message.text is None:
return False # type: ignore

return len(self.extractor.find_urls(message.text)) >= 1
entities_types = set([entity.type for entity in message.entities])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Довольно сложно проверить без тестов, надеюсь ты это внимательно потестил

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут точно хорошо протестил

return len(entities_types.intersection({'url', 'text_link'})) != 0


class ContainsThreeOrMoreEmojies(MessageFilter):
Expand Down
5 changes: 1 addition & 4 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
python_version = 3.9
python_version = 3.10
files = *.py
warn_no_return = off
warn_unused_configs = on
Expand All @@ -17,6 +17,3 @@ ignore_missing_imports = on

[mypy-playhouse.*]
ignore_missing_imports = on

[mypy-urlextract.*]
ignore_missing_imports = on
3 changes: 1 addition & 2 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
python-telegram-bot
python-telegram-bot<20
python-dotenv
urlextract
sentry-sdk
peewee
psycopg2-binary
Expand Down
33 changes: 10 additions & 23 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#
# This file is autogenerated by pip-compile with python 3.8
# To update, run:
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile requirements.in
#
anyio==3.3.4
# via httpcore
appdirs==1.4.4
# via urlextract
apscheduler==3.6.3
# via python-telegram-bot
boto3==1.20.5
Expand All @@ -20,26 +18,22 @@ cachetools==4.2.2
# via python-telegram-bot
certifi==2021.5.30
# via
# httpcore
# httpx
# python-telegram-bot
# sentry-sdk
charset-normalizer==2.0.7
# via httpx
emoji==1.6.1
# via -r requirements.in
filelock==3.0.12
# via urlextract
h11==0.12.0
h11==0.14.0
# via httpcore
httpcore==0.13.7
httpcore==0.17.2
# via httpx
httpx==0.20.0
httpx==0.24.1
# via -r requirements.in
idna==3.2
# via
# anyio
# rfc3986
# urlextract
# httpx
jmespath==0.10.0
# via
# boto3
Expand All @@ -52,15 +46,12 @@ python-dateutil==2.8.2
# via botocore
python-dotenv==0.17.1
# via -r requirements.in
python-telegram-bot==13.6
python-telegram-bot==13.15
# via -r requirements.in
pytz==2021.1
pytz==2023.3
# via
# apscheduler
# python-telegram-bot
# tzlocal
rfc3986[idna2008]==1.5.0
# via httpx
s3transfer==0.5.0
# via boto3
sentry-sdk==1.1.0
Expand All @@ -76,12 +67,8 @@ sniffio==1.2.0
# httpx
tornado==6.1
# via python-telegram-bot
tzlocal==2.1
tzlocal==5.0.1
# via apscheduler
uritools==3.0.2
# via urlextract
urlextract==1.3.0
# via -r requirements.in
urllib3==1.26.5
# via
# botocore
Expand Down