简体   繁体   中英

Python script to download all the media from a Telegram Channel using Telethon

I was trying to use Telethon but turns out is really slow

So I tried usingthis gist as suggested in this post

I have the following errors. Can anyone please help me?

Here is my code:

from telethon.sync import TelegramClient
from FastTelethon import download_file
import os
import asyncio


async def getAllMediaFromchannel():
    os.chdir("/home/gtxtreme/Documents/McHumour")
    api_hash = "<hidden>"
    api_id = <hidden>

    client = TelegramClient('MCHumour', api_id, api_hash)
    client.start()
    ch_entity = await client.get_entity("telegram.me/joinchat/AAAAAEXnb4jK7xyU1SfAsw")

    messages = client.iter_messages(ch_entity, limit=50)

    def progress_cb(current, total):
        print('Uploaded', current, 'out of', total,
              'bytes: {:.5%}'.format(current / total))

    async for msg in messages:
        result = await download_file(client, msg.document, "/home/gtxtreme/Documents/McHumour",
                                     progress_callback=progress_cb)
        print("*************************\nFile named {0} saved to {1} successfully\n********************".format(
            msg.message, result))


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(getAllMediaFromchannel())

Here is my error

[gtxtreme@archlinux ~]$ python PycharmProjects/python_gtxtreme/tgBotrev1.py 

PycharmProjects/python_gtxtreme/tgBotrev1.py:13: RuntimeWarning: coroutine 'AuthMethods._start' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "PycharmProjects/python_gtxtreme/tgBotrev1.py", line 31, in <module>
    loop.run_until_complete(getAllMediaFromchannel())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 612, in run_until_complete
    return future.result()
  File "PycharmProjects/python_gtxtreme/tgBotrev1.py", line 14, in getAllMediaFromchannel
    ch_entity = await client.get_entity("telegram.me/joinchat/AAAAAEXnb4jK7xyU1SfAsw")
  File "/usr/lib/python3.8/site-packages/telethon/client/users.py", line 310, in get_entity
    result.append(await self._get_entity_from_string(x))
  File "/usr/lib/python3.8/site-packages/telethon/client/users.py", line 512, in _get_entity_from_string
    invite = await self(
  File "/usr/lib/python3.8/site-packages/telethon/client/users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
  File "/usr/lib/python3.8/site-packages/telethon/client/users.py", line 56, in _call
    future = sender.send(request, ordered=ordered)
  File "/usr/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 170, in send
    raise ConnectionError('Cannot send requests while disconnected')
ConnectionError: Cannot send requests while disconnected

[gtxtreme@archlinux ~]$

Also any other suitable way of doing it would be preferred

client.start is an async method so you should await it.

it only needs the await if it is inside a function. if you call it outside of a function telethon adds the await implicitly for convenience

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM