Python 如何在电报中迭代文件下载?

Python 如何在电报中迭代文件下载?,python,file,python-asyncio,telethon,Python,File,Python Asyncio,Telethon,我正在弄清楚iter_下载是如何工作的。 我怀疑我计算的偏移量是错误的。“测试”文件大小始终等于零字节 iter_download() knows fileSize , so it should start to iterate from "50% to 100%" import asyncio import telethon from telethon import TelegramClient from telethon import functions, typ

我正在弄清楚iter_下载是如何工作的。

我怀疑我计算的偏移量是错误的。“测试”文件大小始终等于零字节

iter_download() knows fileSize , so it should start to iterate from "50% to 100%"


import asyncio
import telethon
from telethon import TelegramClient 
from telethon import functions, types

api_id =
api_hash =

async def main():
                channel = await client(functions.messages.CheckChatInviteRequest(hash=""))
                async for msg in client.iter_messages(channel.chat, ids=66042):
                      print(f"{msg.file.name} {msg.file.size}")
                      with open('test', 'wb') as fd:
                          async for chunk in client.iter_download(msg.media.document,offset=offset,file_size = msg.file.size):
                              fd.write(chunk)

client = TelegramClient("iter", api_id, api_hash)
loop = asyncio.get_event_loop()
client.start()

try:
      iter_d = loop.create_task(main())    
      loop.run_forever()
except KeyboardInterrupt as keyint:
      print("Keyboard interrupt")
编辑: 我以前误解了iter_的下载功能,把限制和请求大小弄混了。请查看更新的代码

async def test():
channel = await client(functions.messages.CheckChatInviteRequest(hash=""))
async for msg in client.iter_messages(channel.chat, ids=66042):

    with open('test', 'ab') as fd:
        #limit            =  1048576 MAX LIMIT -> see the DOC
        FileBrokenSize    =  210239488  #210,239,488 bytes ( Saved on HDD)
        FileSize          =  416713775  #416,713,775 bytes ( file size) 
        offset            =  FileSize - FileBrokenSize  # 206474287
        request_size      =  1024 # this value must be divisible to 1024
        
        #How many bytes we still need to download the whole file?
        remainder = offset % request_size   #if it's not zero then it's NOT divisible to request_size !!
        #how many part I want to request?
        limit = (offset - remainder ) / request_size  # 201,635,00

        print(f"FileSize: {FileSize} - Offset: {offset} - RequestSize: {request_size}")
        print(f"Remainder: {remainder} - Limit: {limit}")
        input("Press Enter to continue...")

        nPart = 0
        async for chunk in client.iter_download(msg.media.document,offset = offset,limit=limit,request_size =request_size,file_size = FileSize):
            nPart = nPart + 1
            print(f"CHUNK [{nPart}] -> {chunk}")
            fd.write(chunk)
它下载速度很慢,但我不知道如何处理剩余的? 如果您的请求值太高,它将返回

telethon.errors.rpcerrorlist.LimitInvalidError: An invalid limit was provided.
有什么帮助吗?
谢谢

您确定
msg.file.size
足够大吗?也许它甚至还不到500MB。另外,不要使用
chunk\u size
,默认值更好。您应该打印文件大小。您是如何得出偏移量的神奇值的?正如Lonami所指出的,缺少块可能是偏移量超过实际文档大小的结果。您可以添加调试打印,以检查消息报告的文件大小是否实际大于您给它的偏移量。