Refetch the message in case of reference expiration

This commit is contained in:
Egor Aristov 2020-06-15 17:38:32 +03:00
parent 962cfdbef7
commit 0d299fe84f

View File

@ -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