如何从 Telegram 群组下载所有共享的媒体文件?

11

有一个Telegram群组,里面有超过40,000个共享文件。
是否有任何机器人可以一次性下载所有文件?如果没有,是否有使用Python的Telegram API脚本方法来下载共享媒体文件?

3个回答

9
你可以使用Telegram客户端Telethon来下载公共群组中的所有文件:

你可以使用Telethon,一个Telegram客户端,来下载公共群组中的所有文件:

from telethon import TelegramClient
from tqdm import tqdm
# These example values won't work. You must get your own api_id and
# api_hash from `my.telegram.org` , under API Development.
api_id = APIID
api_hash = 'APIHASH'
client = TelegramClient('session_name', api_id, api_hash)
client.start()
print(client.get_me().stringify())
# client.send_message('username', 'Hello! Talking to you from Telethon')
# client.send_file('username', '/home/myself/Pictures/holidays.jpg')
# client.download_profile_photo('vic')
messages = client.get_messages('intothestates', limit=2000)
print(len(messages))
for msg in tqdm(messages):
    client.download_media(msg)

1
这是使用Telethon完成此操作的视频教程 - https://youtu.be/J5H_gRdPJSU - Dima Portenko

6

看起来Telethon自victorvs的回答以来已经发生了变化。(Docs

这应该可以工作:

from telethon.sync import TelegramClient, events
from tqdm import  tqdm
import os

# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = <api_id>
api_hash = '<api_hash>'


with TelegramClient('name', api_id, api_hash) as client:
    messages = client.get_messages('<channel/chat>', limit=50) # limit defaults to 1
    for msg in tqdm(messages):
        msg.download_media(file=os.path.join('media', '<file_name>'))

3
电报机器人 API 不幸地不允许查看旧消息(或文件)。
唯一的方法是使用像 Telethon 这样的 API,它在电报方面被视为用户。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接