From 2bd0c6c525f8f02e945b6d631f614fb7e48bfaee Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 26 Jun 2018 16:39:22 +0200 Subject: [PATCH] Support URLs on send_file --- telethon/client/uploads.py | 24 ++++++++++++++++++------ telethon/utils.py | 7 +++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index 61c4f22b..17db982e 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -3,7 +3,7 @@ import io import logging import os import pathlib -import warnings +import re from io import BytesIO from mimetypes import guess_type @@ -371,14 +371,26 @@ class UploadMethods(MessageParseMethods, UserMethods): except TypeError: return None, None # Can't turn whatever was given into media + media = None as_image = utils.is_image(file) and not force_document use_cache = types.InputPhoto if as_image else types.InputDocument - file_handle = await self.upload_file( - file, progress_callback=progress_callback, - use_cache=use_cache if allow_cache else None - ) + if isinstance(file, str) and re.match('https?://', file): + file_handle = None + if as_image: + media = types.InputMediaPhotoExternal(file) + elif not force_document and utils.is_gif(file): + media = types.InputMediaGifExternal(file, '') + else: + media = types.InputMediaDocumentExternal(file) + else: + file_handle = await self.upload_file( + file, progress_callback=progress_callback, + use_cache=use_cache if allow_cache else None + ) - if isinstance(file_handle, use_cache): + if media: + pass # Already have media, don't check the rest + elif isinstance(file_handle, use_cache): # File was cached, so an instance of use_cache was returned if as_image: media = types.InputMediaPhoto(file_handle) diff --git a/telethon/utils.py b/telethon/utils.py index 1f21b22a..2dcd086e 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -510,6 +510,13 @@ def is_image(file): return re.match(r'\.(png|jpe?g)', _get_extension(file), re.IGNORECASE) +def is_gif(file): + """ + Returns ``True`` if the file extension looks like a gif file to Telegram. + """ + return re.match(r'\.gif', _get_extension(file), re.IGNORECASE) + + def is_audio(file): """Returns ``True`` if the file extension looks like an audio file.""" file = 'a' + _get_extension(file)