This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
MediaButler is a Python-based Telegram bot for automatic media library organization. It uses Telethon for Telegram integration, downloads media files, organizes them by type (movies/TV series), and integrates with TMDB for metadata. The project now includes a FastAPI backend and React frontend for web dashboard management.
# Run locally with Python
python main.py
# Run with Docker Compose (recommended - runs bot, API, and web dashboard)
docker compose up -d
# Build Docker image
docker build -t mediabutler:latest .# Copy example environment file
cp .env.example .env
# Install Python dependencies
pip install -r requirements.txt# Backend (FastAPI) - from project root
python -m web.backend.main
# Frontend (React + Vite) - from web/frontend directory
cd web/frontend
npm install
npm run dev
# Production build
npm run build# Format code
black .
# Lint code
flake8 . --max-line-length=100
# Type checking
mypy .
# Frontend linting
cd web/frontend && npm run lint-
Main Entry Point (
main.py): MediaButler class orchestrates all components -
Core Modules (
core/):config.py: Dataclass-based configuration with environment variable loadingauth.py: Multi-user authorization with admin privilegesdownloader.py: Async download manager with queue system and concurrent workersspace_manager.py: Disk space monitoring with automatic cleanuptmdb_client.py: TMDB API integration for movie/TV metadatadatabase.py: SQLite database for download history and statisticssubtitle_manager.py: OpenSubtitles integration for subtitle downloadsextractor.py: Archive extraction manager for compressed files (ZIP, RAR, 7z)
-
Handlers (
handlers/):commands.py: Telegram command handlers with inline menu systemcallbacks.py: Inline button callback handlersfiles.py: File processing and media type detection
-
Models (
models/):download.py: DownloadInfo, DownloadStatus, QueueItem dataclasses
-
Utils (
utils/):naming.py: File name parsing and folder structure creationformatters.py: Telegram message formatting and progress barshelpers.py: Retry logic, validation, rate limiting utilities
-
Web (
web/):backend/: FastAPI REST API with JWT authentication, WebSocket supportfrontend/: React + Vite dashboard with Tailwind CSS
- Async/Await: Heavy use of asyncio for concurrent operations
- Manager Pattern: Each core component is a manager class (AuthManager, SpaceManager, etc.)
- Handler Registration: Telethon event handlers are registered in handler classes
- Dataclass Configuration: Centralized config with sections (TelegramConfig, TMDBConfig, PathsConfig, LimitsConfig, etc.)
- Queue-based Downloads: Async queue system with worker pattern for downloads
- Space-aware Processing: Downloads respect disk space limits with waiting queues
- Database Persistence: SQLite database tracks download history, statistics, and user preferences
The bot automatically organizes files into:
/media/
├── movies/
│ └── Movie Title (Year)/
│ └── Movie Title (Year).ext
└── tv/
└── Series Name [Language]/
└── Season XX/
└── Series Name - SXXEXX - Episode Title.ext
- File received → FileHandlers processes → Name parsing
- TMDB search (if enabled) → User selection via inline buttons
- Download queued → Worker picks up → Space check
- If space available: download directly
- If space insufficient: queue in space_waiting_queue
- Space monitor periodically processes waiting queue
- Archive extraction (if enabled and file is compressed):
- Detects ZIP, RAR, or 7z archives
- Extracts video files automatically
- Optionally deletes archive after extraction
- If database enabled: save download record
- If subtitles enabled: optionally auto-download subtitles
The project runs three services (defined in docker-compose.yml):
- mediabutler-bot: Telegram bot (main.py)
- mediabutler-api: FastAPI backend (web/backend/main.py)
- mediabutler-web: React frontend (web/frontend)
Services communicate via shared database and filesystem. The API can query download status and statistics from the database that the bot populates.
Python:
telethon: Telegram MTProto clientaiohttp: Async HTTP for TMDB/OpenSubtitles API callsaiosqlite: Async SQLite databasefastapi+uvicorn: Web API backendpython-dotenv: Environment variable loadingcryptg: Telegram encryption optimizationrarfile: RAR archive extraction (optional)py7zr: 7z archive extraction (optional)
JavaScript:
react+react-dom: UI frameworkvite: Frontend build tooltailwindcss: CSS frameworkaxios: HTTP client for API callsrecharts: Data visualization
- All code, comments, strings, and user-facing messages must be in English
- Main entry point is
main.py(notmediabutler.py) - Session files stored in configurable location for persistence
- All handlers use auth checks before processing
- Download manager uses retry logic with exponential backoff
- Space management includes automatic queue processing when space becomes available
- Database is optional but enables download history and web dashboard statistics
- Web dashboard requires JWT_SECRET_KEY configuration for production use
- Archive extraction automatically extracts compressed files (ZIP, RAR, 7z) to enable playback on Jellyfin and other media servers
- ZIP extraction works natively; RAR and 7z require optional dependencies (rarfile, py7zr)