From 8f44364c6c3ae5f2763dbae0970cfec2a550f983 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 21 Jan 2019 19:46:33 +0100 Subject: [PATCH] Add supports_streaming to send_file and update docs/errors --- .../advanced-usage/accessing-the-full-api.rst | 1 - telethon/client/uploads.py | 19 +++++++++++++++---- telethon/utils.py | 9 ++++++--- telethon_generator/data/errors.csv | 1 + telethon_generator/data/methods.csv | 2 +- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/readthedocs/extra/advanced-usage/accessing-the-full-api.rst b/readthedocs/extra/advanced-usage/accessing-the-full-api.rst index dbf805ac..2d61c253 100644 --- a/readthedocs/extra/advanced-usage/accessing-the-full-api.rst +++ b/readthedocs/extra/advanced-usage/accessing-the-full-api.rst @@ -121,7 +121,6 @@ request we do: .. code-block:: python result = client(SendMessageRequest(peer, 'Hello there!')) - # __call__ is an alias for client.invoke(request). Both will work Message sent! Of course, this is only an example. There are over 250 methods available as of layer 80, and you can use every single of them diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index 0cb74462..552a84b8 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -33,7 +33,7 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): progress_callback=None, reply_to=None, attributes=None, thumb=None, allow_cache=True, parse_mode=(), voice_note=False, video_note=False, buttons=None, silent=None, - **kwargs): + supports_streaming=False, **kwargs): """ Sends a file to the specified entity. @@ -119,6 +119,13 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): channel or not. Defaults to ``False``, which means it will notify them. Set it to ``True`` to alter this behaviour. + supports_streaming (`bool`, optional): + Whether the sent video supports streaming or not. Note that + Telegram only recognizes as streamable some formats like MP4, + and others like AVI or MKV will not work. You should convert + these to MP4 before sending if you want them to be streamable. + Unsupported formats will result in ``VideoContentTypeError``. + Notes: If the ``hachoir3`` package (``hachoir`` module) is installed, it will be used to determine metadata from audio and video files. @@ -161,6 +168,7 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): progress_callback=progress_callback, reply_to=reply_to, attributes=attributes, thumb=thumb, voice_note=voice_note, video_note=video_note, buttons=buttons, silent=silent, + supports_streaming=supports_streaming, **kwargs )) @@ -181,7 +189,8 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): file, force_document=force_document, progress_callback=progress_callback, attributes=attributes, allow_cache=allow_cache, thumb=thumb, - voice_note=voice_note, video_note=video_note + voice_note=voice_note, video_note=video_note, + supports_streaming=supports_streaming ) markup = self.build_reply_markup(buttons) @@ -389,7 +398,8 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): async def _file_to_media( self, file, force_document=False, progress_callback=None, attributes=None, thumb=None, - allow_cache=True, voice_note=False, video_note=False): + allow_cache=True, voice_note=False, video_note=False, + supports_streaming=False): if not file: return None, None @@ -447,7 +457,8 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): attributes=attributes, force_document=force_document, voice_note=voice_note, - video_note=video_note + video_note=video_note, + supports_streaming=supports_streaming ) input_kw = {} diff --git a/telethon/utils.py b/telethon/utils.py index 20a23e20..8a9da202 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -463,7 +463,8 @@ def get_message_id(message): def get_attributes(file, *, attributes=None, mime_type=None, - force_document=False, voice_note=False, video_note=False): + force_document=False, voice_note=False, video_note=False, + supports_streaming=False): """ Get a list of attributes for the given file and the mime type as a tuple ([attribute], mime_type). @@ -496,11 +497,13 @@ def get_attributes(file, *, attributes=None, mime_type=None, w=m.get('width') if m.has('width') else 0, h=m.get('height') if m.has('height') else 0, duration=int(m.get('duration').seconds - if m.has('duration') else 0) + if m.has('duration') else 0), + supports_streaming=supports_streaming ) else: doc = types.DocumentAttributeVideo( - 0, 1, 1, round_message=video_note) + 0, 1, 1, round_message=video_note, + supports_streaming=supports_streaming) attr_dict[types.DocumentAttributeVideo] = doc diff --git a/telethon_generator/data/errors.csv b/telethon_generator/data/errors.csv index 6721d1cb..3c703de1 100644 --- a/telethon_generator/data/errors.csv +++ b/telethon_generator/data/errors.csv @@ -247,6 +247,7 @@ USER_NOT_MUTUAL_CONTACT,400 403,The provided user is not a mutual contact USER_NOT_PARTICIPANT,400,The target user is not a member of the specified megagroup or channel USER_PRIVACY_RESTRICTED,403,The user's privacy settings do not allow you to do this USER_RESTRICTED,403,"You're spamreported, you can't create channels or chats." +VIDEO_CONTENT_TYPE_INVALID,400,The video content type is not supported with the given parameters (i.e. supports_streaming) WC_CONVERT_URL_INVALID,400,WC convert URL invalid WEBPAGE_CURL_FAILED,400,Failure while fetching the webpage with cURL WEBPAGE_MEDIA_EMPTY,400,Webpage media empty diff --git a/telethon_generator/data/methods.csv b/telethon_generator/data/methods.csv index f2d2f04d..e90f3bfc 100644 --- a/telethon_generator/data/methods.csv +++ b/telethon_generator/data/methods.csv @@ -139,7 +139,7 @@ messages.sendEncrypted,unknown,CHAT_ID_INVALID DATA_INVALID ENCRYPTION_DECLINED messages.sendEncryptedFile,unknown,MSG_WAIT_FAILED messages.sendEncryptedService,unknown,DATA_INVALID ENCRYPTION_DECLINED MSG_WAIT_FAILED USER_IS_BLOCKED messages.sendInlineBotResult,user,CHAT_WRITE_FORBIDDEN INLINE_RESULT_EXPIRED PEER_ID_INVALID QUERY_ID_EMPTY WEBPAGE_CURL_FAILED WEBPAGE_MEDIA_EMPTY -messages.sendMedia,unknown,BOT_POLLS_DISABLED CHANNEL_INVALID CHANNEL_PRIVATE CHAT_ADMIN_REQUIRED CHAT_SEND_MEDIA_FORBIDDEN CHAT_WRITE_FORBIDDEN EXTERNAL_URL_INVALID FILE_PARTS_INVALID FILE_PART_LENGTH_INVALID INPUT_USER_DEACTIVATED MEDIA_CAPTION_TOO_LONG MEDIA_EMPTY PEER_ID_INVALID PHOTO_EXT_INVALID PHOTO_INVALID_DIMENSIONS PHOTO_SAVE_FILE_INVALID RANDOM_ID_DUPLICATE STORAGE_CHECK_FAILED Timeout USER_BANNED_IN_CHANNEL USER_IS_BLOCKED USER_IS_BOT WEBPAGE_CURL_FAILED WEBPAGE_MEDIA_EMPTY +messages.sendMedia,unknown,BOT_POLLS_DISABLED CHANNEL_INVALID CHANNEL_PRIVATE CHAT_ADMIN_REQUIRED CHAT_SEND_MEDIA_FORBIDDEN CHAT_WRITE_FORBIDDEN EXTERNAL_URL_INVALID FILE_PARTS_INVALID FILE_PART_LENGTH_INVALID INPUT_USER_DEACTIVATED MEDIA_CAPTION_TOO_LONG MEDIA_EMPTY PEER_ID_INVALID PHOTO_EXT_INVALID PHOTO_INVALID_DIMENSIONS PHOTO_SAVE_FILE_INVALID RANDOM_ID_DUPLICATE STORAGE_CHECK_FAILED Timeout USER_BANNED_IN_CHANNEL USER_IS_BLOCKED USER_IS_BOT VIDEO_CONTENT_TYPE_INVALID WEBPAGE_CURL_FAILED WEBPAGE_MEDIA_EMPTY messages.sendMessage,unknown,AUTH_KEY_DUPLICATED BUTTON_DATA_INVALID BUTTON_TYPE_INVALID BUTTON_URL_INVALID CHANNEL_INVALID CHANNEL_PRIVATE CHAT_ADMIN_REQUIRED CHAT_ID_INVALID CHAT_RESTRICTED CHAT_WRITE_FORBIDDEN ENTITY_MENTION_USER_INVALID INPUT_USER_DEACTIVATED MESSAGE_EMPTY MESSAGE_TOO_LONG PEER_ID_INVALID RANDOM_ID_DUPLICATE REPLY_MARKUP_INVALID REPLY_MARKUP_TOO_LONG Timeout USER_BANNED_IN_CHANNEL USER_IS_BLOCKED USER_IS_BOT YOU_BLOCKED_USER messages.sendScreenshotNotification,user,PEER_ID_INVALID messages.setBotCallbackAnswer,unknown,QUERY_ID_INVALID URL_INVALID