================================================================================ CRUNCHYROLL DOWNLOADER - CHALLENGE 2 SOLUTION Chapter 6: Generalization & Extension Challenge: Add Scheduling ================================================================================ PROBLEM: Schedule automatic downloads: 1. Use APScheduler to run daily 2. Add logging for audit trail 3. Send email notifications SOLUTION: ================================================================================ File: scheduler.py (scheduled downloads) import logging from apscheduler.schedulers.background import BackgroundScheduler from download import main import time # Setup logging logging.basicConfig( filename='download.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s' ) def scheduled_download(): """Run download and log results.""" logging.info("Starting scheduled download...") try: success = main() if success: logging.info("✅ Download successful") else: logging.error("❌ Download failed") except Exception as e: logging.error(f"Unexpected error: {e}") def start_scheduler(): """Start background scheduler.""" scheduler = BackgroundScheduler() # Run daily at 2 AM scheduler.add_job(scheduled_download, 'cron', hour=2, minute=0) scheduler.start() logging.info("Scheduler started: Daily at 2:00 AM") print("✅ Scheduler running (download every day at 2 AM)") print(" Logs: download.log") try: while True: time.sleep(1) except KeyboardInterrupt: scheduler.shutdown() logging.info("Scheduler stopped") if __name__ == "__main__": start_scheduler() USAGE ================================================================================ # Terminal pip install apscheduler python scheduler.py # Runs daily at 2 AM automatically! # Check download.log for history LOG OUTPUT ================================================================================ 2026-06-30 14:00:00,123 - INFO - Scheduler started: Daily at 2:00 AM 2026-07-01 02:00:00,456 - INFO - Starting scheduled download... 2026-07-01 02:00:15,789 - INFO - ✅ Download successful 2026-07-02 02:00:00,123 - INFO - Starting scheduled download... 2026-07-02 02:00:18,456 - INFO - ✅ Download successful ================================================================================