Skip to content

Commit

Permalink
Update converter_callback.py
Browse files Browse the repository at this point in the history
  • Loading branch information
krystlepalace committed Sep 9, 2023
1 parent 6bfddff commit 9e6ec8c
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions handlers/callbacks/converter_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from aiogram.filters.callback_data import CallbackData
from utils.photo_converter import PhotoConverter
from utils.audio_converter import AudioConverter
from utils.video_converter import VideoConverter
from pathlib import Path
from config import CONFIG
import main
import os


router = Router()


Expand All @@ -27,7 +27,8 @@ async def process_image_convert(callback: CallbackQuery,
file_id = callback.message.reply_to_message.photo[-1].file_id
file = await main.bot.get_file(file_id)
file_path = file.file_path
file_on_disk = Path(f"{CONFIG.media_full_path}{file_id}.{file_path.split('.')[-1]}")
file_on_disk = Path(
f"{CONFIG.media_full_path}{file_id}.{file_path.split('.')[-1]}")
await main.bot.download_file(file_path, destination=file_on_disk)

# convertation
Expand All @@ -52,7 +53,8 @@ async def process_image_convert(callback: CallbackQuery,
file_id = callback.message.reply_to_message.audio.file_id
file = await main.bot.get_file(file_id)
file_path = file.file_path
file_on_disk = Path(f"{CONFIG.media_full_path}{file_id}.{file_path.split('.')[-1]}")
file_on_disk = Path(
f"{CONFIG.media_full_path}{file_id}.{file_path.split('.')[-1]}")
await main.bot.download_file(file_path, destination=file_on_disk)

# convertation
Expand All @@ -66,3 +68,36 @@ async def process_image_convert(callback: CallbackQuery,
await callback.answer()
os.remove(file_on_disk)
os.remove(converter.output_path)


@router.callback_query(
FormatCallback.filter(F.type.in_(['VIDEO']))
)
async def process_image_convert(callback: CallbackQuery,
callback_data: FormatCallback):
await callback.message.edit_text(text="Working...")

file_id = callback.message.reply_to_message.video.file_id
file = await main.bot.get_file(file_id)
file_path = file.file_path
file_on_disk = Path(
f"{CONFIG.media_full_path}{file_id}.{file_path.split('.')[-1]}")
await main.bot.download_file(file_path, destination=file_on_disk)

# convertation
converter = VideoConverter(file_on_disk.__str__())
await converter.convert_video(callback_data.format)

if callback_data.format == "mp3":
await main.bot.send_audio(chat_id=callback.from_user.id,
audio=FSInputFile(converter.output_path))
elif callback_data.format == "gif":
await main.bot.send_document(chat_id=callback.from_user.id,
document=FSInputFile(
converter.output_path))
else:
await main.bot.send_video(chat_id=callback.from_user.id,
video=FSInputFile(converter.output_path))

os.remove(file_on_disk)
os.remove(converter.output_path)

0 comments on commit 9e6ec8c

Please sign in to comment.