Skip to content

Commit d264297

Browse files
committed
Add use_slow_upload & use_slow_download switches
Telegram become more aggressive to the fast download/upload functions from the 'fastelethon.py' module (issue #30). I added ability to switch to the default functions from the Telethon library. If Telegram will continue its pressure - this will be useful. Currently you can try to lower amount of up/down files at the same time. Fast functions is pretty stable without spam.
1 parent cf5d837 commit d264297

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

tgbox/api/remote.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -909,8 +909,8 @@ async def search_file(
909909
async def _push_file(
910910
self, pf: 'PreparedFile',
911911
progress_callback: Optional[Callable[[int, int], None]] = None,
912-
message_to_edit: Optional[Union[int, Message]] = None
913-
) -> 'DecryptedRemoteBoxFile':
912+
message_to_edit: Optional[Union[int, Message]] = None,
913+
use_slow_upload: Optional[bool] = False) -> 'DecryptedRemoteBoxFile':
914914
"""
915915
Uploads ``PreparedFile`` to the ``RemoteBox``
916916
or updates already uploaded file in ``RemoteBox``.
@@ -927,6 +927,11 @@ async def _push_file(
927927
message_to_edit (``Union[int, Message]``, optional):
928928
If specified, will update existing ``RemoteBox``
929929
(edit) file instead of uploading new.
930+
931+
use_slow_upload (``bool``, optional):
932+
Will use default upload function from the Telethon
933+
library instead of function from `fastelethon.py`.
934+
Use this if you have problems with upload.
930935
"""
931936
if message_to_edit:
932937
logger.info(
@@ -953,6 +958,8 @@ async def _push_file(
953958
oe = OpenPretender(pf.file, aes_state, pf.filesize)
954959
oe.concat_metadata(pf.metadata)
955960
try:
961+
assert not use_slow_upload, 'use_slow_upload enabled'
962+
956963
# Here we will use fast upload function
957964
ifile = await upload_file(
958965
self._tc, oe,
@@ -965,7 +972,8 @@ async def _push_file(
965972
# probably because of fast "upload_file(...)" from
966973
# the custom fastelethon module. We will try to
967974
# use the slow upload from the Telethon library
968-
logger.warning(f'Fast upload FAILED, falling to SLOW!\n{format_exc()}')
975+
if not isinstance(e, AssertionError): # We assert if use_slow_upload
976+
logger.warning(f'Fast upload FAILED, trying with SLOW!\n{format_exc()}')
969977

970978
ifile = await self._tc.upload_file(
971979
oe, file_name=urlsafe_b64encode(pf.filesalt.salt).decode(),
@@ -1017,7 +1025,8 @@ async def _push_file(
10171025

10181026
async def push_file(
10191027
self, pf: 'PreparedFile',
1020-
progress_callback: Optional[Callable[[int, int], None]] = None
1028+
progress_callback: Optional[Callable[[int, int], None]] = None,
1029+
use_slow_upload: Optional[bool] = False,
10211030
) -> 'DecryptedRemoteBoxFile':
10221031
"""
10231032
Uploads ``PreparedFile`` to the ``RemoteBox``.
@@ -1031,15 +1040,19 @@ async def push_file(
10311040
A callback function accepting two parameters:
10321041
(downloaded_bytes, total).
10331042
1043+
use_slow_upload (``bool``, optional):
1044+
Will use default upload function from the Telethon
1045+
library instead of function from `fastelethon.py`.
1046+
Use this if you have problems with upload.
10341047
"""
10351048
return await self._push_file(pf,
10361049
progress_callback=progress_callback)
10371050

10381051
async def update_file(self,
10391052
rbf: Union['EncryptedRemoteBoxFile', 'DecryptedRemoteBoxFile'],
10401053
pf: 'PreparedFile',
1041-
progress_callback: Optional[Callable[[int, int], None]] = None
1042-
) -> 'DecryptedRemoteBoxFile':
1054+
progress_callback: Optional[Callable[[int, int], None]] = None,
1055+
use_slow_upload: Optional[bool] = False) -> 'DecryptedRemoteBoxFile':
10431056
"""
10441057
Updates already uploaded ``RemoteBox`` file.
10451058
This will make a full reupload and ``Message`` edit.
@@ -1057,6 +1070,11 @@ async def update_file(self,
10571070
progress_callback (``Callable[[int, int], None]``, optional):
10581071
A callback function accepting two parameters:
10591072
(downloaded_bytes, total).
1073+
1074+
use_slow_upload (``bool``, optional):
1075+
Will use default upload function from the Telethon
1076+
library instead of function from `fastelethon.py`.
1077+
Use this if you have problems with upload.
10601078
"""
10611079
if rbf is None:
10621080
raise RemoteFileNotFound(
@@ -1066,7 +1084,8 @@ async def update_file(self,
10661084
)
10671085
return await self._push_file(pf,
10681086
message_to_edit=rbf._message,
1069-
progress_callback=progress_callback)
1087+
progress_callback=progress_callback,
1088+
use_slow_upload=use_slow_upload)
10701089

10711090
async def delete_files(
10721091
self,
@@ -2169,7 +2188,8 @@ async def download(
21692188
hide_folder: bool=False, hide_name: bool=False,
21702189
decrypt: bool=True, request_size: int=524288,
21712190
offset: Optional[int] = None,
2172-
progress_callback: Optional[Callable[[int, int], None]] = None) -> BinaryIO:
2191+
progress_callback: Optional[Callable[[int, int], None]] = None,
2192+
use_slow_download: Optional[bool] = False) -> BinaryIO:
21732193
"""
21742194
Downloads and saves remote box file to the ``outfile``.
21752195
@@ -2214,6 +2234,11 @@ async def download(
22142234
progress_callback (``Callable[[int, int], None]``, optional):
22152235
A callback function accepting two parameters:
22162236
(downloaded_bytes, total).
2237+
2238+
use_slow_download (``bool``, optional):
2239+
Will use default download function from the Telethon
2240+
library instead of function from `fastelethon.py`.
2241+
Use this if you have problems with download.
22172242
"""
22182243
self.__raise_initialized()
22192244

@@ -2257,7 +2282,9 @@ async def download(
22572282

22582283
logger.debug(f'outfile is {outfile}')
22592284

2260-
use_slow_download_switch = 0
2285+
# '0' is the fast download from the fastelethon.py, and '1'
2286+
# is the default download from Telethon library.
2287+
download_error_switch = 1 if use_slow_download else 0
22612288

22622289
stream_iv = self._file_iv if not offset else b'' # We know IV if (not offset)
22632290
aws = None # Placeholder for AESwState class, we will set it later
@@ -2300,11 +2327,11 @@ async def download(
23002327
try:
23012328
# By default we will try to download file via the
23022329
# fast "download_file" coroutine from fastelethon
2303-
# module. If it fails, the "use_slow_download_switch"
2330+
# module. If it fails, the "download_error_switch"
23042331
# will be incremented and this code will be switched
23052332
# to the default "iter_download" from TelegramClient;
23062333
# If it will fail too, -- we will raise an error.
2307-
if use_slow_download_switch == 0:
2334+
if download_error_switch == 0:
23082335
iter_down = download_file(
23092336
client = self._rb._tc,
23102337
location = self._message.document,
@@ -2357,14 +2384,14 @@ async def download(
23572384
break # Download is successfull so we can exit this loop
23582385

23592386
except Exception as e:
2360-
if use_slow_download_switch < 1:
2361-
use_slow_download_switch += 1
2387+
if download_error_switch == 0:
2388+
download_error_switch = 1
23622389
logger.warning(
2363-
'''Fast download FAILED. Trying to use SLOW. '''
2364-
f'''Traceback:\n{format_exc()}''')
2390+
'''Fast download FAILED. Trying with SLOW!\n'''
2391+
f'''{format_exc()}''')
23652392
continue
23662393
else:
2367-
logger.error('Fast & Slow download methods failed')
2394+
logger.error('Both fast and slow download methods failed')
23682395
raise e
23692396

23702397
return outfile

0 commit comments

Comments
 (0)