Skip to main content

Download videos from Telegram channels

Project description

telegram-dl

PyPI Version Python Versions License GitHub Release

A Python CLI tool to download videos from Telegram channels.

Installation

pip install telegram-dl

Quick Start

1. Get Telegram API Credentials

  1. Go to my.telegram.org
  2. Log in with your phone number
  3. Click "API development tools"
  4. Create a new app
  5. Copy your API ID and API Hash

2. Download Videos

# List your channels first
telegram-dl --api-id 12345 --api-hash abc123def456 --phone +1234567890 --list-channels

# Download all videos from a channel
telegram-dl --api-id 12345 --api-hash abc123def456 --phone +1234567890 --channel -1001234567890

3. Use Custom Output Directory

telegram-dl --api-id 12345 --api-hash abc123def456 --phone +1234567890 --channel -1001234567890 --output ./my_videos

Programmatic Usage

from telegram_dl import TelegramDownloader

async def main():
    async with TelegramDownloader(
        api_id=12345,
        api_hash="abc123def456",
        phone="+1234567890"
    ) as dl:
        # List channels
        async for dialog in dl.client.iter_dialogs():
            if dialog.is_channel:
                print(f"{dialog.id} | {dialog.title}")
        
        # Download all videos
        await dl.download_all_videos(channel_id, "./videos")

asyncio.run(main())

Publishing Guide

Manual Publishing to PyPI

# 1. Install build tools
pip install build twine

# 2. Update version in pyproject.toml
# version = "1.2.0"

# 3. Build the package
rm -rf dist/
python -m build

# 4. Upload to PyPI
twine upload dist/*

GitHub Actions Auto-Publish

  1. Go to PyPI Trusted Publishing
  2. Add a new publisher:
    • Owner: kuldeep27396
    • Repository name: telegram-dl
    • Workflow filename: publish.yml
    • Environment name: pypi
  3. Push a new tag:
git tag v1.2.0
git push && git push --tags
# Or create release on GitHub

High Level Design (HLD)

Note: For colorful Mermaid diagrams, see the GitHub README

Architecture

┌─────────────────────────────────────────────────────────────┐
│                      User Layer (👤)                         │
│  ┌─────────────────┐         ┌─────────────────────┐       │
│  │  CLI Interface  │         │    Python API        │       │
│  │   (telegram-dl) │         │ TelegramDownloader   │       │
│  └─────────────────┘         └─────────────────────┘       │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                    Core Layer (⚙️)                           │
│  TelegramDownloader                                          │
│  ├─ connect() / disconnect()                               │
│  ├─ get_channel_videos()                                    │
│  ├─ download_video()                                        │
│  └─ download_all_videos()                                   │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                  Protocol Layer (🔌)                         │
│  Telethon Library                                            │
│  ├─ MTProto Protocol                                        │
│  ├─ Session Management                                      │
│  └─ OTP/2FA Authentication                                 │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│              External Services (🌐)                           │
│            Telegram Servers (api.telegram.org)                 │
└─────────────────────────────────────────────────────────────┘

Data Flow

User ──► CLI/API ──► TelegramDownloader ──► Telethon ──► Telegram
           │               │                    │           │
        Credentials    Connect & Auth       MTProto    Server
           │               │                    │           │
           ▼               ▼                    ▼           ▼
         Input      Session Created       Protocol    Video Stream

Class Structure

TelegramDownloader
├── api_id: int
├── api_hash: str  
├── phone: str
├── client: TelegramClient
│
├── connect() → TelegramClient
├── disconnect()
├── get_dialogs() → List[Dict]
├── get_channel_videos(channel_id) → List[Dict]
├── download_video(channel_id, video_id, output_dir, filename) → str
└── download_all_videos(channel_id, output_dir, progress_callback) → List[str]

Exceptions
├── TelegramDLError (base)
├── AuthenticationError
├── ChannelNotFoundError
└── VideoNotFoundError

Session Management Flow

First Run ──► OTP Verification ──► Save Session ──► Ready
    │              │                     │
    │              │                     ▼
    │              └───────────────────► Done!
    │
    ▼
Next Run ──► Session Valid? ──► Yes ──► Ready
                │
                └── No ──► OTP Verification

Error Handling

┌─────────────────────────────────────────┐
│           Telethon Errors               │
│  ConnectionError │ SessionPasswordError  │
└─────────────────┴───────────────────────┘
                     │
                     ▼
┌─────────────────────────────────────────┐
│          Custom Exceptions               │
│  TelegramDLError (base)                 │
│  ├─ AuthenticationError                │
│  ├─ ChannelNotFoundError               │
│  └─ VideoNotFoundError                  │
└─────────────────────────────────────────┘
                     │
                     ▼
          User fixes & retries

Components

Component File Responsibility
CLI cli.py Command-line interface
Client client.py Core download logic
Exceptions exceptions.py Error classes

Key Features

  • CLI Interface - Simple commands, progress tracking, channel listing
  • Core Logic - Async download, auto retry, skip existing files
  • Session Management - Persistent login, OTP/2FA support
  • File Handling - Named preservation, custom output, progress callbacks

Features

  • Download all videos from Telegram channels
  • Automatic session management (login once, use many times)
  • Progress tracking
  • Named file preservation from Telegram
  • Skip already downloaded files
  • CLI and programmatic API

Links

Requirements

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegram_dl-1.1.1.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telegram_dl-1.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file telegram_dl-1.1.1.tar.gz.

File metadata

  • Download URL: telegram_dl-1.1.1.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for telegram_dl-1.1.1.tar.gz
Algorithm Hash digest
SHA256 0959d26befb641f9a2d54fde2b87852c74ffa95612d3930bcb65f96e60913cf8
MD5 45f207202673bc92c9860d385c85732f
BLAKE2b-256 1796ffa6e5f6dcbc53dc51ee97a881fc30837a2a59e1ac33118ebc56ee28049e

See more details on using hashes here.

File details

Details for the file telegram_dl-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: telegram_dl-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for telegram_dl-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b6d1f4cfd0f6bbfa3ff4e0a2b88b2c15b1d340f6dbfd8cbeb9eaeea844e386f9
MD5 f0d4de3fe900364ca7fed5bca92bdbf5
BLAKE2b-256 794c9c178b202b01ad357e3ffd910f4340157ca8456909fc0b09dae6637da95d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page