This document provides an overview of the modular codebase structure in Telegram Channel Saver. The application has been refactored from a monolithic script into modular components to improve maintainability and readability.
telegram-channel-saver/
├── main.py # Main entry point
├── src/ # Source code modules
│ ├── __init__.py # Package initialization
│ ├── app.py # Main application class
│ ├── channels.py # Channel management functions
│ ├── client.py # Telegram client functions
│ ├── config.py # Configuration and constants
│ ├── database.py # Database operations
│ ├── media.py # Media handling functions
│ ├── messages.py # Message operations
│ └── users.py # User management functions
├── temp/ # Temporary data storage
│ ├── channel_saver/ # Database and session files
│ └── videos/ # Downloaded videos
└── docs/ # Documentation
The entry point for the application that imports and runs the main function from src/app.py. This provides a clean interface for users to start the application.
The core application module containing the ChannelSaver class which coordinates all functionality:
- Handles Telegram client initialization
- Manages user sessions
- Provides the main menu interface
- Coordinates operations between other modules
Contains all configuration settings and constants:
- Batch sizes and timing settings
- Media download parameters
- Directory settings
- Logging configuration
Handles database operations:
- Loading and saving the JSON database
- Database schema creation
- Provides a clean interface for other modules to access data
Manages Telegram client interactions:
- Authentication and login
- Session management
- Client creation and connection
Functions for channel management:
- Listing available channels/groups
- Displaying channel information
- Selecting active channel
- Retrieving channel statistics
User management functions:
- Saving channel users
- Displaying user statistics
- Listing saved users
Message operations:
- Saving channel messages
- Searching through messages
- Message display and formatting
- Handling message batches with rate limiting
Media handling functionality:
- Enhanced media download mechanism with retry logic
- Video download management
- Media information display
- Chunked downloads for large files
The codebase follows a hierarchical dependency structure:
config.py- No dependencies on other modules, only standard library importsdatabase.py- Depends onconfig.pyfor directory settingsclient.py- Depends onconfig.pyanddatabase.pychannels.py- Depends ondatabase.pyfor storing channel datausers.py- Depends onchannels.pyanddatabase.pymedia.py- Depends onconfig.py, but has minimal dependenciesmessages.py- Depends onchannels.py,database.py, andmedia.pyapp.py- Depends on all other modules to orchestrate the application
This structure ensures that lower-level modules don't depend on higher-level ones, reducing circular dependencies.
- Sets up logging
- Defines batch sizes for message downloads
- Controls delay times to avoid rate limiting
- Sets timeouts and retry parameters
- Configures directory paths
- Loads JSON database or creates new if none exists
- Saves database state
- Provides schema for users, sessions, messages, etc.
- Creates and initializes Telegram client
- Authenticates users (login, 2FA)
- Manages sessions (saving, restoring)
- Lists all available channels/groups
- Displays channel information in a readable format
- Selects active channel for operations
- Shows channel statistics
- Retrieves and saves channel participants
- Tracks user information (premium status, etc.)
- Displays user statistics
- Lists saved users
- Downloads messages in batches
- Handles rate limiting and retries
- Processes message content (text, reactions)
- Provides search functionality
- Downloads media with progress tracking
- Handles large file downloads efficiently
- Manages video downloads
- Provides retry mechanisms for failed downloads
- Initializes application components
- Provides user interface (menu)
- Coordinates between modules
- Handles application flow
- Identify which module should contain your feature
- Add necessary functions to that module
- If needed, update
app.pyto expose the feature in the UI - Update configuration in
config.pyif new settings are required
- Locate the module containing the feature
- Make changes while maintaining the module's responsibility
- Ensure changes don't break dependencies
- Update documentation to reflect changes
When working with this codebase:
- Maintain separation of concerns: Keep each module focused on its specific responsibility
- Avoid circular dependencies: Lower-level modules shouldn't import higher-level ones
- Update configuration: Use
config.pyfor constants rather than hardcoding values - Follow existing patterns: Maintain consistency with the established code style
- Document changes: Update comments and documentation when making significant changes