Skip to content

Commit

Permalink
Отправка кружков со стороны вк
Browse files Browse the repository at this point in the history
  • Loading branch information
Delitel-WEB committed Sep 26, 2023
1 parent 84f8340 commit 27311c0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 21 deletions.
1 change: 1 addition & 0 deletions sync/vk/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import circles
from . import videos
from . import photos
from . import stickers
Expand Down
42 changes: 42 additions & 0 deletions sync/vk/handlers/circles.py
Original file line number Diff line number Diff line change
@@ -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"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\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,
)
2 changes: 1 addition & 1 deletion sync/vk/handlers/stickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ async def on_stickers(message: Message, bundle):
f"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\n",
parse_mode="html",
disable_web_page_preview=True,
disable_notification=True
disable_notification=True,
)
21 changes: 1 addition & 20 deletions sync/vk/handlers/videos.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions sync/vk/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .links import create_vk_link
from .bundle import check_bundle
from .video import get_best_quality_video
17 changes: 17 additions & 0 deletions sync/vk/utils/video.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 27311c0

Please sign in to comment.