From 16c5e63c8b4cfbd01e3805bb38d9869df866e9f3 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 25 Jan 2018 16:41:59 +0100 Subject: [PATCH 1/4] Add resolve_username method --- pyrogram/client/client.py | 49 ++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index a0b4586a..fdce0d51 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -513,18 +513,51 @@ class Client: peer_username = peer_username.lower() self.peers_by_username[peer_username] = input_peer + def resolve_username(self, username: str): + username = username.lower().strip("@") + + resolved_peer = self.send( + functions.contacts.ResolveUsername( + username=username + ) + ) # type: types.contacts.ResolvedPeer + + if type(resolved_peer.peer) is PeerUser: + input_peer = InputPeerUser( + user_id=resolved_peer.users[0].id, + access_hash=resolved_peer.users[0].access_hash + ) + chat_id = input_peer.user_id + elif type(resolved_peer.peer) is PeerChannel: + input_peer = InputPeerChannel( + channel_id=resolved_peer.chats[0].id, + access_hash=resolved_peer.chats[0].access_hash + ) + chat_id = input_peer.channel_id + else: + raise PeerIdInvalid + + self.peers_by_username[username] = input_peer + self.peers_by_id[chat_id] = input_peer + + return input_peer + def resolve_peer(self, chat_id: int or str): if chat_id in ("self", "me"): return InputPeerSelf() else: - try: - return ( - self.peers_by_username[chat_id.lower().strip("@")] - if isinstance(chat_id, str) - else self.peers_by_id[chat_id] - ) - except KeyError: - raise PeerIdInvalid + if type(chat_id) is str: + chat_id = chat_id.lower().strip("@") + + try: + return self.peers_by_username[chat_id] + except KeyError: + return self.resolve_username(chat_id) + else: + try: + return self.peers_by_id[chat_id] + except KeyError: + raise PeerIdInvalid def get_me(self): """A simple method for testing the user authorization. Requires no parameters. From e542c73966f8372353aa0e920d0d1c4b413054c9 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 26 Jan 2018 01:05:06 +0100 Subject: [PATCH 2/4] Fix UnicodeDecodeError --- pyrogram/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index a290ca0f..78942791 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -16,7 +16,12 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -__copyright__ = "Copyright (C) 2017-2018 Dan Tès ".encode(errors="replace").decode() +import sys + +__copyright__ = "Copyright (C) 2017-2018 Dan Tès ".replace( + "\xe8", + "e" if sys.getfilesystemencoding() == "ascii" else "\xe8" +) __license__ = "GNU Lesser General Public License v3 or later (LGPLv3+)" __version__ = "0.4.2" From 7234edad5d24f2b339c7a65af1bd03b042465d65 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 26 Jan 2018 11:41:09 +0100 Subject: [PATCH 3/4] Add ability to choose the amount of worker threads for the main session --- pyrogram/client/client.py | 23 ++++++++++++++++++++--- pyrogram/session/session.py | 15 +++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index fdce0d51..01dfc986 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -105,7 +105,8 @@ class Client: phone_code: str or callable = None, password: str = None, first_name: str = None, - last_name: str = None): + last_name: str = None, + workers: int = 4): self.session_name = session_name self.test_mode = test_mode @@ -115,6 +116,8 @@ class Client: self.first_name = first_name self.last_name = last_name + self.workers = workers + self.dc_id = None self.auth_key = None self.user_id = None @@ -144,7 +147,14 @@ class Client: self.load_config() self.load_session(self.session_name) - self.session = Session(self.dc_id, self.test_mode, self.proxy, self.auth_key, self.config.api_id) + self.session = Session( + self.dc_id, + self.test_mode, + self.proxy, + self.auth_key, + self.config.api_id, + workers=self.workers + ) terms = self.session.start() @@ -243,7 +253,14 @@ class Client: self.dc_id = e.x self.auth_key = Auth(self.dc_id, self.test_mode, self.proxy).create() - self.session = Session(self.dc_id, self.test_mode, self.proxy, self.auth_key, self.config.api_id) + self.session = Session( + self.dc_id, + self.test_mode, + self.proxy, + self.auth_key, + self.config.api_id, + workers=self.workers + ) self.session.start() r = self.send( diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index c399d9ec..23d686a4 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -60,7 +60,6 @@ class Session: INITIAL_SALT = 0x616e67656c696361 - WORKERS = 4 WAIT_TIMEOUT = 10 MAX_RETRIES = 5 ACKS_THRESHOLD = 8 @@ -68,13 +67,21 @@ class Session: notice_displayed = False - def __init__(self, dc_id: int, test_mode: bool, proxy: type, auth_key: bytes, api_id: str, is_cdn: bool = False): + def __init__(self, + dc_id: int, + test_mode: bool, + proxy: type, + auth_key: bytes, + api_id: str, + is_cdn: bool = False, + workers: int = 2): if not Session.notice_displayed: print("Pyrogram v{}, {}".format(__version__, __copyright__)) print("Licensed under the terms of the " + __license__, end="\n\n") Session.notice_displayed = True self.is_cdn = is_cdn + self.workers = workers self.connection = Connection(DataCenter(dc_id, test_mode), proxy) @@ -115,7 +122,7 @@ class Session: try: self.connection.connect() - for i in range(self.WORKERS): + for i in range(self.workers): Thread(target=self.worker, name="Worker#{}".format(i + 1)).start() Thread(target=self.recv, name="RecvThread").start() @@ -175,7 +182,7 @@ class Session: self.connection.close() - for i in range(self.WORKERS): + for i in range(self.workers): self.recv_queue.put(None) log.debug("Session stopped") From b1f11b7a9722d33d16a23f3257d28481a066a22b Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 26 Jan 2018 11:49:07 +0100 Subject: [PATCH 4/4] Update docstrings --- pyrogram/client/client.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 01dfc986..9c838e0d 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -93,6 +93,9 @@ class Client: last_name (:obj:`str`, optional): Same purpose as *first_name*; pass a Last Name to avoid entering it manually. It can be an empty string: "" + + workers (:obj:`int`, optional): + Thread pool size for handling incoming messages (updates). Defaults to 4. """ INVITE_LINK_RE = re.compile(r"^(?:https?://)?t\.me/joinchat/(.+)$")