From d5ca99dfffcf2521e0e53dbace70aa3a3b6a665d Mon Sep 17 00:00:00 2001 From: zeroone2numeral2 Date: Thu, 30 Aug 2018 11:50:09 +0200 Subject: [PATCH 1/9] Allow to set audio thumbnail when using send_audio With the Bot API 4.0 update (https://core.telegram.org/bots/api#july-26-2018), sendAudio allows bots to pass a thumbnail - making it possible for pyrogram to include a 'thumb' parameter in its convenience method 'send_audio' --- pyrogram/client/methods/messages/send_audio.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pyrogram/client/methods/messages/send_audio.py b/pyrogram/client/methods/messages/send_audio.py index 7d590b79..43635f01 100644 --- a/pyrogram/client/methods/messages/send_audio.py +++ b/pyrogram/client/methods/messages/send_audio.py @@ -35,6 +35,7 @@ class SendAudio(BaseClient): duration: int = 0, performer: str = None, title: str = None, + thumb: str = None, disable_notification: bool = None, reply_to_message_id: int = None, reply_markup=None, @@ -74,6 +75,11 @@ class SendAudio(BaseClient): title (``str``, *optional*): Track name. + thumb (``str``, *optional*): + Audio thumbnail. + Pass a file path as string to send an image that exists on your local machine. + Thumbnail should have 90 or less pixels of width and 90 or less pixels of height. + disable_notification (``bool``, *optional*): Sends the message silently. Users will receive a notification with no sound. @@ -118,10 +124,12 @@ class SendAudio(BaseClient): style = self.html if parse_mode.lower() == "html" else self.markdown if os.path.exists(audio): + thumb = None if thumb is None else self.save_file(thumb) file = self.save_file(audio, progress=progress, progress_args=progress_args) media = types.InputMediaUploadedDocument( mime_type=mimetypes.types_map.get("." + audio.split(".")[-1], "audio/mpeg"), file=file, + thumb=thumb, attributes=[ types.DocumentAttributeAudio( duration=duration, From b619818c407c4ec298e563614360db7d811e0830 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 3 Sep 2018 16:35:22 +0200 Subject: [PATCH 2/9] Clear recv_queue on session stop. Fixes #103 --- pyrogram/session/session.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index b7645b11..2372d6bf 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -196,6 +196,7 @@ class Session: i.join() self.net_worker_list.clear() + self.recv_queue.queue.clear() for i in self.results.values(): i.event.set() From 392f0070fb694c0bc4d3f4a767ebcd3d392d7f93 Mon Sep 17 00:00:00 2001 From: Konstantin Klimov Date: Tue, 4 Sep 2018 08:25:56 +0300 Subject: [PATCH 3/9] Fixed decode error in `api.core.primitives.string.py` --- pyrogram/api/core/primitives/string.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/api/core/primitives/string.py b/pyrogram/api/core/primitives/string.py index 5c05e5b0..7d26fb57 100644 --- a/pyrogram/api/core/primitives/string.py +++ b/pyrogram/api/core/primitives/string.py @@ -24,7 +24,7 @@ from . import Bytes class String(Bytes): @staticmethod def read(b: BytesIO, *args) -> str: - return super(String, String).read(b).decode() + return super(String, String).read(b).decode(errors='replace') def __new__(cls, value: str) -> bytes: return super().__new__(cls, value.encode()) From b1aff3ca5e3b670ebdbc618581f97dfdbc477876 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 12 Sep 2018 07:44:49 +0200 Subject: [PATCH 4/9] Small style fixes "double quotes" --- pyrogram/api/core/primitives/string.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/api/core/primitives/string.py b/pyrogram/api/core/primitives/string.py index 7d26fb57..3584d1b9 100644 --- a/pyrogram/api/core/primitives/string.py +++ b/pyrogram/api/core/primitives/string.py @@ -24,7 +24,7 @@ from . import Bytes class String(Bytes): @staticmethod def read(b: BytesIO, *args) -> str: - return super(String, String).read(b).decode(errors='replace') + return super(String, String).read(b).decode(errors="replace") def __new__(cls, value: str) -> bytes: return super().__new__(cls, value.encode()) From 6a4bf23b0925e1f17945f9b7e95d8f29f8e6a642 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 12 Sep 2018 08:14:49 +0200 Subject: [PATCH 5/9] Update send_audio docstrings --- pyrogram/client/methods/messages/send_audio.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/methods/messages/send_audio.py b/pyrogram/client/methods/messages/send_audio.py index 43635f01..4d9c729d 100644 --- a/pyrogram/client/methods/messages/send_audio.py +++ b/pyrogram/client/methods/messages/send_audio.py @@ -76,9 +76,10 @@ class SendAudio(BaseClient): Track name. thumb (``str``, *optional*): - Audio thumbnail. - Pass a file path as string to send an image that exists on your local machine. - Thumbnail should have 90 or less pixels of width and 90 or less pixels of height. + Thumbnail of the music file album cover. + The thumbnail should be in JPEG format and less than 200 KB in size. + A thumbnail's width and height should not exceed 90 pixels. + Thumbnails can't be reused and can be only uploaded as a new file. disable_notification (``bool``, *optional*): Sends the message silently. From eb8513e00900bd0e3b343756eec23c2484260e60 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 12 Sep 2018 08:26:13 +0200 Subject: [PATCH 6/9] Update send_video docstrings Add a more detailed "thumb" description --- pyrogram/client/methods/messages/send_video.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/methods/messages/send_video.py b/pyrogram/client/methods/messages/send_video.py index b86b4702..f646d425 100644 --- a/pyrogram/client/methods/messages/send_video.py +++ b/pyrogram/client/methods/messages/send_video.py @@ -75,9 +75,10 @@ class SendVideo(BaseClient): Video height. thumb (``str``, *optional*): - Video thumbnail. - Pass a file path as string to send an image that exists on your local machine. - Thumbnail should have 90 or less pixels of width and 90 or less pixels of height. + Thumbnail of the video sent. + The thumbnail should be in JPEG format and less than 200 KB in size. + A thumbnail's width and height should not exceed 90 pixels. + Thumbnails can't be reused and can be only uploaded as a new file. supports_streaming (``bool``, *optional*): Pass True, if the uploaded video is suitable for streaming. From c75a4f182a0d788badfe3b7753604bbbb213ffc0 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 Sep 2018 13:23:25 +0200 Subject: [PATCH 7/9] Handle Telegram exceptions on start(). Fixes #121 The client doesn't need to be stopped as it never started successfully. --- pyrogram/client/client.py | 45 ++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 709ea95a..f0134be0 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -215,29 +215,34 @@ class Client(Methods, BaseClient): self.session.start() self.is_started = True + + try: + if self.user_id is None: + if self.token is None: + self.authorize_user() + else: + self.authorize_bot() + + self.save_session() - if self.user_id is None: if self.token is None: - self.authorize_user() + now = time.time() + + if abs(now - self.date) > Client.OFFLINE_SLEEP: + self.peers_by_username = {} + self.peers_by_phone = {} + + self.get_initial_dialogs() + self.get_contacts() + else: + self.send(functions.messages.GetPinnedDialogs()) + self.get_initial_dialogs_chunk() else: - self.authorize_bot() - - self.save_session() - - if self.token is None: - now = time.time() - - if abs(now - self.date) > Client.OFFLINE_SLEEP: - self.peers_by_username = {} - self.peers_by_phone = {} - - self.get_initial_dialogs() - self.get_contacts() - else: - self.send(functions.messages.GetPinnedDialogs()) - self.get_initial_dialogs_chunk() - else: - self.send(functions.updates.GetState()) + self.send(functions.updates.GetState()) + except Exception as e: + self.is_started = False + self.session.stop() + raise e for i in range(self.UPDATES_WORKERS): self.updates_workers_list.append( From d2d4f55673453dec7167895a5b221c19fa838288 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Sep 2018 16:53:17 +0200 Subject: [PATCH 8/9] Revert "Remove TODO" This reverts commit f576fc8 --- pyrogram/crypto/aes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyrogram/crypto/aes.py b/pyrogram/crypto/aes.py index f16688c4..065a5b4d 100644 --- a/pyrogram/crypto/aes.py +++ b/pyrogram/crypto/aes.py @@ -27,6 +27,7 @@ try: class AES: + # TODO: Use new tgcrypto function names @classmethod def ige256_encrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes: return tgcrypto.ige256_encrypt(data, key, iv) From 0f0e757f4c1647b16e02570521ba8131e9cfb3cd Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Sep 2018 16:53:21 +0200 Subject: [PATCH 9/9] Revert "Update tgcrypto function names" This reverts commit a5979a3 --- pyrogram/crypto/aes.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pyrogram/crypto/aes.py b/pyrogram/crypto/aes.py index 065a5b4d..e4fb94b1 100644 --- a/pyrogram/crypto/aes.py +++ b/pyrogram/crypto/aes.py @@ -30,19 +30,19 @@ try: # TODO: Use new tgcrypto function names @classmethod def ige256_encrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes: - return tgcrypto.ige256_encrypt(data, key, iv) + return tgcrypto.ige_encrypt(data, key, iv) @classmethod def ige256_decrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes: - return tgcrypto.ige256_decrypt(data, key, iv) + return tgcrypto.ige_decrypt(data, key, iv) @staticmethod def ctr256_encrypt(data: bytes, key: bytes, iv: bytearray, state: bytearray = None) -> bytes: - return tgcrypto.ctr256_encrypt(data, key, iv, state or bytearray(1)) + return tgcrypto.ctr_encrypt(data, key, iv, state or bytearray(1)) @staticmethod def ctr256_decrypt(data: bytes, key: bytes, iv: bytearray, state: bytearray = None) -> bytes: - return tgcrypto.ctr256_decrypt(data, key, iv, state or bytearray(1)) + return tgcrypto.ctr_decrypt(data, key, iv, state or bytearray(1)) @staticmethod def xor(a: bytes, b: bytes) -> bytes: