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