mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 17:36:34 +03:00
More consistent with asyncio branch (style/small fixes)
Like passing an extra (invalid) dt parameter when serializing a datetime, and handling more errors in the TcpClient class.
This commit is contained in:
parent
c4e26c95f5
commit
ec4ca5dbfc
|
@ -192,10 +192,10 @@ def get_inner_text(text, entity):
|
|||
:param entity: the entity or entities that must be matched.
|
||||
:return: a single result or a list of the text surrounded by the entities.
|
||||
"""
|
||||
if not isinstance(entity, TLObject) and hasattr(entity, '__iter__'):
|
||||
if isinstance(entity, TLObject):
|
||||
entity = (entity,)
|
||||
multiple = True
|
||||
else:
|
||||
entity = [entity]
|
||||
multiple = False
|
||||
|
||||
text = text.encode(ENC)
|
||||
|
|
|
@ -3,10 +3,17 @@ This module holds a rough implementation of the C# TCP client.
|
|||
"""
|
||||
import errno
|
||||
import socket
|
||||
import time
|
||||
from datetime import timedelta
|
||||
from io import BytesIO, BufferedWriter
|
||||
from threading import Lock
|
||||
|
||||
MAX_TIMEOUT = 15 # in seconds
|
||||
CONN_RESET_ERRNOS = {
|
||||
errno.EBADF, errno.ENOTSOCK, errno.ENETUNREACH,
|
||||
errno.EINVAL, errno.ENOTCONN
|
||||
}
|
||||
|
||||
|
||||
class TcpClient:
|
||||
"""A simple TCP client to ease the work with sockets and proxies."""
|
||||
|
@ -59,6 +66,7 @@ class TcpClient:
|
|||
else:
|
||||
mode, address = socket.AF_INET, (ip, port)
|
||||
|
||||
timeout = 1
|
||||
while True:
|
||||
try:
|
||||
while not self._socket:
|
||||
|
@ -69,10 +77,12 @@ class TcpClient:
|
|||
except OSError as e:
|
||||
# There are some errors that we know how to handle, and
|
||||
# the loop will allow us to retry
|
||||
if e.errno == errno.EBADF:
|
||||
if e.errno in (errno.EBADF, errno.ENOTSOCK, errno.EINVAL):
|
||||
# Bad file descriptor, i.e. socket was closed, set it
|
||||
# to none to recreate it on the next iteration
|
||||
self._socket = None
|
||||
time.sleep(timeout)
|
||||
timeout = min(timeout * 2, MAX_TIMEOUT)
|
||||
else:
|
||||
raise
|
||||
|
||||
|
@ -105,7 +115,7 @@ class TcpClient:
|
|||
:param data: the data to send.
|
||||
"""
|
||||
if self._socket is None:
|
||||
raise ConnectionResetError()
|
||||
self._raise_connection_reset()
|
||||
|
||||
# TODO Timeout may be an issue when sending the data, Changed in v3.5:
|
||||
# The socket timeout is now the maximum total duration to send all data.
|
||||
|
@ -116,7 +126,7 @@ class TcpClient:
|
|||
except ConnectionError:
|
||||
self._raise_connection_reset()
|
||||
except OSError as e:
|
||||
if e.errno == errno.EBADF:
|
||||
if e.errno in CONN_RESET_ERRNOS:
|
||||
self._raise_connection_reset()
|
||||
else:
|
||||
raise
|
||||
|
@ -129,7 +139,7 @@ class TcpClient:
|
|||
:return: the read data with len(data) == size.
|
||||
"""
|
||||
if self._socket is None:
|
||||
raise ConnectionResetError()
|
||||
self._raise_connection_reset()
|
||||
|
||||
# TODO Remove the timeout from this method, always use previous one
|
||||
with BufferedWriter(BytesIO(), buffer_size=size) as buffer:
|
||||
|
@ -142,7 +152,7 @@ class TcpClient:
|
|||
except ConnectionError:
|
||||
self._raise_connection_reset()
|
||||
except OSError as e:
|
||||
if e.errno == errno.EBADF or e.errno == errno.ENOTSOCK:
|
||||
if e.errno in CONN_RESET_ERRNOS:
|
||||
self._raise_connection_reset()
|
||||
else:
|
||||
raise
|
||||
|
|
|
@ -317,10 +317,7 @@ class TelegramClient(TelegramBareClient):
|
|||
|
||||
# region Dialogs ("chats") requests
|
||||
|
||||
def get_dialogs(self,
|
||||
limit=10,
|
||||
offset_date=None,
|
||||
offset_id=0,
|
||||
def get_dialogs(self, limit=10, offset_date=None, offset_id=0,
|
||||
offset_peer=InputPeerEmpty()):
|
||||
"""
|
||||
Gets N "dialogs" (open "chats" or conversations with other people).
|
||||
|
@ -425,11 +422,7 @@ class TelegramClient(TelegramBareClient):
|
|||
if update.message.id == msg_id:
|
||||
return update.message
|
||||
|
||||
def send_message(self,
|
||||
entity,
|
||||
message,
|
||||
reply_to=None,
|
||||
parse_mode=None,
|
||||
def send_message(self, entity, message, reply_to=None, parse_mode=None,
|
||||
link_preview=True):
|
||||
"""
|
||||
Sends the given message to the specified entity (user/chat/channel).
|
||||
|
@ -523,14 +516,8 @@ class TelegramClient(TelegramBareClient):
|
|||
else:
|
||||
return self(messages.DeleteMessagesRequest(message_ids, revoke=revoke))
|
||||
|
||||
def get_message_history(self,
|
||||
entity,
|
||||
limit=20,
|
||||
offset_date=None,
|
||||
offset_id=0,
|
||||
max_id=0,
|
||||
min_id=0,
|
||||
add_offset=0):
|
||||
def get_message_history(self, entity, limit=20, offset_date=None,
|
||||
offset_id=0, max_id=0, min_id=0, add_offset=0):
|
||||
"""
|
||||
Gets the message history for the specified entity
|
||||
|
||||
|
|
|
@ -134,7 +134,7 @@ class TLObject:
|
|||
if isinstance(dt, datetime):
|
||||
dt = int(dt.timestamp())
|
||||
elif isinstance(dt, date):
|
||||
dt = int(datetime(dt.year, dt.month, dt.day, dt).timestamp())
|
||||
dt = int(datetime(dt.year, dt.month, dt.day).timestamp())
|
||||
elif isinstance(dt, float):
|
||||
dt = int(dt)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user