Skip to content

Commit f4c0793

Browse files
committed
Make session leaner by removing some redundant parameters
Related to #86
1 parent 56f616c commit f4c0793

File tree

3 files changed

+46
-57
lines changed

3 files changed

+46
-57
lines changed

pyrogram/client/client.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,9 @@ def start(self):
187187
self.load_session()
188188

189189
self.session = Session(
190+
self,
190191
self.dc_id,
191-
self.test_mode,
192-
self._proxy,
193-
self.auth_key,
194-
self.api_id,
195-
client=self
192+
self.auth_key
196193
)
197194

198195
self.session.start()
@@ -372,12 +369,9 @@ def authorize_bot(self):
372369
self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create()
373370

374371
self.session = Session(
372+
self,
375373
self.dc_id,
376-
self.test_mode,
377-
self._proxy,
378-
self.auth_key,
379-
self.api_id,
380-
client=self
374+
self.auth_key
381375
)
382376

383377
self.session.start()
@@ -420,12 +414,9 @@ def authorize_user(self):
420414
self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create()
421415

422416
self.session = Session(
417+
self,
423418
self.dc_id,
424-
self.test_mode,
425-
self._proxy,
426-
self.auth_key,
427-
self.api_id,
428-
client=self
419+
self.auth_key
429420
)
430421
self.session.start()
431422

@@ -1033,7 +1024,7 @@ def save_file(self,
10331024
file_id = file_id or self.rnd_id()
10341025
md5_sum = md5() if not is_big and not is_missing_part else None
10351026

1036-
session = Session(self.dc_id, self.test_mode, self._proxy, self.auth_key, self.api_id)
1027+
session = Session(self, self.dc_id, self.auth_key, is_media=True)
10371028
session.start()
10381029

10391030
try:
@@ -1117,11 +1108,10 @@ def get_file(self,
11171108
)
11181109

11191110
session = Session(
1111+
self,
11201112
dc_id,
1121-
self.test_mode,
1122-
self._proxy,
11231113
Auth(dc_id, self.test_mode, self._proxy).create(),
1124-
self.api_id
1114+
is_media=True
11251115
)
11261116

11271117
session.start()
@@ -1136,11 +1126,10 @@ def get_file(self,
11361126
)
11371127
else:
11381128
session = Session(
1129+
self,
11391130
dc_id,
1140-
self.test_mode,
1141-
self._proxy,
11421131
self.auth_key,
1143-
self.api_id
1132+
is_media=True
11441133
)
11451134

11461135
session.start()
@@ -1206,11 +1195,10 @@ def get_file(self,
12061195

12071196
if cdn_session is None:
12081197
cdn_session = Session(
1198+
self,
12091199
r.dc_id,
1210-
self.test_mode,
1211-
self._proxy,
12121200
Auth(r.dc_id, self.test_mode, self._proxy).create(),
1213-
self.api_id,
1201+
is_media=True,
12141202
is_cdn=True
12151203
)
12161204

pyrogram/client/ext/base_client.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,34 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import platform
1920
import re
2021
from queue import Queue
2122
from threading import Lock
2223

24+
from pyrogram import __version__
2325
from ..style import Markdown, HTML
2426
from ...api.core import Object
25-
from ...session.internals import MsgId
2627
from ...session import Session
28+
from ...session.internals import MsgId
2729

2830

2931
class BaseClient:
32+
APP_VERSION = "Pyrogram \U0001f525 {}".format(__version__)
33+
34+
DEVICE_MODEL = "{} {}".format(
35+
platform.python_implementation(),
36+
platform.python_version()
37+
)
38+
39+
SYSTEM_VERSION = "{} {}".format(
40+
platform.system(),
41+
platform.release()
42+
)
43+
44+
SYSTEM_LANG_CODE = "en"
45+
LANG_CODE = "en"
46+
3047
INVITE_LINK_RE = re.compile(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/joinchat/)([\w-]+)$")
3148
BOT_TOKEN_RE = re.compile(r"^\d+:[\w-]+$")
3249
DIALOGS_AT_ONCE = 100

pyrogram/session/session.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import logging
20-
import platform
2120
import threading
2221
import time
2322
from datetime import timedelta, datetime
@@ -47,19 +46,6 @@ def __init__(self):
4746

4847

4948
class Session:
50-
VERSION = __version__
51-
APP_VERSION = "Pyrogram \U0001f525 {}".format(VERSION)
52-
53-
DEVICE_MODEL = "{} {}".format(
54-
platform.python_implementation(),
55-
platform.python_version()
56-
)
57-
58-
SYSTEM_VERSION = "{} {}".format(
59-
platform.system(),
60-
platform.release()
61-
)
62-
6349
INITIAL_SALT = 0x616e67656c696361
6450
NET_WORKERS = 1
6551
WAIT_TIMEOUT = 15
@@ -84,28 +70,24 @@ class Session:
8470
}
8571

8672
def __init__(self,
73+
client: pyrogram,
8774
dc_id: int,
88-
test_mode: bool,
89-
proxy: dict,
9075
auth_key: bytes,
91-
api_id: int,
92-
is_cdn: bool = False,
93-
client: pyrogram = None):
76+
is_media: bool = False,
77+
is_cdn: bool = False):
9478
if not Session.notice_displayed:
9579
print("Pyrogram v{}, {}".format(__version__, __copyright__))
9680
print("Licensed under the terms of the " + __license__, end="\n\n")
9781
Session.notice_displayed = True
9882

83+
self.client = client
9984
self.dc_id = dc_id
100-
self.test_mode = test_mode
101-
self.proxy = proxy
102-
self.api_id = api_id
85+
self.auth_key = auth_key
86+
self.is_media = is_media
10387
self.is_cdn = is_cdn
104-
self.client = client
10588

10689
self.connection = None
10790

108-
self.auth_key = auth_key
10991
self.auth_key_id = sha1(auth_key).digest()[-8:]
11092

11193
self.session_id = Long(MsgId())
@@ -130,7 +112,7 @@ def __init__(self,
130112

131113
def start(self):
132114
while True:
133-
self.connection = Connection(DataCenter(self.dc_id, self.test_mode), self.proxy)
115+
self.connection = Connection(DataCenter(self.dc_id, self.client.test_mode), self.client.proxy)
134116

135117
try:
136118
self.connection.connect()
@@ -159,12 +141,14 @@ def start(self):
159141
functions.InvokeWithLayer(
160142
layer,
161143
functions.InitConnection(
162-
self.api_id,
163-
self.DEVICE_MODEL,
164-
self.SYSTEM_VERSION,
165-
self.APP_VERSION,
166-
"en", "", "en",
167-
functions.help.GetConfig(),
144+
api_id=self.client.api_id,
145+
device_model=self.client.DEVICE_MODEL,
146+
system_version=self.client.SYSTEM_VERSION,
147+
app_version=self.client.APP_VERSION,
148+
system_lang_code=self.client.SYSTEM_LANG_CODE,
149+
lang_code=self.client.LANG_CODE,
150+
lang_pack="",
151+
query=functions.help.GetConfig(),
168152
)
169153
)
170154
)

0 commit comments

Comments
 (0)