LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!

Home Page:https://docs.telethon.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can not download media

binoTiger opened this issue · comments

Code that causes the issue

async with TelegramClient(my telegram data of Russian account with proxy) as client:
    channel = await client.get_entity(PeerChannel(1103688715))

    messages = await client.get_messages(
        entity=channel,
        offset_id=0,
        offset_date=None,
        add_offset=0,
        limit=100,
    )

    for message in messages:
        if getattr(message, 'video'):
            if message.file.name:
                filename = message.file.name
            else:
            filename = message.id
        
            await message.download_media(file=f'./media/{filename}')

Expected behavior

client gets a bunch of messages, and downloads the videos, if there are any.

Actual behavior

client receives a stack of messages, but when I try to download them, I get an error

Traceback

File [~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:66](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/programming/wspace/~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:66), in _DirectDownloadIter._load_next_chunk(self)
     [65](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/programming/wspace/~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:65) async def _load_next_chunk(self):
---> [66](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/programming/wspace/~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:66)     cur = await self._request()
     [67](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/programming/wspace/~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:67)     self.buffer.append(cur)
     [68](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/programming/wspace/~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:68)     if len(cur) < self.request.limit:

File [~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:79](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/programming/wspace/~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:79), in _DirectDownloadIter._request(self)
     [77](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/programming/wspace/~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:77) self._timed_out = False
     [78](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/programming/wspace/~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:78) if isinstance(result, types.upload.FileCdnRedirect):
---> [79](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/programming/wspace/~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:79)     raise NotImplementedError  # TODO Implement
     [80](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/programming/wspace/~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:80) else:
     [81](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/programming/wspace/~/Documents/programming/.venv/lib/python3.11/site-packages/telethon/client/downloads.py:81)     return result.bytes

NotImplementedError:

Telethon version

1.34.0

Python version

3.11.6

Operating system (including distribution name and version)

macOS 14.1

Other details

No response

Checklist

  • The error is in the library's code, and not in my own.
  • I have searched for this issue before posting it and there isn't an open duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip and triggered the bug in the latest version.

I don't have plan to implement support for downloads CDNs, however, that shouldn't happen because the default is to not allow CDN redirects:

self.request = functions.upload.GetFileRequest(
file, offset=offset, limit=request_size)

https://tl.telethon.dev/methods/upload/get_file.html (where cdn_supported is a flag and the default is False).

okay, well, then what should I do in this situation?

Someone needs to implement the feature. Whether that's you or someone else is up to you. As I said I'm not interested in doing so and don't really have the time to dig into it. However, there may be some old code in the Git commit history for this repository containing some clues on how CDN downloads work.

@binoTiger that may be because of Your proxy .
because for me CDN don worked well