From 9abeefac7f276336b9235bd3b160427e227ccea6 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 12 Feb 2018 10:33:51 +0100 Subject: [PATCH] Send video files as video by default instead as document (#601) --- telethon/telegram_client.py | 9 ++++++--- telethon/utils.py | 11 ++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index e5689fad..c0c1bb1e 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -70,7 +70,7 @@ from .tl.types import ( ChatInvite, ChatInviteAlready, PeerChannel, Photo, InputPeerSelf, InputSingleMedia, InputMediaPhoto, InputPhoto, InputFile, InputFileBig, InputDocument, InputMediaDocument, Document, MessageEntityTextUrl, - InputMessageEntityMentionName + InputMessageEntityMentionName, DocumentAttributeVideo ) from .tl.types.messages import DialogsSlice from .extensions import markdown, html @@ -922,8 +922,8 @@ class TelegramClient(TelegramBareClient): force_document (:obj:`bool`, optional): If left to ``False`` and the file is a path that ends with - ``.png``, ``.jpg`` and such, the file will be sent as a photo. - Otherwise always as a document. + the extension of an image file or a video file, it will be + sent as such. Otherwise always as a document. progress_callback (:obj:`callable`, optional): A callback function accepting two parameters: @@ -1015,6 +1015,9 @@ class TelegramClient(TelegramBareClient): # TODO If the input file is an audio, find out: # Performer and song title and add DocumentAttributeAudio } + if not force_document and utils.is_video(file): + attr_dict[DocumentAttributeVideo] = \ + DocumentAttributeVideo(0, 0, 0) else: attr_dict = { DocumentAttributeFilename: diff --git a/telethon/utils.py b/telethon/utils.py index 3e310d3d..9460986c 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -3,10 +3,10 @@ Utilities for working with the Telegram API itself (such as handy methods to convert between an entity like an User, Chat, etc. into its Input version) """ import math +import mimetypes import re from mimetypes import add_type, guess_extension -from .tl.types.contacts import ResolvedPeer from .tl.types import ( Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull, ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty, @@ -24,6 +24,7 @@ from .tl.types import ( InputMediaUploadedPhoto, DocumentAttributeFilename, photos, TopPeer, InputNotifyPeer ) +from .tl.types.contacts import ResolvedPeer USERNAME_RE = re.compile( r'@|(?:https?://)?(?:telegram\.(?:me|dog)|t\.me)/(joinchat/)?' @@ -322,8 +323,12 @@ def get_input_media(media, user_caption=None, is_photo=False): def is_image(file): """Returns True if the file extension looks like an image file""" - return (isinstance(file, str) and - bool(re.search(r'\.(png|jpe?g|gif)$', file, re.IGNORECASE))) + return (mimetypes.guess_type(file)[0] or '').startswith('image/') + + +def is_video(file): + """Returns True if the file extension looks like a video file""" + return (mimetypes.guess_type(file)[0] or '').startswith('video/') def parse_phone(phone):