Split start() into reusable methods

This allows custom authorization processes to be implemented much more
easily. Refactors #281
This commit is contained in:
Dan 2019-08-13 13:15:58 +02:00
parent e02b46ea4a
commit 7daf51af9b
6 changed files with 746 additions and 475 deletions

File diff suppressed because it is too large Load Diff

View File

@ -50,7 +50,6 @@ class BaseClient:
PARENT_DIR = Path(sys.argv[0]).parent PARENT_DIR = Path(sys.argv[0]).parent
INVITE_LINK_RE = re.compile(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/joinchat/)([\w-]+)$") INVITE_LINK_RE = re.compile(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/joinchat/)([\w-]+)$")
BOT_TOKEN_RE = re.compile(r"^\d+:[\w-]+$")
DIALOGS_AT_ONCE = 100 DIALOGS_AT_ONCE = 100
UPDATES_WORKERS = 1 UPDATES_WORKERS = 1
DOWNLOAD_WORKERS = 1 DOWNLOAD_WORKERS = 1
@ -103,7 +102,8 @@ class BaseClient:
self.media_sessions = {} self.media_sessions = {}
self.media_sessions_lock = Lock() self.media_sessions_lock = Lock()
self.is_started = None self.is_connected = None
self.is_initialized = None
self.takeout_id = None self.takeout_id = None

View File

@ -23,5 +23,6 @@ from .input_message_content import *
from .list import List from .list import List
from .messages_and_media import * from .messages_and_media import *
from .object import Object from .object import Object
from .authorization import *
from .update import * from .update import *
from .user_and_chats import * from .user_and_chats import *

View File

@ -0,0 +1,22 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
from .terms_of_service import TermsOfService
from .sent_code import SentCode
__all__ = ["TermsOfService", "SentCode"]

View File

@ -0,0 +1,86 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
from pyrogram.api import types
from ..object import Object
class SentCode(Object):
"""Contains info on a sent confirmation code.
Parameters:
type (``str``):
Type of the current sent code.
Can be *"app"* (code sent via Telegram), *"sms"* (code sent via SMS), *"call"* (code sent via voice call) or
*"flash_call"* (code is in the last 5 digits of the caller's phone number).
phone_code_hash (``str``):
Confirmation code identifier useful for the next authorization steps (either :meth:`~Client.sign_in` or
:meth:`~Client.sign_up`).
next_type (``str``):
Type of the next code to be sent with :meth:`~Client.resend_code`.
Can be *"sms"* (code will be sent via SMS), *"call"* (code will be sent via voice call) or *"flash_call"*
(code will be in the last 5 digits of caller's phone number).
timeout (``int``):
Delay in seconds before calling :meth:`~Client.resend_code`.
"""
def __init__(
self, *,
type: str,
phone_code_hash: str,
next_type: str = None,
timeout: int = None
):
super().__init__()
self.type = type
self.phone_code_hash = phone_code_hash
self.next_type = next_type
self.timeout = timeout
@staticmethod
def _parse(sent_code: types.auth.SentCode) -> "SentCode":
type = sent_code.type
if isinstance(type, types.auth.SentCodeTypeApp):
type = "app"
elif isinstance(type, types.auth.SentCodeTypeSms):
type = "sms"
elif isinstance(type, types.auth.SentCodeTypeCall):
type = "call"
elif isinstance(type, types.auth.SentCodeTypeFlashCall):
type = "flash_call"
next_type = sent_code.next_type
if isinstance(next_type, types.auth.CodeTypeSms):
next_type = "sms"
elif isinstance(next_type, types.auth.CodeTypeCall):
next_type = "call"
elif isinstance(next_type, types.auth.CodeTypeFlashCall):
next_type = "flash_call"
return SentCode(
type=type,
phone_code_hash=sent_code.phone_code_hash,
next_type=next_type,
timeout=sent_code.timeout
)

View File

@ -0,0 +1,56 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
from typing import List
from pyrogram.api import types
from ..messages_and_media import MessageEntity
from ..object import Object
class TermsOfService(Object):
"""Telegram's Terms of Service returned by :meth:`~Client.sign_in`.
Parameters:
id (``str``):
Terms of Service identifier.
text (``str``):
Terms of Service text.
entities (List of :obj:`MessageEntity`):
Special entities like URLs that appear in the text.
"""
def __init__(self, *, id: str, text: str, entities: List[MessageEntity]):
super().__init__()
self.id = id
self.text = text
self.entities = entities
@staticmethod
def _parse(terms_of_service: types.help.TermsOfService) -> "TermsOfService":
return TermsOfService(
id=terms_of_service.id.data,
text=terms_of_service.text,
entities=[
MessageEntity._parse(None, entity, {})
for entity in terms_of_service.entities
]
)