mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-16 12:51:18 +00:00
Merge branch 'master' into docs
This commit is contained in:
commit
e28088eac7
@ -16,7 +16,12 @@
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
__copyright__ = "Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>".encode(errors="replace").decode()
|
||||
import sys
|
||||
|
||||
__copyright__ = "Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>".replace(
|
||||
"\xe8",
|
||||
"e" if sys.getfilesystemencoding() == "ascii" else "\xe8"
|
||||
)
|
||||
__license__ = "GNU Lesser General Public License v3 or later (LGPLv3+)"
|
||||
__version__ = "0.4.2"
|
||||
|
||||
|
@ -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/(.+)$")
|
||||
@ -105,7 +108,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 +119,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 +150,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 +256,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(
|
||||
@ -513,18 +533,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.
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user