================================================================================ CRUNCHYROLL DOWNLOADER - CHALLENGE 3 SOLUTION Chapter 5: Complete Workflow Challenge: Complete Script ================================================================================ PROBLEM: Build main() that orchestrates entire workflow with error handling. SOLUTION: ================================================================================ File: download.py (complete with all steps) import sys from config import Config from crunchyroll_utils import login, load_cached_token, save_token, CrunchyrollClient, parse_episodes import csv def export_to_csv(episodes, filename="episodes.csv"): with open(filename, 'w', newline='', encoding='utf-8') as f: writer = csv.DictWriter(f, fieldnames=[ 'show_name', 'episode_number', 'episode_name', 'date_watched' ]) writer.writeheader() writer.writerows(episodes) print(f"✅ Exported {len(episodes)} episodes to {filename}") def main(): print("\n" + "=" * 70) print("🚀 CRUNCHYROLL EPISODE DOWNLOADER") print("=" * 70 + "\n") # STEP 1: Load config print("STEP 1: Loading configuration...") config = Config() if not config.validate(): return False # STEP 2: Get JWT token print("\nSTEP 2: Getting JWT token...") token = load_cached_token(config.token_cache) if not token: print(" Logging in...") token = login(config) if not token: return False save_token(token, config.token_cache) # STEP 3: Fetch episodes print("\nSTEP 3: Fetching episodes...") client = CrunchyrollClient(config, token) episodes = client.get_episodes() if not episodes: print("❌ No episodes found") return False # STEP 4: Parse data print("\nSTEP 4: Parsing episode data...") parsed = parse_episodes(episodes) # STEP 5: Export to CSV print("\nSTEP 5: Exporting to CSV...") export_to_csv(parsed, "episodes.csv") # Summary print("\n" + "=" * 70) print("✅ COMPLETE!") print("=" * 70) print(f"Downloaded: {len(parsed)} episodes") print(f"Saved to: episodes.csv") return True if __name__ == "__main__": success = main() sys.exit(0 if success else 1) EXPECTED OUTPUT ================================================================================ ====================================================================== 🚀 CRUNCHYROLL EPISODE DOWNLOADER ====================================================================== STEP 1: Loading configuration... ✅ Configuration is valid! STEP 2: Getting JWT token... ✅ Using cached token STEP 3: Fetching episodes... 📡 Fetching episodes... ✅ Fetched 147 episodes STEP 4: Parsing episode data... STEP 5: Exporting to CSV... 💾 Exporting to episodes.csv... ✅ Exported 147 episodes to episodes.csv ====================================================================== ✅ COMPLETE! ====================================================================== Downloaded: 147 episodes Saved to: episodes.csv ================================================================================