Skip to content

Instantly share code, notes, and snippets.

@Umbrien
Last active June 12, 2023 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Umbrien/5c0921867cdbef78655f122a0d058118 to your computer and use it in GitHub Desktop.
Save Umbrien/5c0921867cdbef78655f122a0d058118 to your computer and use it in GitHub Desktop.
Download Telegram channel media using Telethon
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.functions.channels import GetFullChannelRequest
from telethon.tl.types import InputPeerEmpty
from telethon.errors.rpcerrorlist import SessionPasswordNeededError
from tqdm import tqdm
api_id = '12345678'
api_hash = '123456781234567812345678'
phone_number = '+380123456789'
PASS = 'your-password-if-enabled'
# Channel ID or name
channel_id = -100
def download_media(group, cl, name):
messages = cl.get_messages(group, limit=2000)
for message in tqdm(messages):
message.download_media('./' + name + '/')
client = TelegramClient('downloading_session', api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone_number)
try:
client.sign_in(phone_number, input(
'Enter the code sent to your phone: '))
except SessionPasswordNeededError as err:
client.sign_in(password=PASS)
print('getting dialogs')
client.get_dialogs()
print('getting dialogs done')
print('starting client')
client.start()
channel = client(GetFullChannelRequest(channel_id))
print(channel.full_chat)
print('downloading media')
download_media(channel.full_chat, client, title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment