|
5 | 5 | # |
6 | 6 | # Pyrogram is free software: you can redistribute it and/or modify |
7 | 7 | # it under the terms of the GNU Lesser General Public License as published |
8 | | -# by the Free Software 我给他職5日4还有个63 摩羯男 Foundation, either version 3 of the License, or |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
9 | 9 | # (at your option) any later version. |
10 | 10 | # |
11 | 11 | # Pyrogram is distributed in the hope that it will be useful, |
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | -# MERCHANTABILITY or FITNESS FOR5A PARTICULAR PURPOSE. See the |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | 14 | # GNU Lesser General Public License for more details. |
15 | 15 | # |
16 | 16 | # You should have received a copy of the GNU Lesser General Public License |
@@ -119,9 +119,10 @@ class Client(Methods): |
119 | 119 | after which the server address will be updated (works both ways). |
120 | 120 | Defaults to False (IPv4). |
121 | 121 |
|
122 | | - proxy (``dict``, *optional*): |
| 122 | + proxy (``dict`` | ``str``, *optional*): |
123 | 123 | The Proxy settings as dict. |
124 | | - E.g.: *dict(scheme="socks5", hostname="11.22.33.44", port=1234, username="user", password="pass")*. |
| 124 | + E.g.: *dict(scheme="socks5", hostname="11.22.33.44", port=1234, username="user", password="pass")* |
| 125 | + or *"http://11.22.33.44:1234"* or *"socks5://user:pass@11.22.33.44:1234"*. |
125 | 126 | The *username* and *password* can be omitted if the proxy doesn't require authorization. |
126 | 127 |
|
127 | 128 | test_mode (``bool``, *optional*): |
@@ -180,6 +181,7 @@ class Client(Methods): |
180 | 181 |
|
181 | 182 | skip_updates (``bool``, *optional*): |
182 | 183 | Pass True to skip pending updates that arrived while the client was offline. |
| 184 | + Doesn't work if *in_memory* is set to True. |
183 | 185 | Defaults to True. |
184 | 186 |
|
185 | 187 | takeout (``bool``, *optional*): |
@@ -287,7 +289,7 @@ def __init__( |
287 | 289 | lang_code: str = LANG_CODE, |
288 | 290 | system_lang_code: str = SYSTEM_LANG_CODE, |
289 | 291 | ipv6: Optional[bool] = False, |
290 | | - proxy: Optional[dict] = None, |
| 292 | + proxy: Optional[Union[dict, str]] = None, |
291 | 293 | test_mode: Optional[bool] = False, |
292 | 294 | bot_token: Optional[str] = None, |
293 | 295 | session_string: Optional[str] = None, |
@@ -382,10 +384,7 @@ def __init__( |
382 | 384 | self.dispatcher: Dispatcher = Dispatcher(self) |
383 | 385 |
|
384 | 386 | self.rnd_id = MsgId |
385 | | - self._last_sync_time = time.time() |
386 | | - self._last_monotonic = time.monotonic() |
387 | | - |
388 | | - self._is_server_time_synced = False |
| 387 | + self._server_time_offset = 0.0 |
389 | 388 |
|
390 | 389 | self.parser: Parser = Parser(self) |
391 | 390 |
|
@@ -425,10 +424,20 @@ def __init__( |
425 | 424 | if isinstance(loop, asyncio.AbstractEventLoop): |
426 | 425 | self.loop = loop |
427 | 426 | else: |
428 | | - self.loop = utils.get_event_loop() |
| 427 | + self.loop = None |
429 | 428 |
|
430 | 429 | self.__config: "raw.types.Config" = None |
431 | 430 |
|
| 431 | + @property |
| 432 | + def loop(self) -> asyncio.AbstractEventLoop: |
| 433 | + if not self._loop: |
| 434 | + self._loop = utils.get_event_loop() |
| 435 | + return self._loop |
| 436 | + |
| 437 | + @loop.setter |
| 438 | + def loop(self, value: asyncio.AbstractEventLoop): |
| 439 | + self._loop = value |
| 440 | + |
432 | 441 | self.listeners = {listener_type: [] for listener_type in pyrogram.enums.ListenerTypes} |
433 | 442 |
|
434 | 443 | self.listeners = {listener_type: [] for listener_type in pyrogram.enums.ListenerTypes} |
@@ -633,12 +642,12 @@ async def authorize(self) -> User: |
633 | 642 | return signed_up |
634 | 643 |
|
635 | 644 | async def authorize_qr(self, except_ids: List[int] = []) -> "User": |
636 | | - import qrcode |
| 645 | + from qrcode import QRCode |
637 | 646 |
|
638 | 647 | qr_login = QRLogin(self, except_ids) |
639 | 648 | await qr_login.recreate() |
640 | 649 |
|
641 | | - qr = qrcode.QRCode(version=1) |
| 650 | + qr = QRCode(version=1) |
642 | 651 |
|
643 | 652 | while True: |
644 | 653 | try: |
@@ -822,7 +831,7 @@ async def handle_updates(self, updates): |
822 | 831 | pts = getattr(update, "pts", None) |
823 | 832 | pts_count = getattr(update, "pts_count", None) |
824 | 833 |
|
825 | | - if pts and not self.skip_updates: |
| 834 | + if pts: |
826 | 835 | await self.storage.update_state( |
827 | 836 | ( |
828 | 837 | utils.get_channel_id(channel_id) if channel_id else 0, |
@@ -864,16 +873,15 @@ async def handle_updates(self, updates): |
864 | 873 |
|
865 | 874 | self.dispatcher.updates_queue.put_nowait((update, users, chats)) |
866 | 875 | elif isinstance(updates, (raw.types.UpdateShortMessage, raw.types.UpdateShortChatMessage)): |
867 | | - if not self.skip_updates: |
868 | | - await self.storage.update_state( |
869 | | - ( |
870 | | - 0, |
871 | | - updates.pts, |
872 | | - None, |
873 | | - updates.date, |
874 | | - None |
875 | | - ) |
| 876 | + await self.storage.update_state( |
| 877 | + ( |
| 878 | + 0, |
| 879 | + updates.pts, |
| 880 | + None, |
| 881 | + updates.date, |
| 882 | + None |
876 | 883 | ) |
| 884 | + ) |
877 | 885 |
|
878 | 886 | diff = await self.invoke( |
879 | 887 | raw.functions.updates.GetDifference( |
@@ -1536,16 +1544,12 @@ async def set_dc( |
1536 | 1544 |
|
1537 | 1545 | @property |
1538 | 1546 | def server_time(self) -> float: |
1539 | | - return self._last_sync_time + (time.monotonic() - self._last_monotonic) |
| 1547 | + return time.time() + self._server_time_offset |
1540 | 1548 |
|
1541 | 1549 | def _set_server_time(self, msg_id: int): |
1542 | | - if self._is_server_time_synced: |
1543 | | - return |
1544 | | - |
1545 | | - self._last_sync_time = msg_id / float(2**32) |
1546 | | - self._last_monotonic = time.monotonic() |
1547 | | - self._is_server_time_synced = True |
1548 | | - log.info(f"Time synced: {utils.timestamp_to_datetime(self._last_sync_time)}") |
| 1550 | + server_ts = msg_id / float(2**32) |
| 1551 | + self._server_time_offset = server_ts - time.time() |
| 1552 | + log.info(f"Time synced: offset={self._server_time_offset:.3f}s, server_time={utils.timestamp_to_datetime(server_ts)}") |
1549 | 1553 |
|
1550 | 1554 | def guess_mime_type(self, filename: Union[str, BytesIO]) -> Optional[str]: |
1551 | 1555 | if isinstance(filename, BytesIO): |
|
0 commit comments