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
This commit is contained in:
dogghi 2022-08-11 13:58:36 +02:00 committed by GitHub
parent 0505ff0d5e
commit 8da0851984
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 18 additions and 16 deletions

View File

@ -19,6 +19,7 @@
import os import os
import re import re
import asyncio import asyncio
import io
import pyrogram import pyrogram
from pyrogram import raw from pyrogram import raw
@ -77,7 +78,7 @@ class EditInlineMedia:
parse_mode = media.parse_mode parse_mode = media.parse_mode
if isinstance(media, types.InputMediaPhoto): 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( media = raw.types.InputMediaUploadedPhoto(
file=await self.save_file(media.media) file=await self.save_file(media.media)
) )
@ -88,7 +89,7 @@ class EditInlineMedia:
else: else:
media = utils.get_input_media_from_file_id(media.media, FileType.PHOTO) media = utils.get_input_media_from_file_id(media.media, FileType.PHOTO)
elif isinstance(media, types.InputMediaVideo): 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( media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(media.media) or "video/mp4", mime_type=self.guess_mime_type(media.media) or "video/mp4",
thumb=await self.save_file(media.thumb), thumb=await self.save_file(media.thumb),
@ -112,7 +113,7 @@ class EditInlineMedia:
else: else:
media = utils.get_input_media_from_file_id(media.media, FileType.VIDEO) media = utils.get_input_media_from_file_id(media.media, FileType.VIDEO)
elif isinstance(media, types.InputMediaAudio): 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( media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(media.media) or "audio/mpeg", mime_type=self.guess_mime_type(media.media) or "audio/mpeg",
thumb=await self.save_file(media.thumb), thumb=await self.save_file(media.thumb),
@ -135,7 +136,7 @@ class EditInlineMedia:
else: else:
media = utils.get_input_media_from_file_id(media.media, FileType.AUDIO) media = utils.get_input_media_from_file_id(media.media, FileType.AUDIO)
elif isinstance(media, types.InputMediaAnimation): 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( media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(media.media) or "video/mp4", mime_type=self.guess_mime_type(media.media) or "video/mp4",
thumb=await self.save_file(media.thumb), thumb=await self.save_file(media.thumb),
@ -161,7 +162,7 @@ class EditInlineMedia:
else: else:
media = utils.get_input_media_from_file_id(media.media, FileType.ANIMATION) media = utils.get_input_media_from_file_id(media.media, FileType.ANIMATION)
elif isinstance(media, types.InputMediaDocument): 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( media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(media.media) or "application/zip", mime_type=self.guess_mime_type(media.media) or "application/zip",
thumb=await self.save_file(media.thumb), thumb=await self.save_file(media.thumb),

View File

@ -18,6 +18,7 @@
import os import os
import re import re
import io
from typing import Union from typing import Union
import pyrogram import pyrogram
@ -89,7 +90,7 @@ class EditMessageMedia:
message, entities = (await self.parser.parse(caption, parse_mode)).values() message, entities = (await self.parser.parse(caption, parse_mode)).values()
if isinstance(media, types.InputMediaPhoto): 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( media = await self.invoke(
raw.functions.messages.UploadMedia( raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id), peer=await self.resolve_peer(chat_id),
@ -113,7 +114,7 @@ class EditMessageMedia:
else: else:
media = utils.get_input_media_from_file_id(media.media, FileType.PHOTO) media = utils.get_input_media_from_file_id(media.media, FileType.PHOTO)
elif isinstance(media, types.InputMediaVideo): 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( media = await self.invoke(
raw.functions.messages.UploadMedia( raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id), peer=await self.resolve_peer(chat_id),
@ -150,7 +151,7 @@ class EditMessageMedia:
else: else:
media = utils.get_input_media_from_file_id(media.media, FileType.VIDEO) media = utils.get_input_media_from_file_id(media.media, FileType.VIDEO)
elif isinstance(media, types.InputMediaAudio): 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( media = await self.invoke(
raw.functions.messages.UploadMedia( raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id), peer=await self.resolve_peer(chat_id),
@ -186,7 +187,7 @@ class EditMessageMedia:
else: else:
media = utils.get_input_media_from_file_id(media.media, FileType.AUDIO) media = utils.get_input_media_from_file_id(media.media, FileType.AUDIO)
elif isinstance(media, types.InputMediaAnimation): 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( media = await self.invoke(
raw.functions.messages.UploadMedia( raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id), peer=await self.resolve_peer(chat_id),
@ -224,7 +225,7 @@ class EditMessageMedia:
else: else:
media = utils.get_input_media_from_file_id(media.media, FileType.ANIMATION) media = utils.get_input_media_from_file_id(media.media, FileType.ANIMATION)
elif isinstance(media, types.InputMediaDocument): 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( media = await self.invoke(
raw.functions.messages.UploadMedia( raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id), peer=await self.resolve_peer(chat_id),

View File

@ -16,7 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import List from typing import List, Union, BinaryIO
from ..messages_and_media import MessageEntity from ..messages_and_media import MessageEntity
from ..object import Object from ..object import Object
@ -36,7 +36,7 @@ class InputMedia(Object):
def __init__( def __init__(
self, self,
media: str, media: Union[str, BinaryIO],
caption: str = "", caption: str = "",
parse_mode: str = None, parse_mode: str = None,
caption_entities: List[MessageEntity] = None caption_entities: List[MessageEntity] = None

View File

@ -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. """An animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent inside an album.
Parameters: Parameters:
media (``str``): media (``str`` | ``BinaryIO``):
Animation to send. Animation to send.
Pass a file_id as string to send a file that exists on the Telegram servers or 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 pass a file path as string to upload a new file that exists on your local machine or

View File

@ -29,7 +29,7 @@ class InputMediaAudio(InputMedia):
It is intended to be used with :meth:`~pyrogram.Client.send_media_group`. It is intended to be used with :meth:`~pyrogram.Client.send_media_group`.
Parameters: Parameters:
media (``str``): media (``str`` | ``BinaryIO``):
Audio to send. Audio to send.
Pass a file_id as string to send an audio that exists on the Telegram servers or 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 pass a file path as string to upload a new audio that exists on your local machine or

View File

@ -27,7 +27,7 @@ class InputMediaDocument(InputMedia):
"""A generic file to be sent inside an album. """A generic file to be sent inside an album.
Parameters: Parameters:
media (``str``): media (``str`` | ``BinaryIO``):
File to send. File to send.
Pass a file_id as string to send a file that exists on the Telegram servers or 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 pass a file path as string to upload a new file that exists on your local machine or

View File

@ -28,7 +28,7 @@ class InputMediaVideo(InputMedia):
It is intended to be used with :obj:`~pyrogram.Client.send_media_group`. It is intended to be used with :obj:`~pyrogram.Client.send_media_group`.
Parameters: Parameters:
media (``str``): media (``str`` | ``BinaryIO``):
Video to send. Video to send.
Pass a file_id as string to send a video that exists on the Telegram servers or 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 pass a file path as string to upload a new video that exists on your local machine or