From a685dd427af7c56bb54d2f9a80e8a8ad6d8baecb Mon Sep 17 00:00:00 2001 From: Egor3f Date: Fri, 3 Jul 2020 17:08:00 +0300 Subject: [PATCH] Fix --- telecup_cli.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/telecup_cli.py b/telecup_cli.py index 7546a6f..b5d93f3 100644 --- a/telecup_cli.py +++ b/telecup_cli.py @@ -216,17 +216,14 @@ async def upload_file(client: TelegramClient, source: typing.BinaryIO, expectedS async def download_part(client: TelegramClient, dest: typing.BinaryIO, dInfo: DownloadInfo): dcId, inputFileLocation = get_input_location(dInfo.message) - chunkSize = pow(2, 20) + chunkSize = pow(2, 19) realSize = int(dInfo.part_info['real_size']) totalBytesDownloaded = 0 lastRealTimeMeasurement = time.time() while totalBytesDownloaded < realSize: offset = totalBytesDownloaded - limit = chunkSize if offset + chunkSize < realSize else realSize - offset - if limit % pow(2, 12) != 0: - extraBytes = pow(2, 12) - limit % pow(2, 12) - limit += extraBytes + limit = chunkSize try: downloadResult: File = await client(GetFileRequest( @@ -242,6 +239,8 @@ async def download_part(client: TelegramClient, dest: typing.BinaryIO, dInfo: Do newMessage = await client.get_messages(dInfo.dialog, ids=dInfo.message.id) dcId, inputFileLocation = get_input_location(newMessage) continue + else: + raise e buffer = downloadResult.bytes bufLen = len(buffer) totalBytesDownloaded += bufLen @@ -377,16 +376,23 @@ async def download(config: Config, filename): destination = sys.stdout.buffer await check_logged_in(config) - filename = filename.strip() - nameHash = hashlib.sha256(filename.encode('utf-8')).hexdigest() - fileMessages = await config.client.get_messages(config.dialog, search=f'#telecup_file_{nameHash}') - if len(fileMessages) == 0: - click.echo(f'`{filename}` not found', err=True) - msg = fileMessages[0] - fileInfo = parse_message(msg.message) - if fileInfo and fileInfo['name'] == filename: - await download_file(config.client, destination, fileInfo, config.dialog) - click.echo('OK', err=True) - else: - click.echo('Message FileInfo corrupt', err=True) - await config.client.disconnect() + try: + filename = filename.strip() + nameHash = hashlib.sha256(filename.encode('utf-8')).hexdigest() + fileMessages = await config.client.get_messages(config.dialog, search=f'#telecup_file_{nameHash}') + if len(fileMessages) == 0: + click.echo(f'`{filename}` not found', err=True) + return + msg = fileMessages[0] + fileInfo = parse_message(msg.message) + if fileInfo and fileInfo['name'] == filename: + await download_file(config.client, destination, fileInfo, config.dialog) + click.echo('OK', err=True) + else: + click.echo('Message FileInfo corrupt', err=True) + finally: + await config.client.disconnect() + + +if __name__ == '__main__': + cli()