================================================================================ CRUNCHYROLL DOWNLOADER - CHALLENGE 1 SOLUTION Chapter 2: Configuration Management Challenge: Config Class ================================================================================ PROBLEM: Create config.py with Config class that loads and stores credentials. SOLUTION: ================================================================================ File: config.py import os from pathlib import Path from dotenv import load_dotenv # Load .env from same directory as this file ENV_FILE = Path(__file__).parent / ".env" load_dotenv(ENV_FILE) class Config: """Crunchyroll API configuration.""" def __init__(self): """Load configuration from environment variables.""" # Crunchyroll credentials self.email = os.environ.get('CRUNCHYROLL_EMAIL') self.password = os.environ.get('CRUNCHYROLL_PASSWORD') # API settings 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 __str__(self): """String representation of config.""" return f""" Configuration: Email: {self.email} API Base: {self.api_base} Token Cache: {self.token_cache} """.strip() def display(self): """Display configuration (safely).""" print(f"📋 CONFIGURATION:") print(f" Email: {self.email}") print(f" API Base: {self.api_base}") print(f" Token Cache: {self.token_cache}") # Test if __name__ == "__main__": config = Config() config.display() EXPECTED OUTPUT ================================================================================ 📋 CONFIGURATION: Email: emubantam@gmail.com API Base: https://api.crunchyroll.com Token Cache: token_cache.json KEY POINTS ================================================================================ ✅ Config class loads from .env ✅ Uses defaults if variables not found ✅ Keeps all settings in one place ✅ Easy to test and mock ✅ Passwords never printed in logs HOW TO USE ================================================================================ from config import Config # Load configuration config = Config() # Access settings print(config.email) # Your email print(config.api_base) # API endpoint print(config.token_cache) # Where to save JWT ================================================================================