Search code examples
pythontelegramtelethontqdm

Telegram Telethon Media Download Progress with TQDM


i just want a working Progressbar (tqdm) when downloading with Telethon.

def prog(current, total):
    global pbar
    pbar.update(current)

pbar = tqdm()

async def DoIt():
    global pbar
    async for message in client.iter_messages(entity=entity, limit=60,reverse=False):
        pbar = tqdm(unit='B',unit_scale=True,total=message.media.document.size)
        await client.download_media(message.media.document, "Q:\\dl", progress_callback=prog)
        pbar.close()

It does not really work. The Progressbar goes from "0" to "100": pbar

then this appears:

pbar


Solution

  • The way tqdm work in my opinion, is that it update the chunk that was completed. For this example to work, I had to workout the change from previous update and feed that to update. Work well.

    # Printing download progress
    def callback(current, total):
        global pbar
        global prev_curr
        pbar.update(current-prev_curr)
        prev_curr = current
    
    async def main():
        global pbar
        global prev_curr
        async for message in client.iter_messages(chat, reverse=True):
        if message.media:  
            prev_curr = 0
            pbar = tqdm(total=message.document.size, unit='B', unit_scale=True)
            path = await message.download_media('{}/{}'.format(dlw_path,file_name), progress_callback=callback)
            pbar.close()