================================================================================ GMAIL CLEANUP - CHALLENGE 1 SOLUTION Chapter 5: Complete Cleanup Workflow Challenge: Safe Deletion ================================================================================ PROBLEM: Create a workflow that: 1. Finds 10 old emails 2. Displays subject, sender, date for preview 3. Asks user to confirm 4. Deletes only if confirmed SOLUTION: ================================================================================ File: cleanup_safe.py import sys from gmail_utils import get_gmail_service, GmailCleanup from config import Config def preview_emails(cleanup: GmailCleanup, emails: list, limit: int = 5): """Show preview of emails.""" print(f"\nšŸ“‹ PREVIEW ({min(limit, len(emails))} of {len(emails)}):") print("-" * 80) for email in emails[:limit]: details = cleanup.get_email_details(email['id']) if details: print(f"Subject: {details['subject'][:60]}") print(f"From: {details['from'][:60]}") print(f"Date: {details['date']}") print(f"Size: {details['size_mb']:.2f} MB") print("-" * 80) def confirm_deletion(email_count: int) -> bool: """Ask user to confirm deletion.""" print(f"\nāš ļø About to delete {email_count} emails!") response = input("Type 'yes' to confirm, 'no' to cancel: ") return response.lower() == 'yes' def main(): print("=" * 80) print("šŸš€ GMAIL CLEANUP - SAFE MODE") print("=" * 80) # Load config config = Config() if not config.validate(): return False # Connect service = get_gmail_service() cleanup = GmailCleanup(service, config) # Find old emails print(f"\nFinding emails older than {config.days_old} days...") emails = cleanup.find_old_emails(config.days_old) if not emails: print("āœ… No old emails found!") return True print(f"āœ… Found {len(emails)} emails") # Preview preview_emails(cleanup, emails) # Confirm if not confirm_deletion(len(emails)): print("āŒ Cleanup cancelled") return False # Delete print("\nšŸ—‘ļø Deleting emails...") for email in emails: cleanup.delete_email(email['id']) # Report cleanup.print_report() return True if __name__ == "__main__": success = main() sys.exit(0 if success else 1) EXAMPLE INTERACTION ================================================================================ šŸš€ GMAIL CLEANUP - SAFE MODE ================================================================================ Finding emails older than 365 days... āœ… Found 10 emails šŸ“‹ PREVIEW (5 of 10): -------------------------------------------------------------------------------- Subject: Old Newsletter #1 From: newsletter@company.com Date: Tue, 1 Jan 2023 12:00:00 Size: 0.50 MB -------------------------------------------------------------------------------- Subject: Promotional Email From: promo@store.com Date: Thu, 15 Jan 2023 09:30:00 Size: 1.20 MB ... āš ļø About to delete 10 emails! Type 'yes' to confirm, 'no' to cancel: yes šŸ—‘ļø Deleting emails... ============================================================ CLEANUP REPORT ============================================================ Emails deleted: 10 Emails archived: 0 Storage freed: 10.40 MB ============================================================ ================================================================================