From 8da085198442a879103e813b035672c2ab827c52 Mon Sep 17 00:00:00 2001 From: dogghi <75123663+doggyhaha@users.noreply.github.com> Date: Thu, 11 Aug 2022 13:58:36 +0200 Subject: [PATCH] Add support for BytesIO in InputMedia objects (#1047) fix docstrings and fix "TypeError: stat: path should be string, bytes, os.PathLike or integer, not BytesIO" when passing a BytesIO object to an InputMedia subclass --- pyrogram/methods/messages/edit_inline_media.py | 11 ++++++----- pyrogram/methods/messages/edit_message_media.py | 11 ++++++----- pyrogram/types/input_media/input_media.py | 4 ++-- pyrogram/types/input_media/input_media_animation.py | 2 +- pyrogram/types/input_media/input_media_audio.py | 2 +- pyrogram/types/input_media/input_media_document.py | 2 +- pyrogram/types/input_media/input_media_video.py | 2 +- 7 files changed, 18 insertions(+), 16 deletions(-) diff --git a/pyrogram/methods/messages/edit_inline_media.py b/pyrogram/methods/messages/edit_inline_media.py index cec4a3e1..4126c802 100644 --- a/pyrogram/methods/messages/edit_inline_media.py +++ b/pyrogram/methods/messages/edit_inline_media.py @@ -19,6 +19,7 @@ import os import re import asyncio +import io import pyrogram from pyrogram import raw @@ -77,7 +78,7 @@ class EditInlineMedia: parse_mode = media.parse_mode if isinstance(media, types.InputMediaPhoto): - if os.path.isfile(media.media): + if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media): media = raw.types.InputMediaUploadedPhoto( file=await self.save_file(media.media) ) @@ -88,7 +89,7 @@ class EditInlineMedia: else: media = utils.get_input_media_from_file_id(media.media, FileType.PHOTO) elif isinstance(media, types.InputMediaVideo): - if os.path.isfile(media.media): + if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media): media = raw.types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "video/mp4", thumb=await self.save_file(media.thumb), @@ -112,7 +113,7 @@ class EditInlineMedia: else: media = utils.get_input_media_from_file_id(media.media, FileType.VIDEO) elif isinstance(media, types.InputMediaAudio): - if os.path.isfile(media.media): + if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media): media = raw.types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "audio/mpeg", thumb=await self.save_file(media.thumb), @@ -135,7 +136,7 @@ class EditInlineMedia: else: media = utils.get_input_media_from_file_id(media.media, FileType.AUDIO) elif isinstance(media, types.InputMediaAnimation): - if os.path.isfile(media.media): + if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media): media = raw.types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "video/mp4", thumb=await self.save_file(media.thumb), @@ -161,7 +162,7 @@ class EditInlineMedia: else: media = utils.get_input_media_from_file_id(media.media, FileType.ANIMATION) elif isinstance(media, types.InputMediaDocument): - if os.path.isfile(media.media): + if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media): media = raw.types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "application/zip", thumb=await self.save_file(media.thumb), diff --git a/pyrogram/methods/messages/edit_message_media.py b/pyrogram/methods/messages/edit_message_media.py index 3dce81ed..a3840f23 100644 --- a/pyrogram/methods/messages/edit_message_media.py +++ b/pyrogram/methods/messages/edit_message_media.py @@ -18,6 +18,7 @@ import os import re +import io from typing import Union import pyrogram @@ -89,7 +90,7 @@ class EditMessageMedia: message, entities = (await self.parser.parse(caption, parse_mode)).values() if isinstance(media, types.InputMediaPhoto): - if os.path.isfile(media.media): + if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media): media = await self.invoke( raw.functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), @@ -113,7 +114,7 @@ class EditMessageMedia: else: media = utils.get_input_media_from_file_id(media.media, FileType.PHOTO) elif isinstance(media, types.InputMediaVideo): - if os.path.isfile(media.media): + if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media): media = await self.invoke( raw.functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), @@ -150,7 +151,7 @@ class EditMessageMedia: else: media = utils.get_input_media_from_file_id(media.media, FileType.VIDEO) elif isinstance(media, types.InputMediaAudio): - if os.path.isfile(media.media): + if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media): media = await self.invoke( raw.functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), @@ -186,7 +187,7 @@ class EditMessageMedia: else: media = utils.get_input_media_from_file_id(media.media, FileType.AUDIO) elif isinstance(media, types.InputMediaAnimation): - if os.path.isfile(media.media): + if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media): media = await self.invoke( raw.functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), @@ -224,7 +225,7 @@ class EditMessageMedia: else: media = utils.get_input_media_from_file_id(media.media, FileType.ANIMATION) elif isinstance(media, types.InputMediaDocument): - if os.path.isfile(media.media): + if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media): media = await self.invoke( raw.functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), diff --git a/pyrogram/types/input_media/input_media.py b/pyrogram/types/input_media/input_media.py index 8e2f3a7b..bd60aeb3 100644 --- a/pyrogram/types/input_media/input_media.py +++ b/pyrogram/types/input_media/input_media.py @@ -16,7 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from typing import List +from typing import List, Union, BinaryIO from ..messages_and_media import MessageEntity from ..object import Object @@ -36,7 +36,7 @@ class InputMedia(Object): def __init__( self, - media: str, + media: Union[str, BinaryIO], caption: str = "", parse_mode: str = None, caption_entities: List[MessageEntity] = None diff --git a/pyrogram/types/input_media/input_media_animation.py b/pyrogram/types/input_media/input_media_animation.py index 04aa940e..2eae1a66 100644 --- a/pyrogram/types/input_media/input_media_animation.py +++ b/pyrogram/types/input_media/input_media_animation.py @@ -27,7 +27,7 @@ class InputMediaAnimation(InputMedia): """An animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent inside an album. Parameters: - media (``str``): + media (``str`` | ``BinaryIO``): Animation to send. Pass a file_id as string to send a file that exists on the Telegram servers or pass a file path as string to upload a new file that exists on your local machine or diff --git a/pyrogram/types/input_media/input_media_audio.py b/pyrogram/types/input_media/input_media_audio.py index b4bb7575..cc91e7bd 100644 --- a/pyrogram/types/input_media/input_media_audio.py +++ b/pyrogram/types/input_media/input_media_audio.py @@ -29,7 +29,7 @@ class InputMediaAudio(InputMedia): It is intended to be used with :meth:`~pyrogram.Client.send_media_group`. Parameters: - media (``str``): + media (``str`` | ``BinaryIO``): Audio to send. Pass a file_id as string to send an audio that exists on the Telegram servers or pass a file path as string to upload a new audio that exists on your local machine or diff --git a/pyrogram/types/input_media/input_media_document.py b/pyrogram/types/input_media/input_media_document.py index 91dfc7d6..3e4d510b 100644 --- a/pyrogram/types/input_media/input_media_document.py +++ b/pyrogram/types/input_media/input_media_document.py @@ -27,7 +27,7 @@ class InputMediaDocument(InputMedia): """A generic file to be sent inside an album. Parameters: - media (``str``): + media (``str`` | ``BinaryIO``): File to send. Pass a file_id as string to send a file that exists on the Telegram servers or pass a file path as string to upload a new file that exists on your local machine or diff --git a/pyrogram/types/input_media/input_media_video.py b/pyrogram/types/input_media/input_media_video.py index bad4e3ef..c163cba9 100644 --- a/pyrogram/types/input_media/input_media_video.py +++ b/pyrogram/types/input_media/input_media_video.py @@ -28,7 +28,7 @@ class InputMediaVideo(InputMedia): It is intended to be used with :obj:`~pyrogram.Client.send_media_group`. Parameters: - media (``str``): + media (``str`` | ``BinaryIO``): Video to send. Pass a file_id as string to send a video that exists on the Telegram servers or pass a file path as string to upload a new video that exists on your local machine or