如何将媒体下载到 Telethon 上的特定路径
How Can I download media to specific path on Telethon
我正在研究电视节目 download_media 和 _download_document 从电报下载媒体的方法。我的代码是这样的:
from telethon import TelegramClient
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)
async def main():
async for message in client.iter_messages('me'):
print(message.id, message.text)
# You can download media from messages, too!
# The method will return the path where the file was saved.
if message.photo:
path = await message.download_media()
print('File saved to', path) # printed after download is done
with client:
client.loop.run_until_complete(main())
但此代码无法将媒体下载到特定路径,
以及如何获取保存的文件的名称
Docs of telethon 表明 download_media
方法接受名为 file
的参数,即
The output file path, directory, or stream-like object. If the path
exists and is a file, it will be overwritten. If file is the type
bytes, it will be downloaded in-memory as a bytestring (e.g.
file=bytes).
我没有能力测试它,但是可以替换
message.download_media()
和
message.download_media(file="path/to/downloads_dir")
应该可以。
您可以使用message.file.name
获取文件名,这是代码
from telethon import TelegramClient
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)
async def main():
async for message in client.iter_messages('me'):
print(message.id, message.text)
if message.photo:
print('File Name :' + str(message.file.name))
path = await client.download_media(message.media, "youranypathhere")
print('File saved to', path) # printed after download is done
with client:
client.loop.run_until_complete(main())
我正在研究电视节目 download_media 和 _download_document 从电报下载媒体的方法。我的代码是这样的:
from telethon import TelegramClient
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)
async def main():
async for message in client.iter_messages('me'):
print(message.id, message.text)
# You can download media from messages, too!
# The method will return the path where the file was saved.
if message.photo:
path = await message.download_media()
print('File saved to', path) # printed after download is done
with client:
client.loop.run_until_complete(main())
但此代码无法将媒体下载到特定路径, 以及如何获取保存的文件的名称
Docs of telethon 表明 download_media
方法接受名为 file
的参数,即
The output file path, directory, or stream-like object. If the path exists and is a file, it will be overwritten. If file is the type bytes, it will be downloaded in-memory as a bytestring (e.g. file=bytes).
我没有能力测试它,但是可以替换
message.download_media()
和
message.download_media(file="path/to/downloads_dir")
应该可以。
您可以使用message.file.name
获取文件名,这是代码
from telethon import TelegramClient
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)
async def main():
async for message in client.iter_messages('me'):
print(message.id, message.text)
if message.photo:
print('File Name :' + str(message.file.name))
path = await client.download_media(message.media, "youranypathhere")
print('File saved to', path) # printed after download is done
with client:
client.loop.run_until_complete(main())