ARCHITECTURE.md

System Architecture

This document explains how the Ultimate Media Downloader is designed and how its different parts work together. If you are a computer science student or someone trying to understand the project structure, this guide will walk you through everything step by step.


Overview

The Ultimate Media Downloader is a Python-based command-line application that downloads media content from over 100 different platforms. The application follows a modular architecture where different components handle specific responsibilities. This makes the code easier to maintain, test, and extend.

Design Principles

The project follows these key principles:

  • Modularity: Each component does one thing well
  • Extensibility: New platforms can be added without changing existing code
  • Graceful Degradation: If optional features are not available, the app still works
  • User Experience: Beautiful CLI interface with progress indicators

High-Level Architecture

The following diagram shows how the main components interact with each other:

Loading diagram...

Component Breakdown

Entry Point

The application starts from ultimate_downloader.py. This file contains the main class UltimateMediaDownloader which orchestrates everything.

Loading diagram...

Main Components

ComponentFilePurpose
Main Downloaderultimate_downloader.pyCore application logic and coordination
Generic Downloadergeneric_downloader.pyHandles sites with special requirements like SSL bypass
CLI Parsercli_args.pyProcesses command-line arguments
Loggerlogger.pyCustom logging to suppress verbose output
YouTube Scoreryoutube_scorer.pyRanks YouTube search results for accuracy
Platform Infoplatform_info.pyDisplays supported platform information
Patch Handlerspatch_handlers.pyManages runtime patches and overrides for handlers

Data Flow

When a user provides a URL, here is what happens inside the application:

Loading diagram...

Handler System

The handler system is how the application supports different platforms. Each platform that needs special handling has its own handler class.

Handler Architecture

Loading diagram...

How Handlers Work

  1. Detection: The main class detects which platform a URL belongs to
  2. Delegation: If a handler exists for that platform, the request is delegated
  3. Processing: The handler extracts metadata and determines how to download
  4. Fallback: If direct download is not possible, handlers search YouTube and download from there

Spotify and Apple Music Flow

These platforms do not allow direct downloading. Here is how the application handles them:

Loading diagram...

Utility Modules

The utils/ directory contains helper modules that provide common functionality.

Module Overview

Loading diagram...

Key Utility Functions

ModuleFunctionDescription
utils.pysanitize_filename()Removes invalid characters from filenames
utils.pyformat_bytes()Converts bytes to human-readable format
utils.pydetect_platform()Identifies platform from URL
url_validator.pyis_valid_url()Checks if URL is properly formatted
url_validator.pycheck_url_support()Verifies if URL can be downloaded
terminal_image.pydisplay_image()Renders an image using sixel if supported
ui_components.pyIcons.get()Returns consistent icons for UI
ui_components.pyMessages.success()Formats success messages

Configuration Management

The application uses a JSON configuration file to store settings.

Configuration Structure

Loading diagram...

Environment Variables

Some settings can be configured through environment variables:

VariablePurpose
SPOTIFY_CLIENT_IDSpotify API authentication
SPOTIFY_CLIENT_SECRETSpotify API authentication
APPLE_MUSIC_TOKENApple Music direct download
APPLE_MUSIC_STOREFRONTApple Music region

Error Handling

The application handles errors gracefully to provide a good user experience.

Error Handling Flow

Loading diagram...

Fallback Strategies

  1. SSL Issues: The generic downloader creates permissive SSL contexts
  2. Rate Limiting: Automatic delays between requests
  3. Anti-bot Protection: Multiple request methods (cloudscraper, curl-cffi, selenium)
  4. Missing Handlers: Falls back to yt-dlp generic extractor

Summary

The Ultimate Media Downloader is built with a clean, modular architecture that separates concerns and makes the codebase maintainable. The handler system allows easy extension for new platforms, while the utility modules provide reusable functionality across the application.

Understanding this architecture will help you:

  • Navigate the codebase effectively
  • Add new features or platforms
  • Debug issues by tracing the data flow
  • Contribute improvements to the project

For more details on specific components, refer to the individual source files and their inline documentation.