async def finish_download(track, inline_message_id, user): file_id = await db_utils.get_track(track.id) if file_id: return await bot.edit_message_media( media=types.InputMediaAudio(media=file_id, title=track.title, performer=track.artist.name, duration=track.duration), inline_message_id=inline_message_id) try: track_file = await track.download() thumb = await track.get_thumb() if thumb: thumb = BytesIO(thumb) filename = f"{track.artist.name} - {track.title}.mp3".replace('/', '_') except ValueError: return await bot.edit_message_reply_markup( inline_message_id=inline_message_id, reply_markup=inline_keyboards.download_error_keyboard) msg = await bot.send_audio(chat_id=-1001246220493, audio=InputFile(track_file, filename=filename), thumb=InputFile(thumb, filename='thumb.jpg'), performer=track.artist.name, title=track.title, duration=track.duration) file_id = msg.audio.file_id await db_utils.add_track(track.id, file_id) file_id = await db_utils.get_track(track.id) try: await bot.edit_message_media(media=types.InputMediaAudio( media=file_id, title=track.title, performer=track.artist.name, duration=track.duration), inline_message_id=inline_message_id) except exceptions.BadRequest: try: await bot.send_audio(user.id, file_id) except: pass file_download_logger.info( f'[downloaded track {track.id} (inline)] {track}') sent_message_logger.info( f'[send track {track.id} to {format_name(user)} (inline)] {track}')
async def finish_download(track, inline_message_id, user): file_id = await db_utils.get_track(track.id) if file_id: return await bot.edit_message_media( media=types.InputMediaAudio(media=file_id, title=track.title, performer=track.artist.name, duration=track.duration), inline_message_id=inline_message_id) path = await track.download() if (os.path.getsize(path) >> 20) > 50: await bot.edit_message_reply_markup( inline_message_id=inline_message_id, reply_markup=inline_keyboards.large_file_keyboard) await post_large_track(path, track) file_id = await db_utils.get_track(track.id) else: msg = await upload_track(track, path) await db_utils.add_track(track.id, msg.audio.file_id) file_id = msg.audio.file_id try: await bot.edit_message_media(media=types.InputMediaAudio( media=file_id, title=track.title, performer=track.artist.name, duration=track.duration), inline_message_id=inline_message_id) shutil.rmtree(path.rsplit('/', 1)[0]) except exceptions.BadRequest: try: await bot.send_audio(user.id, file_id) except: pass file_download_logger.info( f'[downloaded track {track.id} (inline)] {track}') sent_message_logger.info( f'[send track {track.id} to {format_name(user)} (inline)] {track}')
from aiogram import types from .dataset import AUDIO, ANIMATION, \ DOCUMENT, PHOTO, VIDEO WIDTH = 'width' HEIGHT = 'height' input_media_audio = types.InputMediaAudio(types.Audio(**AUDIO)) input_media_animation = types.InputMediaAnimation(types.Animation(**ANIMATION)) input_media_document = types.InputMediaDocument(types.Document(**DOCUMENT)) input_media_video = types.InputMediaVideo(types.Video(**VIDEO)) input_media_photo = types.InputMediaPhoto(types.PhotoSize(**PHOTO)) def test_field_width(): """ https://core.telegram.org/bots/api#inputmedia """ assert not hasattr(input_media_audio, WIDTH) assert not hasattr(input_media_document, WIDTH) assert not hasattr(input_media_photo, WIDTH) assert hasattr(input_media_animation, WIDTH) assert hasattr(input_media_video, WIDTH) def test_field_height(): """ https://core.telegram.org/bots/api#inputmedia """ assert not hasattr(input_media_audio, HEIGHT)