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
