Skip to content

Commit 10f3829

Browse files
committed
Merge branch 'develop' into asyncio
# Conflicts: # pyrogram/client/client.py # pyrogram/connection/connection.py # pyrogram/connection/transport/tcp/tcp.py # pyrogram/connection/transport/tcp/tcp_intermediate.py # pyrogram/session/session.py
2 parents 00f0051 + 07a9cce commit 10f3829

16 files changed

Lines changed: 116 additions & 91 deletions

File tree

docs/source/pyrogram/Types.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Input Media
6262

6363
InputMediaPhoto
6464
InputMediaVideo
65+
InputMediaAudio
66+
InputMediaAnimation
67+
InputMediaDocument
6568
InputPhoneContact
6669

6770
.. User & Chats
@@ -172,5 +175,14 @@ Input Media
172175
.. autoclass:: InputMediaVideo
173176
:members:
174177

178+
.. autoclass:: InputMediaAudio
179+
:members:
180+
181+
.. autoclass:: InputMediaAnimation
182+
:members:
183+
184+
.. autoclass:: InputMediaDocument
185+
:members:
186+
175187
.. autoclass:: InputPhoneContact
176188
:members:

pyrogram/client/client.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def __init__(self,
146146
device_model: str = None,
147147
system_version: str = None,
148148
lang_code: str = None,
149+
ipv6: bool = False,
149150
proxy: dict = None,
150151
test_mode: bool = False,
151152
phone_number: str = None,
@@ -166,6 +167,7 @@ def __init__(self,
166167
self.device_model = device_model
167168
self.system_version = system_version
168169
self.lang_code = lang_code
170+
self.ipv6 = ipv6
169171
# TODO: Make code consistent, use underscore for private/protected fields
170172
self._proxy = proxy
171173
self.test_mode = test_mode
@@ -201,7 +203,7 @@ async def start(self):
201203
raise ConnectionError("Client has already been started")
202204

203205
if self.BOT_TOKEN_RE.match(self.session_name):
204-
self.token = self.session_name
206+
self.bot_token = self.session_name
205207
self.session_name = self.session_name.split(":")[0]
206208

207209
self.load_config()
@@ -217,14 +219,14 @@ async def start(self):
217219
self.is_started = True
218220

219221
if self.user_id is None:
220-
if self.token is None:
222+
if self.bot_token is None:
221223
await self.authorize_user()
222224
else:
223225
await self.authorize_bot()
224226

225227
self.save_session()
226228

227-
if self.token is None:
229+
if self.bot_token is None:
228230
now = time.time()
229231

230232
if abs(now - self.date) > Client.OFFLINE_SLEEP:
@@ -385,14 +387,14 @@ async def authorize_bot(self):
385387
flags=0,
386388
api_id=self.api_id,
387389
api_hash=self.api_hash,
388-
bot_auth_token=self.token
390+
bot_auth_token=self.bot_token
389391
)
390392
)
391393
except UserMigrate as e:
392394
await self.session.stop()
393395

394396
self.dc_id = e.x
395-
self.auth_key = await Auth(self.dc_id, self.test_mode, self._proxy).create()
397+
self.auth_key = await Auth(self.dc_id, self.test_mode, self.ipv6, self._proxy).create()
396398

397399
self.session = Session(
398400
self,
@@ -437,7 +439,7 @@ async def authorize_user(self):
437439
await self.session.stop()
438440

439441
self.dc_id = e.x
440-
self.auth_key = await Auth(self.dc_id, self.test_mode, self._proxy).create()
442+
self.auth_key = await Auth(self.dc_id, self.test_mode, self.ipv6, self._proxy).create()
441443

442444
self.session = Session(
443445
self,
@@ -934,7 +936,7 @@ async def load_session(self):
934936
except FileNotFoundError:
935937
self.dc_id = 1
936938
self.date = 0
937-
self.auth_key = await Auth(self.dc_id, self.test_mode, self._proxy).create()
939+
self.auth_key = await Auth(self.dc_id, self.test_mode, self.ipv6, self._proxy).create()
938940
else:
939941
self.dc_id = s["dc_id"]
940942
self.test_mode = s["test_mode"]
@@ -1177,7 +1179,7 @@ async def get_file(self,
11771179
session = Session(
11781180
self,
11791181
dc_id,
1180-
await Auth(dc_id, self.test_mode, self._proxy).create(),
1182+
await Auth(dc_id, self.test_mode, self.ipv6, self._proxy).create(),
11811183
is_media=True
11821184
)
11831185

@@ -1262,7 +1264,7 @@ async def get_file(self,
12621264
cdn_session = Session(
12631265
self,
12641266
r.dc_id,
1265-
await Auth(r.dc_id, self.test_mode, self._proxy).create(),
1267+
await Auth(r.dc_id, self.test_mode, self.ipv6, self._proxy).create(),
12661268
is_media=True,
12671269
is_cdn=True
12681270
)

pyrogram/client/ext/base_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class BaseClient:
6363
}
6464

6565
def __init__(self):
66-
self.token = None
66+
self.bot_token = None
6767
self.dc_id = None
6868
self.auth_key = None
6969
self.user_id = None

pyrogram/connection/connection.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import logging
2121

2222
from .transport import *
23+
from ..session.internals import DataCenter
2324

2425
log = logging.getLogger(__name__)
2526

@@ -36,24 +37,30 @@ class Connection:
3637
4: TCPIntermediateO
3738
}
3839

39-
def __init__(self, address: tuple, proxy: dict, mode: int = 2):
40-
self.address = address
40+
def __init__(self, dc_id: int, test_mode: bool, ipv6: bool, proxy: dict, mode: int = 2):
41+
self.ipv6 = ipv6
4142
self.proxy = proxy
43+
self.address = DataCenter(dc_id, test_mode, ipv6)
4244
self.mode = self.MODES.get(mode, TCPAbridged)
4345

4446
self.protocol = None # type: TCP
4547

4648
async def connect(self):
4749
for i in range(Connection.MAX_RETRIES):
48-
self.protocol = self.mode(self.proxy)
50+
self.protocol = self.mode(self.ipv6, self.proxy)
4951

5052
try:
5153
log.info("Connecting...")
5254
await self.protocol.connect(self.address)
53-
except OSError:
55+
except OSError as e:
56+
log.warning(e) # TODO: Remove
5457
self.protocol.close()
5558
await asyncio.sleep(1)
5659
else:
60+
log.info("Connected! IPv{} - {}".format(
61+
"6" if self.ipv6 else "4",
62+
self.mode.__name__
63+
))
5764
break
5865
else:
5966
log.warning("Connection failed! Trying again...")

pyrogram/connection/transport/tcp/tcp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@
3636
class TCP:
3737
TIMEOUT = 10
3838

39-
def __init__(self, proxy: dict):
39+
def __init__(self, ipv6: bool, proxy: dict):
4040
self.proxy = proxy
4141

4242
self.lock = asyncio.Lock()
4343

44-
self.socket = socks.socksocket()
44+
self.socket = socks.socksocket(family=socket.AF_INET6 if ipv6 else socket.AF_INET)
45+
4546
self.socket.settimeout(TCP.TIMEOUT)
4647

4748
self.reader = None # type: asyncio.StreamReader
4849
self.writer = None # type: asyncio.StreamWriter
49-
5050
self.proxy_enabled = proxy.get("enabled", False)
5151

5252
if proxy and self.proxy_enabled:

pyrogram/connection/transport/tcp/tcp_abridged.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424

2525

2626
class TCPAbridged(TCP):
27-
def __init__(self, proxy: dict):
28-
super().__init__(proxy)
27+
def __init__(self, ipv6: bool, proxy: dict):
28+
super().__init__(ipv6, proxy)
2929

3030
def connect(self, address: tuple):
3131
super().connect(address)
3232
super().sendall(b"\xef")
3333

34-
log.info("Connected{}!".format(" with proxy" if self.proxy_enabled else ""))
35-
3634
def sendall(self, data: bytes, *args):
3735
length = len(data) // 4
3836

pyrogram/connection/transport/tcp/tcp_abridged_o.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
class TCPAbridgedO(TCP):
2929
RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4)
3030

31-
def __init__(self, proxy: dict):
32-
super().__init__(proxy)
31+
def __init__(self, ipv6: bool, proxy: dict):
32+
super().__init__(ipv6, proxy)
33+
3334
self.encrypt = None
3435
self.decrypt = None
3536

@@ -54,8 +55,6 @@ def connect(self, address: tuple):
5455

5556
super().sendall(nonce)
5657

57-
log.info("Connected{}!".format(" with proxy" if self.proxy_enabled else ""))
58-
5958
def sendall(self, data: bytes, *args):
6059
length = len(data) // 4
6160

pyrogram/connection/transport/tcp/tcp_full.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626

2727

2828
class TCPFull(TCP):
29-
def __init__(self, proxy: dict):
30-
super().__init__(proxy)
29+
def __init__(self, ipv6: bool, proxy: dict):
30+
super().__init__(ipv6, proxy)
31+
3132
self.seq_no = None
3233

3334
def connect(self, address: tuple):
3435
super().connect(address)
3536
self.seq_no = 0
36-
log.info("Connected{}!".format(" with proxy" if self.proxy_enabled else ""))
3737

3838
def sendall(self, data: bytes, *args):
3939
# 12 = packet_length (4), seq_no (4), crc32 (4) (at the end)

pyrogram/connection/transport/tcp/tcp_intermediate.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,13 @@
2525

2626

2727
class TCPIntermediate(TCP):
28-
def __init__(self, proxy: dict):
29-
super().__init__(proxy)
28+
def __init__(self, ipv6: bool, proxy: dict):
29+
super().__init__(ipv6, proxy)
3030

3131
async def connect(self, address: tuple):
3232
await super().connect(address)
3333
await super().send(b"\xee" * 4)
3434

35-
log.info("Connected{}!".format(
36-
" with proxy"
37-
if self.proxy_enabled
38-
else ""
39-
))
40-
4135
async def send(self, data: bytes, *args):
4236
await super().send(pack("<i", len(data)) + data)
4337

pyrogram/connection/transport/tcp/tcp_intermediate_o.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
class TCPIntermediateO(TCP):
3030
RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4)
3131

32-
def __init__(self, proxy: dict):
33-
super().__init__(proxy)
32+
def __init__(self, ipv6: bool, proxy: dict):
33+
super().__init__(ipv6, proxy)
34+
3435
self.encrypt = None
3536
self.decrypt = None
3637

@@ -55,8 +56,6 @@ def connect(self, address: tuple):
5556

5657
super().sendall(nonce)
5758

58-
log.info("Connected{}!".format(" with proxy" if self.proxy_enabled else ""))
59-
6059
def sendall(self, data: bytes, *args):
6160
super().sendall(
6261
AES.ctr256_encrypt(

0 commit comments

Comments
 (0)