Modular Telegram bot for automatic organization of your media library
- 🎬 Smart Organization - Automatically creates folders for movies and TV series
- 📺 Series Detection - Recognizes season/episode patterns (S01E01, 1x01, etc.)
- 🎯 TMDB Integration - Metadata, posters, and automatic renaming
- 📁 Clean Structure - Movies in individual folders, series organized by season
- ⏳ Queue Management - Multiple downloads with configurable limits
- 💾 Space Monitoring - Real-time control with automatic queue management
- 👥 Multi-user - Whitelist system for authorized users
- 🔄 Resilience - Resume and automatic retry support
- 🐳 Docker Ready - Easy deployment with Docker Compose
The project uses a modular architecture for maximum maintainability and extensibility:
mediabutler/
├── main.py # Main entry point
├── core/ # Core system modules
│ ├── __init__.py
│ ├── config.py # Centralized configuration
│ ├── auth.py # Authentication management
│ ├── downloader.py # Download and queue management
│ ├── space_manager.py # Disk space monitoring
│ └── tmdb_client.py # TMDB API client
├── handlers/ # Telegram event handlers
│ ├── __init__.py
│ ├── commands.py # Command handlers (/start, /status, etc.)
│ ├── callbacks.py # Inline button handlers
│ └── files.py # Received file handlers
├── models/ # Data models
│ ├── __init__.py
│ └── download.py # Download info dataclass
├── utils/ # Utilities and helpers
│ ├── __init__.py
│ ├── naming.py # File name parser and management
│ ├── formatters.py # Message formatting
│ └── helpers.py # Generic helpers
└── requirements.txt # Python dependencies
config: Centralized configuration management with validationauth: Multi-user authorization system with admindownloader: Download manager with queues, retry, and error handlingspace_manager: Space monitoring and smart cleanuptmdb_client: TMDB integration with rate limiting
commands: All bot commands (/start,/status,/space, etc.)callbacks: Inline button and interaction managementfiles: Processing of received files with content recognition
naming: Smart file name parsing and folder structure creationformatters: Telegram message formatting and progress barhelpers: Retry logic, validation, rate limiting, async helpers
- Python 3.8+ or Docker
- Telegram API credentials (my.telegram.org)
- Bot token from @BotFather
- (Optional) TMDB API key for metadata
- Clone the repository:
git clone https://github.com/yourusername/mediabutler.git
cd mediabutler- Configure environment:
cp .env.example .env
nano .env # Enter your credentials- Start with Docker Compose:
docker compose up -d- Setup Python environment:
python -m venv venv
source venv/bin/activate # Linux/Mac
# or
venv\Scripts\activate # Windows- Install dependencies:
pip install -r requirements.txt- Configure and start:
cp .env.example .env
nano .env # Configure credentials
python main.py# Telegram (Required)
TELEGRAM_API_ID=123456
TELEGRAM_API_HASH=abcdef123456
TELEGRAM_BOT_TOKEN=123456:ABC-DEF
# TMDB (Optional but recommended)
TMDB_API_KEY=your_tmdb_api_key
TMDB_LANGUAGE=en-US
# Paths
MOVIES_PATH=/media/movies
TV_PATH=/media/tv
TEMP_PATH=/media/temp
# Authorizations
AUTHORIZED_USERS=123456789,987654321
# Limits
MAX_CONCURRENT_DOWNLOADS=3
MIN_FREE_SPACE_GB=5
WARNING_THRESHOLD_GB=10See .env.example for all available options.
| Command | Description | Permissions |
|---|---|---|
/start |
Start bot and show info | All |
/status |
Show active downloads and queue | All |
/space |
Disk space details | All |
/waiting |
Files waiting for space | All |
/cancel_all |
Cancel all downloads | All |
/help |
Show command guide | All |
/users |
List authorized users | Admin |
/stop |
Stop the bot | Admin |
- Send/forward a video file to the bot
- Bot analyzes the name and searches TMDB
- Confirm or select Movie/TV Series
- For series, select season if needed
- Automatic download or queued
/media/
├── movies/
│ ├── Avatar (2009)/
│ │ └── Avatar (2009).mp4
│ └── Inception (2010)/
│ └── Inception (2010).mp4
└── tv/
├── Breaking Bad [EN]/
│ ├── Season 01/
│ │ ├── Breaking Bad - S01E01 - Pilot.mp4
│ │ └── Breaking Bad - S01E02 - Cat's in the Bag.mp4
│ └── Season 02/
└── The Office/
└── Season 04/
The modular design makes it easy to add new features:
- Create the method in
handlers/commands.py:
async def mycommand_handler(self, event):
"""Handler for /mycommand"""
if not await self.auth.check_authorized(event):
return
# Command logic
await event.reply("Command response")- Register in
register():
self.client.on(events.NewMessage(pattern='/mycommand'))(self.mycommand_handler)- Create a new module in
core/:
# core/metadata_provider.py
class MetadataProvider:
async def search(self, query: str):
# Implement search
pass- Integrate in
FileHandlersor where needed
# Unit tests
python -m pytest tests/
# Test with coverage
python -m pytest --cov=core --cov=handlers --cov=utils tests/The project follows PEP 8:
# Formatting
black .
# Linting
flake8 . --max-line-length=100
# Type checking
mypy .docker build -t mediabutler:latest .version: '3.8'
services:
mediabutler:
image: mediabutler:latest
container_name: mediabutler
restart: unless-stopped
env_file: .env
volumes:
- ${MOVIES_PATH}:/media/movies
- ${TV_PATH}:/media/tv
- ./session:/app/session
networks:
- media_network
networks:
media_network:
external: true# Docker logs
docker logs -f mediabutler
# Log file (if configured)
tail -f logs/mediabutler.logThe bot exposes internal metrics via the /status command:
- Active downloads
- Queued files
- Available space
- Download speed
- Web UI for management
- Jellyfin/Plex integration
- Subtitle support
- Playlist/channel downloads
- Customizable notifications
- Configuration backup/restore
- REST API for integrations
- Full multi-language support
Contributions welcome! See CONTRIBUTING.md for guidelines.
- Fork the project
- Create feature branch (
git checkout -b feature/AmazingFeature) - Commit changes (
git commit -m 'Add AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open Pull Request
This project is completely free and open source. If you find it useful and would like to support its continued development and updates, consider making a donation. Your support helps keep the project alive and motivates me to add new features and improvements!
Every contribution, no matter how small, is greatly appreciated! ❤️
Distributed under the MIT License. See LICENSE for details.
- Telethon - Telegram MTProto client
- TMDB - Metadata database
- aiohttp - Asynchronous HTTP client
- Self-hosted community ❤️
Bot for personal use. Respect copyright laws and only download content you have rights to.
Developed with ❤️ for the self-hosted community
