Cdn File (Media) Download Error
Code that causes the issue
await client.download_media(any_media_from_cdn)
Expected behavior
Successful file download
Actual behavior
The method you tried to call cannot be called on non-CDN DCs (caused by GetCdnFileRequest)
Traceback
INFO:telethon.client.downloads:Starting direct file download in chunks of 131072 at 0, stride 131072
INFO:telethon.client.downloads:File lives in another DC
INFO:telethon.client.downloads:FileCdnRedirect to CDN data center 203
INFO:telethon.client.downloads:Starting direct file download in chunks of 131072 at 0, stride 131072
INFO:telethon.client.telegrambaseclient:Creating new CDN client
INFO:telethon.network.mtprotosender:Connecting to 91.105.192.100:443/TcpFull...
INFO:telethon.network.mtprotosender:Connection to 91.105.192.100:443/TcpFull complete!
WARNING:telethon.client.users:Telegram is having internal issues UnknownMethodError: The method you tried to call cannot be called on non-CDN DCs (caused by GetCdnFileRequest)
WARNING:telethon.client.users:Telegram is having internal issues UnknownMethodError: The method you tried to call cannot be called on non-CDN DCs (caused by GetCdnFileRequest)
WARNING:telethon.client.users:Telegram is having internal issues UnknownMethodError: The method you tried to call cannot be called on non-CDN DCs (caused by GetCdnFileRequest)
WARNING:telethon.client.users:Telegram is having internal issues UnknownMethodError: The method you tried to call cannot be called on non-CDN DCs (caused by GetCdnFileRequest)
WARNING:telethon.client.users:Telegram is having internal issues UnknownMethodError: The method you tried to call cannot be called on non-CDN DCs (caused by GetCdnFileRequest)
WARNING:telethon.client.users:Telegram is having internal issues UnknownMethodError: The method you tried to call cannot be called on non-CDN DCs (caused by GetCdnFileRequest)
---------------------------------------------------------------------------
FileMigrateError Traceback (most recent call last)
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:86, in _DirectDownloadIter._request(self)
85 try:
---> 86 result = await self._client._call(self._sender, self.request)
87 self._timed_out = False
File /opt/conda/lib/python3.10/site-packages/telethon/client/users.py:87, in UserMethods._call(self, sender, request, ordered, flood_sleep_threshold)
86 else:
---> 87 result = await future
88 self.session.process_entities(result)
FileMigrateError: The file to be accessed is currently stored in DC 4 (caused by GetFileRequest)
During handling of the above exception, another exception occurred:
_CdnRedirect Traceback (most recent call last)
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:563, in DownloadMethods._download_file(self, input_location, file, part_size_kb, file_size, progress_callback, dc_id, key, iv, msg_data, cdn_redirect)
562 try:
--> 563 async for chunk in self._iter_download(
564 input_location, request_size=part_size, dc_id=dc_id, msg_data=msg_data, cdn_redirect=cdn_redirect):
565 if iv and key:
File /opt/conda/lib/python3.10/site-packages/telethon/requestiter.py:74, in RequestIter.__anext__(self)
73 self.buffer = []
---> 74 if await self._load_next_chunk():
75 self.left = len(self.buffer)
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:76, in _DirectDownloadIter._load_next_chunk(self)
75 async def _load_next_chunk(self):
---> 76 cur = await self._request()
77 self.buffer.append(cur)
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:113, in _DirectDownloadIter._request(self)
112 self._exported = True
--> 113 return await self._request()
115 except (errors.FilerefUpgradeNeededError, errors.FileReferenceExpiredError) as e:
116 # Only implemented for documents which are the ones that may take that long to download
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:91, in _DirectDownloadIter._request(self)
90 raise ValueError('FileCdnRedirect but the GetCdnFileRequest API access for bot users is restricted. Try to change api_id to avoid FileCdnRedirect')
---> 91 raise _CdnRedirect(result)
92 if isinstance(result, types.upload.CdnFileReuploadNeeded):
_CdnRedirect: FileCdnRedirect(dc_id=203, file_token=b"M)\x9f\x11\x9b\xb99\xb5?\tq\xf1\xda\x90\xe8\xe6\xee&\xdd\xb5\x86\x92\xf7\x94Jh\x19\x90\x036:\xde\xfbwpz\xe5\x95\xa6W\t\xba>\xbd\xde[\x9e`a\xe2A o\x9c\x7fwkP\xf6\x19s\xddz\xb3\x04\x84,+\xb9>\x075(\x93\xf5\xefP\t\x8e\x95\xa0\xba1\x9e\xf3\xd4M:\n\x905\xbcW\x1f\xf5\xd8r.\xebY'W\x95\xc9\xac\xa0#~\xb8\x1b\xbd6\x92\x1b\x06\xb9L\xc9\x1an\xec\xdc\xe3\x0bYl\x96\x8d\xb4O-_2\t\xd0&\xd7\x95\\\xbce\x0ch\xcax\xac\xf9\xc9`L\xd2\xa9\xb9\xca\xe3\x0ch\x83\xbdNK_[\x82G\x9b\xbd\x02&\x90\xb2\x14\xc8\xf1\xe5q\x84\xdc\x03\xc3\xa0\xd0\xea\x86~\x80\xf8\xbdS\x15J\x82\xf4U\xaaG\x85P\x84\x1d\xa5\xe56,\xa0\xaf\x1b\xfa\xc9\x01\x07?\x84}|\x95\xfc\xb6\x94/|\x15>\xe2Bk^ST\x06\x88Q\xb6\xa5\x15\xd3Bx\xcf\x87\x92\xf1\xe4\x16\xf8\xc2\x8bq&\xaf^\xbf\xd3tdo", encryption_key=b'\xbc[\xf1\x02\xb0\xc5S\xb8\xa0\xc1\x11\xe0\x8a\x90g/\x9d\x84S;|\x05\xbb\x98\x0fZ\x98\x8f\xf1pBF', encryption_iv=b'\xdf)\xd9\xc5j\xe3\x9f\xa2\x7fC\xc6Z\xdb\xd6k\xc5', file_hashes=[FileHash(offset=0, limit=131072, hash=b"=\xf2\xeb\xdd\xae\xfb\xde\xe0/\x06_\xa23\xc1\xbe\x9c\xb5\xb0O\r\xd9\x04@'\xf0\xf6\xe8_\x9c\x80\x83\xdb"), FileHash(offset=131072, limit=131072, hash=b'a2\xa0\x13\xd3a\xb5\xfd6\x1b\xb4\x14\tg\xd0\x11WE\xe32\xa66\xb6\xec\x95\x81\x8a\xf0dh<\t')])
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
Cell In [54], line 1
----> 1 await client.download_media(s.messages[0].photo)
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:434, in DownloadMethods.download_media(self, message, file, thumb, progress_callback)
431 media = media.webpage.document or media.webpage.photo
433 if isinstance(media, (types.MessageMediaPhoto, types.Photo)):
--> 434 return await self._download_photo(
435 media, file, date, thumb, progress_callback
436 )
437 elif isinstance(media, (types.MessageMediaDocument, types.Document)):
438 return await self._download_document(
439 media, file, date, thumb, progress_callback, msg_data
440 )
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:868, in DownloadMethods._download_photo(self, photo, file, date, thumb, progress_callback)
865 else:
866 file_size = size.size
--> 868 result = await self.download_file(
869 types.InputPhotoFileLocation(
870 id=photo.id,
871 access_hash=photo.access_hash,
872 file_reference=photo.file_reference,
873 thumb_size=size.type
874 ),
875 file,
876 file_size=file_size,
877 progress_callback=progress_callback
878 )
879 return result if file is bytes else file
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:513, in DownloadMethods.download_file(self, input_location, file, part_size_kb, file_size, progress_callback, dc_id, key, iv)
450 async def download_file(
451 self: 'TelegramClient',
452 input_location: 'hints.FileLike',
(...)
459 key: bytes = None,
460 iv: bytes = None) -> typing.Optional[bytes]:
461 """
462 Low-level method to download files from their input location.
463
(...)
511 print(data[:16])
512 """
--> 513 return await self._download_file(
514 input_location,
515 file,
516 part_size_kb=part_size_kb,
517 file_size=file_size,
518 progress_callback=progress_callback,
519 dc_id=dc_id,
520 key=key,
521 iv=iv,
522 )
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:584, in DownloadMethods._download_file(self, input_location, file, part_size_kb, file_size, progress_callback, dc_id, key, iv, msg_data, cdn_redirect)
582 except _CdnRedirect as e:
583 self._log[__name__].info('FileCdnRedirect to CDN data center %s', e.cdn_redirect.dc_id)
--> 584 return await self._download_file(
585 input_location=input_location,
586 file=file,
587 part_size_kb=part_size_kb,
588 file_size=file_size,
589 progress_callback=progress_callback,
590 dc_id=e.cdn_redirect.dc_id,
591 key=e.cdn_redirect.encryption_key,
592 iv=e.cdn_redirect.encryption_iv,
593 msg_data=msg_data,
594 cdn_redirect=e.cdn_redirect
595 )
596 finally:
597 if isinstance(file, str) or in_memory:
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:563, in DownloadMethods._download_file(self, input_location, file, part_size_kb, file_size, progress_callback, dc_id, key, iv, msg_data, cdn_redirect)
560 f = file
562 try:
--> 563 async for chunk in self._iter_download(
564 input_location, request_size=part_size, dc_id=dc_id, msg_data=msg_data, cdn_redirect=cdn_redirect):
565 if iv and key:
566 chunk = AES.decrypt_ige(chunk, key, iv)
File /opt/conda/lib/python3.10/site-packages/telethon/requestiter.py:74, in RequestIter.__anext__(self)
72 self.index = 0
73 self.buffer = []
---> 74 if await self._load_next_chunk():
75 self.left = len(self.buffer)
77 if not self.buffer:
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:76, in _DirectDownloadIter._load_next_chunk(self)
75 async def _load_next_chunk(self):
---> 76 cur = await self._request()
77 self.buffer.append(cur)
78 if len(cur) < self.request.limit:
File /opt/conda/lib/python3.10/site-packages/telethon/client/downloads.py:86, in _DirectDownloadIter._request(self)
84 async def _request(self):
85 try:
---> 86 result = await self._client._call(self._sender, self.request)
87 self._timed_out = False
88 if isinstance(result, types.upload.FileCdnRedirect):
File /opt/conda/lib/python3.10/site-packages/telethon/client/users.py:134, in UserMethods._call(self, sender, request, ordered, flood_sleep_threshold)
132 if self._raise_last_call_error and last_error is not None:
133 raise last_error
--> 134 raise ValueError('Request was unsuccessful {} time(s)'
135 .format(attempt))
ValueError: Request was unsuccessful 6 time(s)
Telethon version
1.37.0
Python version
3.10
Operating system (including distribution name and version)
Ubuntu 24.04
Other details
The problem seems to be related to this PR https://github.com/LonamiWebs/Telethon/pull/4420
Checklist
- [X] The error is in the library's code, and not in my own.
- [X] I have searched for this issue before posting it and there isn't an open duplicate.
- [X] I ran
pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zipand triggered the bug in the latest version.
I checked the code and it seems that self._sender = self.client._sender should be self._sender = self._client._sender in
https://github.com/LonamiWebs/Telethon/blob/a0e42c1eb7b884c2247cf845bc2acbf545563f50/telethon/client/downloads.py#L57
I ran code with this modification. The above exception resolves but produces another unexpected behavior. Nothing happens after GetCdnFileRequest. After running for several minutes there was only PingRequest and MsgAck which seems to be from "parent" non-CDN DC client.
INFO:telethon.client.telegrambaseclient:Creating new CDN client
INFO:telethon.network.mtprotosender:Connecting to 91.105.192.100:443/TcpFull...
DEBUG:telethon.network.mtprotosender:Connection attempt 1...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:Starting send loop
DEBUG:telethon.network.mtprotosender:Starting receive loop
INFO:telethon.network.mtprotosender:Connection to 91.105.192.100:443/TcpFull complete!
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 7425221684821810592 to GetCdnFileRequest (108ba2930)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 292 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Receiving items from the network...
CDN support was added back by @HBcao233 in #4420. I personally have no interest in maintaining it or trying to fix issues with it.
Maybe they'd be willing to look into the issue, otherwise you're also welcome to send a PR and I will happily review it.
I keep getting similar issue during CDN video url message sending Error: Postponed Timeout error @Lonami