diff --git a/telecup_cli.py b/telecup_cli.py index 0c5d10c..977ed27 100644 --- a/telecup_cli.py +++ b/telecup_cli.py @@ -13,6 +13,7 @@ from concurrent.futures.thread import ThreadPoolExecutor from functools import wraps import click +from telethon.errors import BadRequestError from telethon.helpers import generate_random_long from telethon import TelegramClient, connection from telethon.tl.custom.message import Message @@ -27,7 +28,7 @@ MESSAGE_HEADER = f'TeleCup File Uploader https://lnurl.ru/telecup' MESSAGE_BLOCK_END = '_cup_end_' UploadInfo = namedtuple('UploadInfo', ['file_list', 'real_size']) -DownloadInfo = namedtuple('DownloadInfo', ['message', 'part_info']) +DownloadInfo = typing.NamedTuple('DownloadInfo', [('message', Message), ('part_info', dict)]) class Config: @@ -227,13 +228,20 @@ async def download_part(client: TelegramClient, dest: typing.BinaryIO, dInfo: Do extraBytes = pow(2, 12) - limit % pow(2, 12) limit += extraBytes - downloadResult: File = await client(GetFileRequest( - inputFileLocation, - offset, - limit, - precise=False, - cdn_supported=False - )) + try: + downloadResult: File = await client(GetFileRequest( + inputFileLocation, + offset, + limit, + precise=False, + cdn_supported=False + )) + except BadRequestError as e: + if 'expire' in str(e.message).lower(): + click.echo('Reloading message...', err=True) + newMessage = await client.get_messages(ids=dInfo.message.id) + dcId, inputFileLocation = get_input_location(newMessage) + continue buffer = downloadResult.bytes bufLen = len(buffer) totalBytesDownloaded += bufLen