Configuration Management

Crunchyroll Downloader — Practical Scripting
Course 1 · Chapter 2 · Configuration Management

⚙️ Configuration Management

Learn to load and manage Crunchyroll credentials securely, validate configuration, and handle sensitive data properly. This is critical for keeping your login information safe while building reusable code.

🔒 Building a Secure Config Class

import os
from pathlib import Path
from dotenv import load_dotenv

ENV_FILE = Path(__file__).parent / ".env"
load_dotenv(ENV_FILE)

class Config:
    """Crunchyroll configuration."""

    def __init__(self):
        self.email = os.environ.get('CRUNCHYROLL_EMAIL')
        self.password = os.environ.get('CRUNCHYROLL_PASSWORD')
        self.api_base = os.environ.get('API_BASE_URL', 'https://api.crunchyroll.com')
        self.token_cache = os.environ.get('TOKEN_CACHE_FILE', 'token_cache.json')

    def validate(self) -> bool:
        """Validate configuration."""
        if not self.email or not self.password:
            print("❌ Missing email or password in .env")
            return False
        if '@' not in self.email:
            print("❌ Invalid email format")
            return False
        return True

💻 Coding Challenges

Challenge 1: Config Class

Create config.py with Config class that loads and stores credentials.

Challenge 2: Validation

Extend Config to validate email format and required fields.

Challenge 3: Error Handling

Add error handling for missing .env file and type conversion errors.