【问题标题】:Python script to download all the media from a Telegram Channel using Telethon使用 Telethon 从 Telegram 频道下载所有媒体的 Python 脚本
【发布时间】:2020-08-03 17:38:17
【问题描述】:

我尝试使用 Telethon,但结果真的很慢

所以我尝试按照建议使用this gist this post

我有以下错误。谁能帮帮我?

这是我的代码:

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())

这是我的错误

[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 ~]$

任何其他合适的方式都是首选

【问题讨论】:

    标签: python asynchronous telegram telethon


    【解决方案1】:

    client.start 是一个异步方法,所以你应该等待它。

    只有在函数内部时才需要等待。如果你在函数telethon之外调用它,为了方便起见隐式添加等待

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-17
      • 1970-01-01
      • 2023-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多