2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Make session leaner by removing some redundant parameters

Related to #86
This commit is contained in:
Dan 2018-06-24 18:26:11 +02:00
parent 56f616c753
commit f4c0793a0b
3 changed files with 47 additions and 58 deletions

View File

@ -187,12 +187,9 @@ class Client(Methods, BaseClient):
self.load_session() self.load_session()
self.session = Session( self.session = Session(
self,
self.dc_id, self.dc_id,
self.test_mode, self.auth_key
self._proxy,
self.auth_key,
self.api_id,
client=self
) )
self.session.start() self.session.start()
@ -372,12 +369,9 @@ class Client(Methods, BaseClient):
self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create() self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create()
self.session = Session( self.session = Session(
self,
self.dc_id, self.dc_id,
self.test_mode, self.auth_key
self._proxy,
self.auth_key,
self.api_id,
client=self
) )
self.session.start() self.session.start()
@ -420,12 +414,9 @@ class Client(Methods, BaseClient):
self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create() self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create()
self.session = Session( self.session = Session(
self,
self.dc_id, self.dc_id,
self.test_mode, self.auth_key
self._proxy,
self.auth_key,
self.api_id,
client=self
) )
self.session.start() self.session.start()
@ -1033,7 +1024,7 @@ class Client(Methods, BaseClient):
file_id = file_id or self.rnd_id() file_id = file_id or self.rnd_id()
md5_sum = md5() if not is_big and not is_missing_part else None md5_sum = md5() if not is_big and not is_missing_part else None
session = Session(self.dc_id, self.test_mode, self._proxy, self.auth_key, self.api_id) session = Session(self, self.dc_id, self.auth_key, is_media=True)
session.start() session.start()
try: try:
@ -1117,11 +1108,10 @@ class Client(Methods, BaseClient):
) )
session = Session( session = Session(
self,
dc_id, dc_id,
self.test_mode,
self._proxy,
Auth(dc_id, self.test_mode, self._proxy).create(), Auth(dc_id, self.test_mode, self._proxy).create(),
self.api_id is_media=True
) )
session.start() session.start()
@ -1136,11 +1126,10 @@ class Client(Methods, BaseClient):
) )
else: else:
session = Session( session = Session(
self,
dc_id, dc_id,
self.test_mode,
self._proxy,
self.auth_key, self.auth_key,
self.api_id is_media=True
) )
session.start() session.start()
@ -1206,11 +1195,10 @@ class Client(Methods, BaseClient):
if cdn_session is None: if cdn_session is None:
cdn_session = Session( cdn_session = Session(
self,
r.dc_id, r.dc_id,
self.test_mode,
self._proxy,
Auth(r.dc_id, self.test_mode, self._proxy).create(), Auth(r.dc_id, self.test_mode, self._proxy).create(),
self.api_id, is_media=True,
is_cdn=True is_cdn=True
) )

View File

@ -16,17 +16,34 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import platform
import re import re
from queue import Queue from queue import Queue
from threading import Lock from threading import Lock
from pyrogram import __version__
from ..style import Markdown, HTML from ..style import Markdown, HTML
from ...api.core import Object from ...api.core import Object
from ...session.internals import MsgId
from ...session import Session from ...session import Session
from ...session.internals import MsgId
class BaseClient: class BaseClient:
APP_VERSION = "Pyrogram \U0001f525 {}".format(__version__)
DEVICE_MODEL = "{} {}".format(
platform.python_implementation(),
platform.python_version()
)
SYSTEM_VERSION = "{} {}".format(
platform.system(),
platform.release()
)
SYSTEM_LANG_CODE = "en"
LANG_CODE = "en"
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-]+$") BOT_TOKEN_RE = re.compile(r"^\d+:[\w-]+$")
DIALOGS_AT_ONCE = 100 DIALOGS_AT_ONCE = 100

View File

@ -17,7 +17,6 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import logging import logging
import platform
import threading import threading
import time import time
from datetime import timedelta, datetime from datetime import timedelta, datetime
@ -47,19 +46,6 @@ class Result:
class Session: class Session:
VERSION = __version__
APP_VERSION = "Pyrogram \U0001f525 {}".format(VERSION)
DEVICE_MODEL = "{} {}".format(
platform.python_implementation(),
platform.python_version()
)
SYSTEM_VERSION = "{} {}".format(
platform.system(),
platform.release()
)
INITIAL_SALT = 0x616e67656c696361 INITIAL_SALT = 0x616e67656c696361
NET_WORKERS = 1 NET_WORKERS = 1
WAIT_TIMEOUT = 15 WAIT_TIMEOUT = 15
@ -84,28 +70,24 @@ class Session:
} }
def __init__(self, def __init__(self,
client: pyrogram,
dc_id: int, dc_id: int,
test_mode: bool,
proxy: dict,
auth_key: bytes, auth_key: bytes,
api_id: int, is_media: bool = False,
is_cdn: bool = False, is_cdn: bool = False):
client: pyrogram = None):
if not Session.notice_displayed: if not Session.notice_displayed:
print("Pyrogram v{}, {}".format(__version__, __copyright__)) print("Pyrogram v{}, {}".format(__version__, __copyright__))
print("Licensed under the terms of the " + __license__, end="\n\n") print("Licensed under the terms of the " + __license__, end="\n\n")
Session.notice_displayed = True Session.notice_displayed = True
self.dc_id = dc_id
self.test_mode = test_mode
self.proxy = proxy
self.api_id = api_id
self.is_cdn = is_cdn
self.client = client self.client = client
self.dc_id = dc_id
self.auth_key = auth_key
self.is_media = is_media
self.is_cdn = is_cdn
self.connection = None self.connection = None
self.auth_key = auth_key
self.auth_key_id = sha1(auth_key).digest()[-8:] self.auth_key_id = sha1(auth_key).digest()[-8:]
self.session_id = Long(MsgId()) self.session_id = Long(MsgId())
@ -130,7 +112,7 @@ class Session:
def start(self): def start(self):
while True: while True:
self.connection = Connection(DataCenter(self.dc_id, self.test_mode), self.proxy) self.connection = Connection(DataCenter(self.dc_id, self.client.test_mode), self.client.proxy)
try: try:
self.connection.connect() self.connection.connect()
@ -159,12 +141,14 @@ class Session:
functions.InvokeWithLayer( functions.InvokeWithLayer(
layer, layer,
functions.InitConnection( functions.InitConnection(
self.api_id, api_id=self.client.api_id,
self.DEVICE_MODEL, device_model=self.client.DEVICE_MODEL,
self.SYSTEM_VERSION, system_version=self.client.SYSTEM_VERSION,
self.APP_VERSION, app_version=self.client.APP_VERSION,
"en", "", "en", system_lang_code=self.client.SYSTEM_LANG_CODE,
functions.help.GetConfig(), lang_code=self.client.LANG_CODE,
lang_pack="",
query=functions.help.GetConfig(),
) )
) )
) )