From 27311c0a7f482a7840f8a4848b699937ffbcf71d Mon Sep 17 00:00:00 2001 From: Delitel Date: Tue, 26 Sep 2023 17:06:23 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BA=D1=80=D1=83=D0=B6=D0=BA=D0=BE=D0=B2=20=D1=81?= =?UTF-8?q?=D0=BE=20=D1=81=D1=82=D0=BE=D1=80=D0=BE=D0=BD=D1=8B=20=D0=B2?= =?UTF-8?q?=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sync/vk/handlers/__init__.py | 1 + sync/vk/handlers/circles.py | 42 ++++++++++++++++++++++++++++++++++++ sync/vk/handlers/stickers.py | 2 +- sync/vk/handlers/videos.py | 21 +----------------- sync/vk/utils/__init__.py | 1 + sync/vk/utils/video.py | 17 +++++++++++++++ 6 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 sync/vk/handlers/circles.py create mode 100644 sync/vk/utils/video.py diff --git a/sync/vk/handlers/__init__.py b/sync/vk/handlers/__init__.py index 626192f..e659813 100644 --- a/sync/vk/handlers/__init__.py +++ b/sync/vk/handlers/__init__.py @@ -1,3 +1,4 @@ +from . import circles from . import videos from . import photos from . import stickers diff --git a/sync/vk/handlers/circles.py b/sync/vk/handlers/circles.py new file mode 100644 index 0000000..cb40d7e --- /dev/null +++ b/sync/vk/handlers/circles.py @@ -0,0 +1,42 @@ +from ..core import bot +from vkbottle.user import Message +from ...tg.core import bot as tg +from ..utils import create_vk_link, check_bundle, get_best_quality_video +from ...utils import get_file_from_url +from vkbottle.dispatch.rules import ABCRule + + +class CircleRule(ABCRule[Message]): + """ + Кастомное правило которое будет срабатывать если + будет отправлен кружок + """ + + async def check(self, event: Message): + if event.attachments: + if event.attachments[0].video: + width = event.attachments[0].video.width + height = event.attachments[0].video.width + if width == 480 and height == 480: + return True + + return False + + +@bot.on.message(CircleRule()) +@check_bundle +async def on_circle(message: Message, bundle): + user_info = await bot.api.users.get(message.from_id) + text = f"{user_info[0].first_name} {user_info[0].last_name}\n" + + circle = await get_file_from_url( + await get_best_quality_video(message.attachments[0].video.files) + ) + + circle_message = await tg.send_video_note(chat_id=bundle.tg_id, video_note=circle) + await circle_message.reply( + text, + parse_mode="html", + disable_web_page_preview=True, + disable_notification=True, + ) diff --git a/sync/vk/handlers/stickers.py b/sync/vk/handlers/stickers.py index 1e7c34c..bcb1579 100644 --- a/sync/vk/handlers/stickers.py +++ b/sync/vk/handlers/stickers.py @@ -32,5 +32,5 @@ async def on_stickers(message: Message, bundle): f"{user_info[0].first_name} {user_info[0].last_name}\n", parse_mode="html", disable_web_page_preview=True, - disable_notification=True + disable_notification=True, ) diff --git a/sync/vk/handlers/videos.py b/sync/vk/handlers/videos.py index 4116313..7f826fc 100644 --- a/sync/vk/handlers/videos.py +++ b/sync/vk/handlers/videos.py @@ -1,7 +1,7 @@ from ..core import bot from vkbottle.user import Message from ...tg.core import bot as tg -from ..utils import create_vk_link, check_bundle +from ..utils import create_vk_link, check_bundle, get_best_quality_video from ...utils import get_file_from_url from vkbottle.dispatch.rules import ABCRule from aiogram.types import InputMediaVideo @@ -43,22 +43,3 @@ async def on_video(message: Message, bundle): ) ) await tg.send_media_group(bundle.tg_id, videos) - - -async def get_best_quality_video(files): - qualities = [ - 'mp4_2160', # 4K - 'mp4_1440', # 1440p - 'mp4_1080', # 1080p - 'mp4_720', # 720p - 'mp4_480', # 480p - 'mp4_360', # 360p - 'mp4_240', # 240p - 'mp4_144', # 144p - ] - - for quality in qualities: - if getattr(files, quality) is not None: - return getattr(files, quality) - - return None diff --git a/sync/vk/utils/__init__.py b/sync/vk/utils/__init__.py index b31853b..cc2631b 100644 --- a/sync/vk/utils/__init__.py +++ b/sync/vk/utils/__init__.py @@ -1,2 +1,3 @@ from .links import create_vk_link from .bundle import check_bundle +from .video import get_best_quality_video diff --git a/sync/vk/utils/video.py b/sync/vk/utils/video.py new file mode 100644 index 0000000..77fd355 --- /dev/null +++ b/sync/vk/utils/video.py @@ -0,0 +1,17 @@ +async def get_best_quality_video(files): + qualities = [ + 'mp4_2160', # 4K + 'mp4_1440', # 1440p + 'mp4_1080', # 1080p + 'mp4_720', # 720p + 'mp4_480', # 480p + 'mp4_360', # 360p + 'mp4_240', # 240p + 'mp4_144', # 144p + ] + + for quality in qualities: + if getattr(files, quality) is not None: + return getattr(files, quality) + + return None