From 8caffee7bae12c8d9b47f65e5e07ab4cd835f2b9 Mon Sep 17 00:00:00 2001 From: Delitel Date: Sat, 23 Sep 2023 19:06:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=81=D0=B2=D1=8F=D0=B7=D0=BA=D0=B8=20=D1=87=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D0=B2=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=B2=20=D0=B2=D0=B8=D0=B4=D0=B5=20=D0=B4=D0=B5=D0=BA=D0=BE?= =?UTF-8?q?=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sync/vk/handlers/messages.py | 31 +++++++++-------------- sync/vk/handlers/photos.py | 49 ++++++++++++++++-------------------- sync/vk/handlers/stickers.py | 33 ++++++++++-------------- sync/vk/handlers/videos.py | 47 +++++++++++++++------------------- sync/vk/handlers/voices.py | 33 +++++++++--------------- sync/vk/utils/__init__.py | 1 + sync/vk/utils/bundle.py | 18 +++++++++++++ 7 files changed, 97 insertions(+), 115 deletions(-) create mode 100644 sync/vk/utils/bundle.py diff --git a/sync/vk/handlers/messages.py b/sync/vk/handlers/messages.py index 9111afd..939a1fb 100644 --- a/sync/vk/handlers/messages.py +++ b/sync/vk/handlers/messages.py @@ -1,25 +1,18 @@ from ..core import bot from vkbottle.user import Message -from ...db import Sessions -from ...db.models import Conversations -from sqlalchemy import select from ...tg.core import bot as tg -from ..utils import create_vk_link +from ..utils import create_vk_link, check_bundle @bot.on.message() -async def on_message(message: Message): - async with Sessions() as session: - bundle = await session.scalar( - select(Conversations).where(Conversations.vk_id == message.peer_id) - ) - if bundle: - user_info = await bot.api.users.get(message.from_id) - await tg.send_message( - bundle.tg_id, - f"{user_info[0].first_name} {user_info[0].last_name}\n" - + "‾" * 10 - + f"\n{message.text}", - parse_mode="html", - disable_web_page_preview=True, - ) +@check_bundle +async def on_message(message: Message, bundle): + user_info = await bot.api.users.get(message.from_id) + await tg.send_message( + bundle.tg_id, + f"{user_info[0].first_name} {user_info[0].last_name}\n" + + "‾" * 10 + + f"\n{message.text}", + parse_mode="html", + disable_web_page_preview=True, + ) diff --git a/sync/vk/handlers/photos.py b/sync/vk/handlers/photos.py index 1f68f65..530f60e 100644 --- a/sync/vk/handlers/photos.py +++ b/sync/vk/handlers/photos.py @@ -1,10 +1,7 @@ from ..core import bot from vkbottle.user import Message -from ...db import Sessions -from ...db.models import Conversations -from sqlalchemy import select from ...tg.core import bot as tg -from ..utils import create_vk_link +from ..utils import create_vk_link, check_bundle from ...utils import get_file_from_url from vkbottle.dispatch.rules import ABCRule from aiogram.types import InputMediaPhoto @@ -27,31 +24,27 @@ async def check(self, event: Message): @bot.on.message(PhotoRule()) -async def on_photo(message: Message): - async with Sessions() as session: - bundle = await session.scalar( - select(Conversations).where(Conversations.vk_id == message.peer_id) - ) - if bundle: - user_info = await bot.api.users.get(message.from_id) - text = f"{user_info[0].first_name} {user_info[0].last_name}\n" - if message.text: - text += "‾" * 10 - text += f"\n{message.text}" +@check_bundle +async def on_photo(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" + if message.text: + text += "‾" * 10 + text += f"\n{message.text}" - photos = [] - for index, attachment in enumerate(message.attachments): - if attachment and attachment.photo: - photos.append( - InputMediaPhoto( - await get_file_from_url( - await get_best_quality_image(attachment.photo.sizes) - ), - parse_mode="html", - caption=text if index == 0 else None, - ) - ) - await tg.send_media_group(bundle.tg_id, photos) + photos = [] + for index, attachment in enumerate(message.attachments): + if attachment and attachment.photo: + photos.append( + InputMediaPhoto( + await get_file_from_url( + await get_best_quality_image(attachment.photo.sizes) + ), + parse_mode="html", + caption=text if index == 0 else None, + ) + ) + await tg.send_media_group(bundle.tg_id, photos) async def get_best_quality_image(sizes: list): diff --git a/sync/vk/handlers/stickers.py b/sync/vk/handlers/stickers.py index 14f553e..44da51f 100644 --- a/sync/vk/handlers/stickers.py +++ b/sync/vk/handlers/stickers.py @@ -1,10 +1,7 @@ from ..core import bot from vkbottle.user import Message -from ...db import Sessions -from ...db.models import Conversations -from sqlalchemy import select from ...tg.core import bot as tg -from ..utils import create_vk_link +from ..utils import create_vk_link, check_bundle from ...utils import get_file_from_url from vkbottle.dispatch.rules import ABCRule @@ -26,19 +23,15 @@ async def check(self, event: Message): @bot.on.message(StrickersRule()) -async def on_stickers(message: Message): - async with Sessions() as session: - bundle = await session.scalar( - select(Conversations).where(Conversations.vk_id == message.peer_id) - ) - if bundle: - user_info = await bot.api.users.get(message.from_id) - sticker = await get_file_from_url( - message.attachments[0].sticker.images_with_background[-1].url - ) - sticker_message = await tg.send_sticker(bundle.tg_id, sticker=sticker) - await sticker_message.reply( - f"{user_info[0].first_name} {user_info[0].last_name}\n", - parse_mode="html", - disable_web_page_preview=True, - ) +@check_bundle +async def on_stickers(message: Message, bundle): + user_info = await bot.api.users.get(message.from_id) + sticker = await get_file_from_url( + message.attachments[0].sticker.images_with_background[-1].url + ) + sticker_message = await tg.send_sticker(bundle.tg_id, sticker=sticker) + await sticker_message.reply( + f"{user_info[0].first_name} {user_info[0].last_name}\n", + parse_mode="html", + disable_web_page_preview=True, + ) diff --git a/sync/vk/handlers/videos.py b/sync/vk/handlers/videos.py index 2e260cc..2497aa1 100644 --- a/sync/vk/handlers/videos.py +++ b/sync/vk/handlers/videos.py @@ -1,10 +1,7 @@ from ..core import bot from vkbottle.user import Message -from ...db import Sessions -from ...db.models import Conversations -from sqlalchemy import select from ...tg.core import bot as tg -from ..utils import create_vk_link +from ..utils import create_vk_link, check_bundle from ...utils import get_file_from_url from vkbottle.dispatch.rules import ABCRule from aiogram.types import InputMediaVideo @@ -27,31 +24,27 @@ async def check(self, event: Message): @bot.on.message(VideoRule()) -async def on_video(message: Message): - async with Sessions() as session: - bundle = await session.scalar( - select(Conversations).where(Conversations.vk_id == message.peer_id) - ) - if bundle: - user_info = await bot.api.users.get(message.from_id) - text = f"{user_info[0].first_name} {user_info[0].last_name}\n" - if message.text: - text += "‾" * 10 - text += f"\n{message.text}" +@check_bundle +async def on_video(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" + if message.text: + text += "‾" * 10 + text += f"\n{message.text}" - videos = [] - for index, attachment in enumerate(message.attachments): - if attachment and attachment.video: - videos.append( - InputMediaVideo( - await get_file_from_url( - await get_best_quality_video(attachment.video.files) - ), - parse_mode="html", - caption=text if index == 0 else None, - ) + videos = [] + for index, attachment in enumerate(message.attachments): + if attachment and attachment.video: + videos.append( + InputMediaVideo( + await get_file_from_url( + await get_best_quality_video(attachment.video.files) + ), + parse_mode="html", + caption=text if index == 0 else None, ) - await tg.send_media_group(bundle.tg_id, videos) + ) + await tg.send_media_group(bundle.tg_id, videos) async def get_best_quality_video(files): diff --git a/sync/vk/handlers/voices.py b/sync/vk/handlers/voices.py index 70d731d..4efcb75 100644 --- a/sync/vk/handlers/voices.py +++ b/sync/vk/handlers/voices.py @@ -1,29 +1,20 @@ from ..core import bot from vkbottle.user import Message -from ...db import Sessions -from ...db.models import Conversations -from sqlalchemy import select from ...tg.core import bot as tg -from ..utils import create_vk_link +from ..utils import create_vk_link, check_bundle from ...utils import get_file_from_url @bot.on.message(attachment=["audio_message"]) -async def on_audio_message(message: Message): - async with Sessions() as session: - bundle = await session.scalar( - select(Conversations).where(Conversations.vk_id == message.peer_id) - ) - if bundle: - user_info = await bot.api.users.get(message.from_id) - voice_file = await get_file_from_url( - message.attachments[0].audio_message.link_ogg - ) +@check_bundle +async def on_audio_message(message: Message, bundle): + user_info = await bot.api.users.get(message.from_id) + voice_file = await get_file_from_url(message.attachments[0].audio_message.link_ogg) - await tg.send_voice( - bundle.tg_id, - voice_file, - f"{user_info[0].first_name} {user_info[0].last_name}\n", - parse_mode="html", - disable_notification=True, - ) + await tg.send_voice( + bundle.tg_id, + voice_file, + f"{user_info[0].first_name} {user_info[0].last_name}\n", + parse_mode="html", + disable_notification=True, + ) diff --git a/sync/vk/utils/__init__.py b/sync/vk/utils/__init__.py index e1dfb4a..b31853b 100644 --- a/sync/vk/utils/__init__.py +++ b/sync/vk/utils/__init__.py @@ -1 +1,2 @@ from .links import create_vk_link +from .bundle import check_bundle diff --git a/sync/vk/utils/bundle.py b/sync/vk/utils/bundle.py new file mode 100644 index 0000000..c923f24 --- /dev/null +++ b/sync/vk/utils/bundle.py @@ -0,0 +1,18 @@ +from ...db import Sessions +from ...db.models import Conversations +from sqlalchemy import select +from functools import wraps +from vkbottle.user import Message + + +def check_bundle(func): + @wraps(func) + async def wrapper(message: Message, *args): + async with Sessions() as session: + bundle = await session.scalar( + select(Conversations).where(Conversations.vk_id == message.peer_id) + ) + if bundle: + await func(message, bundle, *args) + + return wrapper