hi, I have used telethon to download telegram posts, but I can not downlaod media from the post. I have tried using download_msg_media but no result. how can I download media using telethon??
this is the code i have used for downloading function:
def download_media(self, media_id):
try:
msg_media_id = int(media_id)
output = str('usermedia/{}'.format(msg_media_id))
print('Downloading media wit h name {}...'.format(output))
output = self.client.download_msg_media(
msg_media_id,
file_path=output,
progress_callback=self.client2.download_progress_callback)
print('Media downloaded to {}!'.format(output))
except ValueError:
print('Invalid media ID given!')
the document says that the input of this function should be a MessageMedia, which has 10 constructors:
https://lonamiwebs.github.io/Telethon/types/message_media.html
how can I pass a telegram post to this function and get a MessageMedia and then pass that MessageMedia to Download function?
As I always suggest, it's nice looking at the InteractiveTelegramClient example. More precisely, these lines. What the example does is "store all the messages which have any sort of media". Then the user enters the message ID, fetches that message, and accesses its msg.media.
Notice that you can retrieve the media from a message, as noted by the media:flags.9?MessageMedia argument. That media is one of the 10 constructors available, which you can just pass to the client.
I have checked InteractiveTelegramClient, and I have used that code in my program. when I give the function a specific telegram post id, it just says:
Downloading media with name usermedia/1426...
Media downloaded to None!
"store all the messages which have any sort of media": it does this for a specific channel (with a given id) or? could you explain this more? what is media_id? how does the user have access to the message id? @Lonami
I could retrieve the post photo from media attribute. but I can not save the media locally. this is what I am getting for photo attribute:
(photo (ID: 0x9288dd29) = (has_stickers=None, id=5845865598901398016, access_hash=-297749042833952597, date=2017-05-23 09:01:28, sizes=['(photoCachedSize (ID: 0xe9a734fa) = (type=s, location=(fileLocation (ID: 0x53d69076) = (dc_id=4, volume_id=430531531, local_id=111833, secret=1132264834260955610)), w=90, h=68, bytes=b\'\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x01\\x01\\x01\\x00H\\x00H\\x00\\x00\\xff\\xdb\\x00C\\x00\\x0e\\n\\x0b\\r\\x0b\\t\\x0e\\r\\x0c\\r\\x10\\x0f\\x0e\\x11\\x16$\\x17\\x16\\x14\\x14\\x16, !\\x1a$4.763.22:ASF:=N>22HbINVX]^]8EfmeZlS[]Y\\xff\\xdb\\x00C\\x01\\x0f\\x10\\x10\\x16\\x13\\x16*\\x17\\x17*Y;2;YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY\\xff\\xc0\\x00\\x11\\x08\\x00D\\x00Z\\x03\\x01"\\x00\\x02\\x11\\x01\\x03\\x11\\x01\\xff\\xc4\\x00\\x1b\\x00\\x00\\x01\\x05\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x06\\x00\\x02\\x03\\x04\\x05\\x01\\x07\\xff\\xc4\\x00;\\x10\\x00\\x02\\x01\\x03\\x01\\x04\\x05\\t\\x06\\x06\\x03\\x00\\x00\\x00\\x00\\x00\\x01\\x02\\x03\\x00\\x04\\x11\\x12\\x05\\x13!1\\x06"Aa\\x81\\x14#2Qq\\x91\\x92\\x93\\xd13CRS\\x94\\xa1\\x15Tr\\x83\\xb1\\xc1b\\x84\\xe1\\xff\\xc4\\x00\\x19\\x01\\x00\\x03\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x03\\x01\\x04\\x05\\xff\\xc4\\x00\\x1d\\x11\\x00\\x02\\x02\\x03\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x02\\x11\\x12\\x13!\\x031A\\xff\\xda\\x00\\x0c\\x03\\x01\\x00\\x02\\x11\\x03\\x11\\x00?\\x00\\xd9\\xb7T\\xd1\\xc7\\xc9\\xf3\\xff\\x00(\\x1c\\xff\\x00\\x8a\\x92=!\\xcf\\x18>S\\x8a\\x8e\\xdaV\\t\\xc1\\x8f\\xea\\xb1\\xfe\\xaaX\\xe5m\\xe1\\xeb?\\x85\\xc8\\xaf9\\x96\\x14\\xef\\x12)v1\\x05Q\\x92@q\\xc2\\x86d\\xdbWwSn\\xac\\xe3US\\x9d=\\xc3\\xbf4A\\xb6\\x19\\x8e\\xcb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xd1\\x14\\xa9V!\\\'\\xf0\\xb5\\xbc\\xe1\\xe8\\\'\\xba\\xb8g\\xe3\\xf6Q\\xfb\\xa9R\\xa6"\\x7f\\xff\\xd9\'))', '(photoSize (ID: 0x77bfb61b) = (type=m, location=(fileLocation (ID: 0x53d69076) = (dc_id=4, volume_id=430531531, local_id=111834, secret=-840546536578932866)), w=320, h=242, size=21695))', '(photoSize (ID: 0x77bfb61b) = (type=x, location=(fileLocation (ID: 0x53d69076) = (dc_id=4, volume_id=430531531, local_id=111832, secret=-2943408949814512901)), w=429, h=325, size=28828))']))
how can I download the media? what is the url?
There is no url. If you have the photo you should be able to use download_photo on the TelegramClient.
thanks @Lonami
I have another question, is this part of code true?
if not msg.media == None:
self.client.download_msg_media(msg.media, "home/negin/media/")
if the message media attribute is not null, it should download that media. but the folder is still empty after running this
but the folder is still empty after running this
Do you get InvalidDCError's? That's being worked on another branch, but currently I don't have the time for it so please be patient. Also thanks for spotting that comparison to None, it should be if msg.media is not None.
no there is no Error. ok thanks @Lonami
Also thanks for spotting that comparison to
None, it should be ifmsg.media is not None.
I thought that was part from Telethon but it's not. You should replace:
if not msg.media == None:
with:
if msg.media is not None:
Not only because it reads better, but because it's the right way to do it.
it works now! thanks
for me webpage preview photo could be download in following way:
messages = client.get_message_history(channel)
location = messages[1][0].media.webpage.photo.sizes[0].location
inputFileLocation = InputFileLocation(volume_id=location.volume_id, local_id=location.local_id, secret=location.secret)
file = telegram.download_file(inputFileLocation, 'aaa.jpg')
@Lonami what might be wrong in this snippet?
from telethon import TelegramClient
import getpass
from telethon.errors import SessionPasswordNeededError
from telethon.tl.types import UpdateNewChannelMessage, Message, InputPeerChat, InputPeerChannel
from telethon.tl.functions.messages import ForwardMessagesRequest
def replier(update):
media = update.message.media
if media is not None:
print("Will download image")
print(media) # The last output I get
download_res = client.download_msg_media(
media, '/Users/<user>/Development/TelegramFun')
print("Download done: {}".format(download_res))
# create client and add updater here
No sign of download...
@mar02500 were you able to download media?
Nevermind. Should be:
client.download_media(media, path)
how did you download messages telegram messages. can you send the code to download txt messages?
hi,i dont know anything about coding . but i want to download media from a particular channel .
my status is , i have installed telethon and created a client succesfully with help of tutorials
i request some one to kindly give me a ready to deploy command so that i only need to change the n=channel name.
@idontknowanything1 I am sure you could pay someone to write code for you. This is not an issue with telethon so please don't hijack this issue
Most helpful comment
@idontknowanything1 I am sure you could pay someone to write code for you. This is not an issue with telethon so please don't hijack this issue