2017-06-08 14:12:57 +03:00
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
|
|
|
from hashlib import md5
|
2017-08-23 02:43:08 +03:00
|
|
|
from io import BytesIO
|
2017-09-04 18:18:33 +03:00
|
|
|
from os import path
|
2017-09-22 13:44:09 +03:00
|
|
|
from threading import Lock
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
from . import helpers as utils
|
2017-09-04 18:18:33 +03:00
|
|
|
from .crypto import rsa, CdnDecrypter
|
2017-07-26 17:10:45 +03:00
|
|
|
from .errors import (
|
2017-09-17 19:38:03 +03:00
|
|
|
RPCError, BrokenAuthKeyError,
|
|
|
|
FloodWaitError, FileMigrateError, TypeNotFoundError
|
2017-07-26 17:10:45 +03:00
|
|
|
)
|
2017-09-04 12:24:10 +03:00
|
|
|
from .network import authenticator, MtProtoSender, Connection, ConnectionMode
|
2017-08-29 16:59:08 +03:00
|
|
|
from .tl import TLObject, Session
|
2017-09-17 17:17:55 +03:00
|
|
|
from .tl.all_tlobjects import LAYER
|
2017-08-24 14:02:48 +03:00
|
|
|
from .tl.functions import (
|
2017-09-17 19:38:03 +03:00
|
|
|
InitConnectionRequest, InvokeWithLayerRequest
|
2017-08-24 14:02:48 +03:00
|
|
|
)
|
2017-07-04 11:21:15 +03:00
|
|
|
from .tl.functions.auth import (
|
|
|
|
ImportAuthorizationRequest, ExportAuthorizationRequest
|
|
|
|
)
|
2017-09-04 18:18:33 +03:00
|
|
|
from .tl.functions.help import (
|
|
|
|
GetCdnConfigRequest, GetConfigRequest
|
|
|
|
)
|
2017-06-08 14:12:57 +03:00
|
|
|
from .tl.functions.upload import (
|
2017-09-04 18:18:33 +03:00
|
|
|
GetFileRequest, SaveBigFilePartRequest, SaveFilePartRequest
|
2017-07-04 11:21:15 +03:00
|
|
|
)
|
2017-06-08 14:12:57 +03:00
|
|
|
from .tl.types import InputFile, InputFileBig
|
2017-09-04 18:18:33 +03:00
|
|
|
from .tl.types.upload import FileCdnRedirect
|
2017-09-07 19:49:08 +03:00
|
|
|
from .update_state import UpdateState
|
2017-09-04 18:18:33 +03:00
|
|
|
from .utils import get_appropriated_part_size
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TelegramBareClient:
|
|
|
|
"""Bare Telegram Client with just the minimum -
|
|
|
|
|
|
|
|
The reason to distinguish between a MtProtoSender and a
|
|
|
|
TelegramClient itself is because the sender is just that,
|
|
|
|
a sender, which should know nothing about Telegram but
|
|
|
|
rather how to handle this specific connection.
|
|
|
|
|
|
|
|
The TelegramClient itself should know how to initialize
|
|
|
|
a proper connection to the servers, as well as other basic
|
|
|
|
methods such as disconnection and reconnection.
|
|
|
|
|
|
|
|
This distinction between a bare client and a full client
|
|
|
|
makes it possible to create clones of the bare version
|
|
|
|
(by using the same session, IP address and port) to be
|
|
|
|
able to execute queries on either, without the additional
|
|
|
|
cost that would involve having the methods for signing in,
|
|
|
|
logging out, and such.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Current TelegramClient version
|
2017-09-23 12:44:04 +03:00
|
|
|
__version__ = '0.13.6'
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-09-17 15:30:23 +03:00
|
|
|
# TODO Make this thread-safe, all connections share the same DC
|
|
|
|
_dc_options = None
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
# region Initialization
|
|
|
|
|
2017-06-22 12:43:42 +03:00
|
|
|
def __init__(self, session, api_id, api_hash,
|
2017-09-04 12:24:10 +03:00
|
|
|
connection_mode=ConnectionMode.TCP_FULL,
|
2017-09-07 19:49:08 +03:00
|
|
|
proxy=None,
|
2017-09-08 13:54:38 +03:00
|
|
|
process_updates=False,
|
2017-09-07 19:49:08 +03:00
|
|
|
timeout=timedelta(seconds=5)):
|
2017-06-08 14:12:57 +03:00
|
|
|
"""Initializes the Telegram client with the specified API ID and Hash.
|
|
|
|
Session must always be a Session instance, and an optional proxy
|
|
|
|
can also be specified to be used on the connection.
|
|
|
|
"""
|
|
|
|
self.session = session
|
2017-06-11 23:42:04 +03:00
|
|
|
self.api_id = int(api_id)
|
2017-06-08 14:12:57 +03:00
|
|
|
self.api_hash = api_hash
|
2017-09-15 12:49:39 +03:00
|
|
|
if self.api_id < 20: # official apps must use obfuscated
|
2017-09-21 14:43:33 +03:00
|
|
|
connection_mode = ConnectionMode.TCP_OBFUSCATED
|
|
|
|
|
|
|
|
self._sender = MtProtoSender(self.session, Connection(
|
|
|
|
self.session.server_address, self.session.port,
|
|
|
|
mode=connection_mode, proxy=proxy, timeout=timeout
|
|
|
|
))
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
self._logger = logging.getLogger(__name__)
|
|
|
|
|
2017-09-22 13:20:38 +03:00
|
|
|
# Two threads may be calling reconnect() when the connection is lost,
|
|
|
|
# we only want one to actually perform the reconnection.
|
2017-09-22 13:44:09 +03:00
|
|
|
self._connect_lock = Lock()
|
2017-09-22 13:20:38 +03:00
|
|
|
|
2017-07-04 11:21:15 +03:00
|
|
|
# Cache "exported" senders 'dc_id: TelegramBareClient' and
|
|
|
|
# their corresponding sessions not to recreate them all
|
|
|
|
# the time since it's a (somewhat expensive) process.
|
|
|
|
self._cached_clients = {}
|
|
|
|
|
2017-09-07 19:49:08 +03:00
|
|
|
# This member will process updates if enabled.
|
|
|
|
# One may change self.updates.enabled at any later point.
|
2017-09-08 13:54:38 +03:00
|
|
|
self.updates = UpdateState(process_updates)
|
2017-09-02 22:45:27 +03:00
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Connecting
|
|
|
|
|
2017-09-17 17:06:43 +03:00
|
|
|
def connect(self, exported_auth=None):
|
2017-06-08 14:12:57 +03:00
|
|
|
"""Connects to the Telegram servers, executing authentication if
|
|
|
|
required. Note that authenticating to the Telegram servers is
|
|
|
|
not the same as authenticating the desired user itself, which
|
|
|
|
may require a call (or several) to 'sign_in' for the first time.
|
2017-06-09 11:35:19 +03:00
|
|
|
|
|
|
|
If 'exported_auth' is not None, it will be used instead to
|
|
|
|
determine the authorization key for the current session.
|
2017-06-08 14:12:57 +03:00
|
|
|
"""
|
|
|
|
try:
|
2017-09-21 14:43:33 +03:00
|
|
|
self._sender.connect()
|
2017-06-08 14:12:57 +03:00
|
|
|
if not self.session.auth_key:
|
2017-09-17 15:25:53 +03:00
|
|
|
# New key, we need to tell the server we're going to use
|
|
|
|
# the latest layer
|
2017-09-17 19:38:03 +03:00
|
|
|
try:
|
|
|
|
self.session.auth_key, self.session.time_offset = \
|
2017-09-21 14:43:33 +03:00
|
|
|
authenticator.do_authentication(self._sender.connection)
|
2017-09-17 19:38:03 +03:00
|
|
|
except BrokenAuthKeyError:
|
|
|
|
return False
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-09-17 17:17:55 +03:00
|
|
|
self.session.layer = LAYER
|
2017-06-08 14:12:57 +03:00
|
|
|
self.session.save()
|
2017-09-17 15:25:53 +03:00
|
|
|
init_connection = True
|
|
|
|
else:
|
2017-09-17 17:17:55 +03:00
|
|
|
init_connection = self.session.layer != LAYER
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-09-17 15:25:53 +03:00
|
|
|
if init_connection:
|
|
|
|
if exported_auth is not None:
|
|
|
|
self._init_connection(ImportAuthorizationRequest(
|
|
|
|
exported_auth.id, exported_auth.bytes
|
|
|
|
))
|
|
|
|
else:
|
2017-09-17 15:30:23 +03:00
|
|
|
TelegramBareClient._dc_options = \
|
2017-09-17 15:25:53 +03:00
|
|
|
self._init_connection(GetConfigRequest()).dc_options
|
2017-09-17 17:06:43 +03:00
|
|
|
|
|
|
|
elif exported_auth is not None:
|
|
|
|
self(ImportAuthorizationRequest(
|
|
|
|
exported_auth.id, exported_auth.bytes
|
|
|
|
))
|
|
|
|
|
|
|
|
if TelegramBareClient._dc_options is None:
|
|
|
|
TelegramBareClient._dc_options = \
|
|
|
|
self(GetConfigRequest()).dc_options
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-09-17 15:25:53 +03:00
|
|
|
return True
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-07-26 17:10:45 +03:00
|
|
|
except TypeNotFoundError as e:
|
|
|
|
# This is fine, probably layer migration
|
|
|
|
self._logger.debug('Found invalid item, probably migrating', e)
|
|
|
|
self.disconnect()
|
2017-09-17 17:20:04 +03:00
|
|
|
return self.connect(exported_auth=exported_auth)
|
2017-07-26 17:10:45 +03:00
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
except (RPCError, ConnectionError) as error:
|
|
|
|
# Probably errors from the previous session, ignore them
|
|
|
|
self.disconnect()
|
2017-09-04 18:10:04 +03:00
|
|
|
self._logger.debug(
|
|
|
|
'Could not stabilise initial connection: {}'.format(error)
|
|
|
|
)
|
2017-09-17 17:20:04 +03:00
|
|
|
return False
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-09-17 17:39:29 +03:00
|
|
|
def is_connected(self):
|
2017-09-21 14:43:33 +03:00
|
|
|
return self._sender.is_connected()
|
2017-09-17 17:39:29 +03:00
|
|
|
|
2017-09-17 15:25:53 +03:00
|
|
|
def _init_connection(self, query=None):
|
2017-09-17 17:17:55 +03:00
|
|
|
result = self(InvokeWithLayerRequest(LAYER, InitConnectionRequest(
|
2017-09-17 15:25:53 +03:00
|
|
|
api_id=self.api_id,
|
|
|
|
device_model=self.session.device_model,
|
|
|
|
system_version=self.session.system_version,
|
|
|
|
app_version=self.session.app_version,
|
|
|
|
lang_code=self.session.lang_code,
|
|
|
|
system_lang_code=self.session.system_lang_code,
|
|
|
|
lang_pack='', # "langPacks are for official apps only"
|
|
|
|
query=query
|
|
|
|
)))
|
2017-09-17 17:17:55 +03:00
|
|
|
self.session.layer = LAYER
|
2017-09-17 15:25:53 +03:00
|
|
|
self.session.save()
|
|
|
|
return result
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
def disconnect(self):
|
|
|
|
"""Disconnects from the Telegram server"""
|
2017-09-21 14:43:33 +03:00
|
|
|
self._sender.disconnect()
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-09-22 13:31:41 +03:00
|
|
|
def _reconnect(self, new_dc=None):
|
2017-09-22 13:20:38 +03:00
|
|
|
"""If 'new_dc' is not set, only a call to .connect() will be made
|
|
|
|
since it's assumed that the connection has been lost and the
|
|
|
|
library is reconnecting.
|
2017-06-08 17:51:20 +03:00
|
|
|
|
2017-09-22 13:20:38 +03:00
|
|
|
If 'new_dc' is set, the client is first disconnected from the
|
|
|
|
current data center, clears the auth key for the old DC, and
|
|
|
|
connects to the new data center.
|
2017-06-08 17:51:20 +03:00
|
|
|
"""
|
2017-09-22 13:20:38 +03:00
|
|
|
if new_dc is None:
|
|
|
|
# Assume we are disconnected due to some error, so connect again
|
|
|
|
with self._connect_lock:
|
|
|
|
# Another thread may have connected again, so check that first
|
|
|
|
if not self.is_connected():
|
2017-09-22 13:45:14 +03:00
|
|
|
return self.connect()
|
|
|
|
else:
|
|
|
|
return True
|
2017-09-22 13:20:38 +03:00
|
|
|
else:
|
|
|
|
self.disconnect()
|
2017-06-08 17:51:20 +03:00
|
|
|
self.session.auth_key = None # Force creating new auth_key
|
|
|
|
dc = self._get_dc(new_dc)
|
2017-09-21 14:58:57 +03:00
|
|
|
ip = dc.ip_address
|
|
|
|
self._sender.connection.ip = self.session.server_address = ip
|
|
|
|
self._sender.connection.port = self.session.port = dc.port
|
2017-06-08 17:51:20 +03:00
|
|
|
self.session.save()
|
2017-09-22 13:45:14 +03:00
|
|
|
return self.connect()
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Working with different Data Centers
|
|
|
|
|
2017-08-29 16:59:08 +03:00
|
|
|
def _get_dc(self, dc_id, ipv6=False, cdn=False):
|
2017-06-08 14:12:57 +03:00
|
|
|
"""Gets the Data Center (DC) associated to 'dc_id'"""
|
2017-09-17 15:30:23 +03:00
|
|
|
if TelegramBareClient._dc_options is None:
|
2017-06-08 14:12:57 +03:00
|
|
|
raise ConnectionError(
|
|
|
|
'Cannot determine the required data center IP address. '
|
|
|
|
'Stabilise a successful initial connection first.')
|
|
|
|
|
2017-08-24 14:02:48 +03:00
|
|
|
try:
|
2017-09-05 17:11:02 +03:00
|
|
|
if cdn:
|
|
|
|
# Ensure we have the latest keys for the CDNs
|
|
|
|
for pk in self(GetCdnConfigRequest()).public_keys:
|
|
|
|
rsa.add_key(pk.public_key)
|
|
|
|
|
2017-08-29 14:49:41 +03:00
|
|
|
return next(
|
2017-09-17 15:30:23 +03:00
|
|
|
dc for dc in TelegramBareClient._dc_options if dc.id == dc_id
|
|
|
|
and bool(dc.ipv6) == ipv6 and bool(dc.cdn) == cdn
|
2017-08-29 14:49:41 +03:00
|
|
|
)
|
2017-08-24 14:02:48 +03:00
|
|
|
except StopIteration:
|
|
|
|
if not cdn:
|
|
|
|
raise
|
|
|
|
|
2017-09-05 17:11:02 +03:00
|
|
|
# New configuration, perhaps a new CDN was added?
|
2017-09-17 15:30:23 +03:00
|
|
|
TelegramBareClient._dc_options = self(GetConfigRequest()).dc_options
|
2017-08-29 14:49:41 +03:00
|
|
|
return self._get_dc(dc_id, ipv6=ipv6, cdn=cdn)
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-07-04 11:21:15 +03:00
|
|
|
def _get_exported_client(self, dc_id,
|
|
|
|
init_connection=False,
|
|
|
|
bypass_cache=False):
|
|
|
|
"""Gets a cached exported TelegramBareClient for the desired DC.
|
|
|
|
|
|
|
|
If it's the first time retrieving the TelegramBareClient, the
|
|
|
|
current authorization is exported to the new DC so that
|
|
|
|
it can be used there, and the connection is initialized.
|
|
|
|
|
|
|
|
If after using the sender a ConnectionResetError is raised,
|
|
|
|
this method should be called again with init_connection=True
|
|
|
|
in order to perform the reconnection.
|
|
|
|
|
|
|
|
If bypass_cache is True, a new client will be exported and
|
|
|
|
it will not be cached.
|
|
|
|
"""
|
|
|
|
# Thanks badoualy/kotlogram on /telegram/api/DefaultTelegramClient.kt
|
|
|
|
# for clearly showing how to export the authorization! ^^
|
|
|
|
client = self._cached_clients.get(dc_id)
|
|
|
|
if client and not bypass_cache:
|
|
|
|
if init_connection:
|
|
|
|
client.reconnect()
|
|
|
|
return client
|
|
|
|
else:
|
|
|
|
dc = self._get_dc(dc_id)
|
|
|
|
|
|
|
|
# Export the current authorization to the new DC.
|
|
|
|
export_auth = self(ExportAuthorizationRequest(dc_id))
|
|
|
|
|
|
|
|
# Create a temporary session for this IP address, which needs
|
|
|
|
# to be different because each auth_key is unique per DC.
|
|
|
|
#
|
|
|
|
# Construct this session with the connection parameters
|
|
|
|
# (system version, device model...) from the current one.
|
2017-08-29 16:59:08 +03:00
|
|
|
session = Session(self.session)
|
2017-07-04 11:21:15 +03:00
|
|
|
session.server_address = dc.ip_address
|
|
|
|
session.port = dc.port
|
|
|
|
client = TelegramBareClient(
|
|
|
|
session, self.api_id, self.api_hash,
|
2017-09-22 17:11:07 +03:00
|
|
|
proxy=self._sender.connection.conn.proxy,
|
|
|
|
timeout=self._sender.connection.get_timeout()
|
2017-07-04 11:21:15 +03:00
|
|
|
)
|
|
|
|
client.connect(exported_auth=export_auth)
|
|
|
|
|
|
|
|
if not bypass_cache:
|
|
|
|
# Don't go through this expensive process every time.
|
|
|
|
self._cached_clients[dc_id] = client
|
|
|
|
return client
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Invoking Telegram requests
|
|
|
|
|
2017-09-11 11:52:36 +03:00
|
|
|
def invoke(self, request, call_receive=True, retries=5):
|
2017-06-08 14:12:57 +03:00
|
|
|
"""Invokes (sends) a MTProtoRequest and returns (receives) its result.
|
|
|
|
|
|
|
|
If 'updates' is not None, all read update object will be put
|
|
|
|
in such list. Otherwise, update objects will be ignored.
|
2017-09-03 10:56:10 +03:00
|
|
|
|
|
|
|
If 'call_receive' is set to False, then there should be another
|
|
|
|
thread calling to 'self._sender.receive()' running or this method
|
|
|
|
will lock forever.
|
2017-06-08 14:12:57 +03:00
|
|
|
"""
|
2017-07-24 17:54:48 +03:00
|
|
|
if not isinstance(request, TLObject) and not request.content_related:
|
|
|
|
raise ValueError('You can only invoke requests, not types!')
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-09-11 11:52:36 +03:00
|
|
|
if retries <= 0:
|
|
|
|
raise ValueError('Number of retries reached 0.')
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
try:
|
2017-09-03 14:45:13 +03:00
|
|
|
# Ensure that we start with no previous errors (i.e. resending)
|
|
|
|
request.confirm_received.clear()
|
|
|
|
request.rpc_error = None
|
|
|
|
|
2017-06-22 12:43:42 +03:00
|
|
|
self._sender.send(request)
|
2017-09-03 10:56:10 +03:00
|
|
|
if not call_receive:
|
2017-09-02 22:27:11 +03:00
|
|
|
# TODO This will be slightly troublesome if we allow
|
|
|
|
# switching between constant read or not on the fly.
|
|
|
|
# Must also watch out for calling .read() from two places,
|
|
|
|
# in which case a Lock would be required for .receive().
|
2017-09-22 20:20:37 +03:00
|
|
|
request.confirm_received.wait(
|
|
|
|
self._sender.connection.get_timeout()
|
|
|
|
)
|
2017-09-02 22:27:11 +03:00
|
|
|
else:
|
|
|
|
while not request.confirm_received.is_set():
|
2017-09-07 19:49:08 +03:00
|
|
|
self._sender.receive(update_state=self.updates)
|
2017-09-02 22:27:11 +03:00
|
|
|
|
2017-09-17 17:32:51 +03:00
|
|
|
except TimeoutError:
|
|
|
|
pass # We will just retry
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
except ConnectionResetError:
|
2017-07-10 16:21:20 +03:00
|
|
|
self._logger.debug('Server disconnected us. Reconnecting and '
|
2017-08-24 14:02:48 +03:00
|
|
|
'resending request...')
|
2017-09-22 13:31:41 +03:00
|
|
|
self._reconnect()
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
except FloodWaitError:
|
|
|
|
self.disconnect()
|
|
|
|
raise
|
|
|
|
|
2017-09-11 11:52:36 +03:00
|
|
|
if request.rpc_error:
|
|
|
|
raise request.rpc_error
|
|
|
|
if request.result is None:
|
|
|
|
return self.invoke(
|
|
|
|
request, call_receive=call_receive, retries=(retries - 1)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return request.result
|
|
|
|
|
2017-07-02 12:56:40 +03:00
|
|
|
# Let people use client(SomeRequest()) instead client.invoke(...)
|
|
|
|
__call__ = invoke
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Uploading media
|
|
|
|
|
|
|
|
def upload_file(self,
|
2017-08-23 02:43:08 +03:00
|
|
|
file,
|
2017-06-08 14:12:57 +03:00
|
|
|
part_size_kb=None,
|
|
|
|
file_name=None,
|
|
|
|
progress_callback=None):
|
2017-08-23 02:43:08 +03:00
|
|
|
"""Uploads the specified file and returns a handle (an instance
|
2017-06-08 14:12:57 +03:00
|
|
|
of InputFile or InputFileBig, as required) which can be later used.
|
|
|
|
|
2017-08-23 01:27:33 +03:00
|
|
|
Uploading a file will simply return a "handle" to the file stored
|
|
|
|
remotely in the Telegram servers, which can be later used on. This
|
|
|
|
will NOT upload the file to your own chat.
|
|
|
|
|
2017-08-23 02:43:08 +03:00
|
|
|
'file' may be either a file path, a byte array, or a stream.
|
|
|
|
Note that if the file is a stream it will need to be read
|
|
|
|
entirely into memory to tell its size first.
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
If 'progress_callback' is not None, it should be a function that
|
|
|
|
takes two parameters, (bytes_uploaded, total_bytes).
|
|
|
|
|
|
|
|
Default values for the optional parameters if left as None are:
|
|
|
|
part_size_kb = get_appropriated_part_size(file_size)
|
|
|
|
file_name = path.basename(file_path)
|
|
|
|
"""
|
2017-08-23 02:43:08 +03:00
|
|
|
if isinstance(file, str):
|
|
|
|
file_size = path.getsize(file)
|
|
|
|
elif isinstance(file, bytes):
|
|
|
|
file_size = len(file)
|
|
|
|
else:
|
|
|
|
file = file.read()
|
|
|
|
file_size = len(file)
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
if not part_size_kb:
|
|
|
|
part_size_kb = get_appropriated_part_size(file_size)
|
|
|
|
|
|
|
|
if part_size_kb > 512:
|
|
|
|
raise ValueError('The part size must be less or equal to 512KB')
|
|
|
|
|
|
|
|
part_size = int(part_size_kb * 1024)
|
|
|
|
if part_size % 1024 != 0:
|
|
|
|
raise ValueError('The part size must be evenly divisible by 1024')
|
|
|
|
|
|
|
|
# Determine whether the file is too big (over 10MB) or not
|
|
|
|
# Telegram does make a distinction between smaller or larger files
|
|
|
|
is_large = file_size > 10 * 1024 * 1024
|
|
|
|
part_count = (file_size + part_size - 1) // part_size
|
|
|
|
|
|
|
|
file_id = utils.generate_random_long()
|
|
|
|
hash_md5 = md5()
|
|
|
|
|
2017-08-23 02:43:08 +03:00
|
|
|
stream = open(file, 'rb') if isinstance(file, str) else BytesIO(file)
|
|
|
|
try:
|
2017-06-08 14:12:57 +03:00
|
|
|
for part_index in range(part_count):
|
|
|
|
# Read the file by in chunks of size part_size
|
2017-08-23 02:43:08 +03:00
|
|
|
part = stream.read(part_size)
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
# The SavePartRequest is different depending on whether
|
|
|
|
# the file is too large or not (over or less than 10MB)
|
|
|
|
if is_large:
|
|
|
|
request = SaveBigFilePartRequest(file_id, part_index,
|
|
|
|
part_count, part)
|
|
|
|
else:
|
|
|
|
request = SaveFilePartRequest(file_id, part_index, part)
|
|
|
|
|
2017-07-02 12:56:40 +03:00
|
|
|
result = self(request)
|
2017-06-08 14:12:57 +03:00
|
|
|
if result:
|
|
|
|
if not is_large:
|
|
|
|
# No need to update the hash if it's a large file
|
|
|
|
hash_md5.update(part)
|
|
|
|
|
|
|
|
if progress_callback:
|
2017-08-23 02:43:08 +03:00
|
|
|
progress_callback(stream.tell(), file_size)
|
2017-06-08 14:12:57 +03:00
|
|
|
else:
|
|
|
|
raise ValueError('Failed to upload file part {}.'
|
|
|
|
.format(part_index))
|
2017-08-23 02:43:08 +03:00
|
|
|
finally:
|
|
|
|
stream.close()
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
# Set a default file name if None was specified
|
|
|
|
if not file_name:
|
2017-08-23 02:43:08 +03:00
|
|
|
if isinstance(file, str):
|
|
|
|
file_name = path.basename(file)
|
|
|
|
else:
|
|
|
|
file_name = str(file_id)
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
if is_large:
|
|
|
|
return InputFileBig(file_id, part_count, file_name)
|
|
|
|
else:
|
|
|
|
return InputFile(file_id, part_count, file_name,
|
|
|
|
md5_checksum=hash_md5.hexdigest())
|
|
|
|
|
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Downloading media
|
|
|
|
|
|
|
|
def download_file(self,
|
2017-06-09 11:35:19 +03:00
|
|
|
input_location,
|
2017-06-09 12:12:56 +03:00
|
|
|
file,
|
2017-06-09 11:35:19 +03:00
|
|
|
part_size_kb=None,
|
|
|
|
file_size=None,
|
|
|
|
progress_callback=None):
|
2017-06-09 12:12:56 +03:00
|
|
|
"""Downloads the given InputFileLocation to file (a stream or str).
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
If 'progress_callback' is not None, it should be a function that
|
|
|
|
takes two parameters, (bytes_downloaded, total_bytes). Note that
|
2017-06-09 11:35:19 +03:00
|
|
|
'total_bytes' simply equals 'file_size', and may be None.
|
|
|
|
"""
|
2017-06-08 14:12:57 +03:00
|
|
|
if not part_size_kb:
|
|
|
|
if not file_size:
|
|
|
|
part_size_kb = 64 # Reasonable default
|
|
|
|
else:
|
|
|
|
part_size_kb = get_appropriated_part_size(file_size)
|
|
|
|
|
|
|
|
part_size = int(part_size_kb * 1024)
|
2017-08-24 14:15:33 +03:00
|
|
|
# https://core.telegram.org/api/files says:
|
|
|
|
# > part_size % 1024 = 0 (divisible by 1KB)
|
|
|
|
#
|
|
|
|
# But https://core.telegram.org/cdn (more recent) says:
|
|
|
|
# > limit must be divisible by 4096 bytes
|
|
|
|
# So we just stick to the 4096 limit.
|
|
|
|
if part_size % 4096 != 0:
|
|
|
|
raise ValueError('The part size must be evenly divisible by 4096.')
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-06-09 12:12:56 +03:00
|
|
|
if isinstance(file, str):
|
|
|
|
# Ensure that we'll be able to download the media
|
|
|
|
utils.ensure_parent_dir_exists(file)
|
|
|
|
f = open(file, 'wb')
|
|
|
|
else:
|
|
|
|
f = file
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-07-04 11:21:15 +03:00
|
|
|
# The used client will change if FileMigrateError occurs
|
|
|
|
client = self
|
2017-08-28 17:33:23 +03:00
|
|
|
cdn_decrypter = None
|
2017-07-04 11:21:15 +03:00
|
|
|
|
2017-06-09 12:12:56 +03:00
|
|
|
try:
|
|
|
|
offset_index = 0
|
2017-06-08 14:12:57 +03:00
|
|
|
while True:
|
|
|
|
offset = offset_index * part_size
|
2017-07-04 11:21:15 +03:00
|
|
|
|
|
|
|
try:
|
2017-08-28 17:25:10 +03:00
|
|
|
if cdn_decrypter:
|
2017-09-05 17:43:53 +03:00
|
|
|
result = cdn_decrypter.get_file()
|
2017-08-24 14:02:48 +03:00
|
|
|
else:
|
|
|
|
result = client(GetFileRequest(
|
|
|
|
input_location, offset, part_size
|
|
|
|
))
|
|
|
|
|
2017-08-24 19:00:27 +03:00
|
|
|
if isinstance(result, FileCdnRedirect):
|
2017-08-28 17:25:10 +03:00
|
|
|
cdn_decrypter, result = \
|
|
|
|
CdnDecrypter.prepare_decrypter(
|
2017-09-05 17:43:53 +03:00
|
|
|
client, TelegramBareClient, result
|
2017-09-04 18:10:04 +03:00
|
|
|
)
|
2017-08-24 14:02:48 +03:00
|
|
|
|
2017-07-04 11:21:15 +03:00
|
|
|
except FileMigrateError as e:
|
|
|
|
client = self._get_exported_client(e.new_dc)
|
|
|
|
continue
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
offset_index += 1
|
|
|
|
|
|
|
|
# If we have received no data (0 bytes), the file is over
|
|
|
|
# So there is nothing left to download and write
|
|
|
|
if not result.bytes:
|
2017-08-28 17:25:10 +03:00
|
|
|
# Return some extra information, unless it's a CDN file
|
|
|
|
return getattr(result, 'type', '')
|
2017-08-28 15:17:31 +03:00
|
|
|
|
|
|
|
f.write(result.bytes)
|
2017-06-08 14:12:57 +03:00
|
|
|
if progress_callback:
|
2017-06-09 12:12:56 +03:00
|
|
|
progress_callback(f.tell(), file_size)
|
|
|
|
finally:
|
2017-08-28 17:33:23 +03:00
|
|
|
if cdn_decrypter:
|
2017-09-04 18:10:04 +03:00
|
|
|
try:
|
|
|
|
cdn_decrypter.client.disconnect()
|
|
|
|
except:
|
|
|
|
pass
|
2017-06-09 12:12:56 +03:00
|
|
|
if isinstance(file, str):
|
|
|
|
f.close()
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
# endregion
|