This commit is contained in:
Egor Aristov 2020-07-03 17:08:00 +03:00
parent 9fab87b045
commit a685dd427a

View File

@ -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): async def download_part(client: TelegramClient, dest: typing.BinaryIO, dInfo: DownloadInfo):
dcId, inputFileLocation = get_input_location(dInfo.message) dcId, inputFileLocation = get_input_location(dInfo.message)
chunkSize = pow(2, 20) chunkSize = pow(2, 19)
realSize = int(dInfo.part_info['real_size']) realSize = int(dInfo.part_info['real_size'])
totalBytesDownloaded = 0 totalBytesDownloaded = 0
lastRealTimeMeasurement = time.time() lastRealTimeMeasurement = time.time()
while totalBytesDownloaded < realSize: while totalBytesDownloaded < realSize:
offset = totalBytesDownloaded offset = totalBytesDownloaded
limit = chunkSize if offset + chunkSize < realSize else realSize - offset limit = chunkSize
if limit % pow(2, 12) != 0:
extraBytes = pow(2, 12) - limit % pow(2, 12)
limit += extraBytes
try: try:
downloadResult: File = await client(GetFileRequest( 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) newMessage = await client.get_messages(dInfo.dialog, ids=dInfo.message.id)
dcId, inputFileLocation = get_input_location(newMessage) dcId, inputFileLocation = get_input_location(newMessage)
continue continue
else:
raise e
buffer = downloadResult.bytes buffer = downloadResult.bytes
bufLen = len(buffer) bufLen = len(buffer)
totalBytesDownloaded += bufLen totalBytesDownloaded += bufLen
@ -377,16 +376,23 @@ async def download(config: Config, filename):
destination = sys.stdout.buffer destination = sys.stdout.buffer
await check_logged_in(config) await check_logged_in(config)
filename = filename.strip() try:
nameHash = hashlib.sha256(filename.encode('utf-8')).hexdigest() filename = filename.strip()
fileMessages = await config.client.get_messages(config.dialog, search=f'#telecup_file_{nameHash}') nameHash = hashlib.sha256(filename.encode('utf-8')).hexdigest()
if len(fileMessages) == 0: fileMessages = await config.client.get_messages(config.dialog, search=f'#telecup_file_{nameHash}')
click.echo(f'`{filename}` not found', err=True) if len(fileMessages) == 0:
msg = fileMessages[0] click.echo(f'`{filename}` not found', err=True)
fileInfo = parse_message(msg.message) return
if fileInfo and fileInfo['name'] == filename: msg = fileMessages[0]
await download_file(config.client, destination, fileInfo, config.dialog) fileInfo = parse_message(msg.message)
click.echo('OK', err=True) if fileInfo and fileInfo['name'] == filename:
else: await download_file(config.client, destination, fileInfo, config.dialog)
click.echo('Message FileInfo corrupt', err=True) click.echo('OK', err=True)
await config.client.disconnect() else:
click.echo('Message FileInfo corrupt', err=True)
finally:
await config.client.disconnect()
if __name__ == '__main__':
cli()