From da436461a88b7d8a80b05ae770ea8d9e9e82de88 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 10 Aug 2018 11:29:13 +0200 Subject: [PATCH] Revert "Move resolve_peer into utilities" This reverts commit 6437c6c --- pyrogram/client/client.py | 48 ++++++++++++ .../client/methods/utilities/resolve_peer.py | 73 ------------------- 2 files changed, 48 insertions(+), 73 deletions(-) delete mode 100644 pyrogram/client/methods/utilities/resolve_peer.py diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 9fcf6324..3af60931 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -1010,6 +1010,54 @@ class Client(Methods, BaseClient): self.get_initial_dialogs_chunk() + def resolve_peer(self, peer_id: int or str): + """Use this method to get the InputPeer of a known peer_id. + + This is a utility method intended to be used only when working with Raw Functions (i.e: a Telegram API method + you wish to use which is not available yet in the Client class as an easy-to-use method), whenever an InputPeer + type is required. + + Args: + peer_id (``int`` | ``str``): + The peer id you want to extract the InputPeer from. + Can be a direct id (int), a username (str) or a phone number (str). + + Returns: + On success, the resolved peer id is returned in form of an InputPeer object. + + Raises: + :class:`Error ` + """ + if type(peer_id) is str: + if peer_id in ("self", "me"): + return types.InputPeerSelf() + + peer_id = re.sub(r"[@+\s]", "", peer_id.lower()) + + try: + int(peer_id) + except ValueError: + if peer_id not in self.peers_by_username: + self.send(functions.contacts.ResolveUsername(peer_id)) + + return self.peers_by_username[peer_id] + else: + try: + return self.peers_by_phone[peer_id] + except KeyError: + raise PeerIdInvalid + + try: # User + return self.peers_by_id[peer_id] + except KeyError: + try: # Chat + return self.peers_by_id[-peer_id] + except KeyError: + try: # Channel + return self.peers_by_id[int("-100" + str(peer_id))] + except (KeyError, ValueError): + raise PeerIdInvalid + def save_file(self, path: str, file_id: int = None, diff --git a/pyrogram/client/methods/utilities/resolve_peer.py b/pyrogram/client/methods/utilities/resolve_peer.py deleted file mode 100644 index 7cf31078..00000000 --- a/pyrogram/client/methods/utilities/resolve_peer.py +++ /dev/null @@ -1,73 +0,0 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# This file is part of Pyrogram. -# -# Pyrogram is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Pyrogram is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with Pyrogram. If not, see . - -import re - -from pyrogram.api import types, functions -from pyrogram.api.errors import PeerIdInvalid -from ...ext import BaseClient - - -class ResolvePeer(BaseClient): - def resolve_peer(self, peer_id: int or str): - """Use this method to get the InputPeer of a known peer_id. - - This is a utility method intended to be used only when working with Raw Functions (i.e: a Telegram API method - you wish to use which is not available yet in the Client class as an easy-to-use method), whenever an InputPeer - type is required. - - Args: - peer_id (``int`` | ``str``): - The peer id you want to extract the InputPeer from. - Can be a direct id (int), a username (str) or a phone number (str). - - Returns: - On success, the resolved peer id is returned in form of an InputPeer object. - - Raises: - :class:`Error ` - """ - if type(peer_id) is str: - if peer_id in ("self", "me"): - return types.InputPeerSelf() - - peer_id = re.sub(r"[@+\s]", "", peer_id.lower()) - - try: - int(peer_id) - except ValueError: - if peer_id not in self.peers_by_username: - self.send(functions.contacts.ResolveUsername(peer_id)) - - return self.peers_by_username[peer_id] - else: - try: - return self.peers_by_phone[peer_id] - except KeyError: - raise PeerIdInvalid - - try: # User - return self.peers_by_id[peer_id] - except KeyError: - try: # Chat - return self.peers_by_id[-peer_id] - except KeyError: - try: # Channel - return self.peers_by_id[int("-100" + str(peer_id))] - except (KeyError, ValueError): - raise PeerIdInvalid