================================================================================ GMAIL CLEANUP - CHALLENGE 3 SOLUTION Chapter 5: Complete Cleanup Workflow Challenge: Complete Workflow ================================================================================ PROBLEM: Build the full cleanup script with: 1. Configuration loading and validation 2. Gmail authentication 3. Finding and previewing emails 4. User confirmation 5. Deletion and reporting SOLUTION: ================================================================================ File: cleanup_complete.py import sys from gmail_utils import get_gmail_service, GmailCleanup from config import Config def show_menu(): """Show cleanup options.""" print("\n" + "=" * 60) print("CLEANUP OPTIONS") print("=" * 60) print("1. Delete old emails (365+ days)") print("2. Delete large attachments (10+ MB)") print("3. Delete from spam senders") print("4. Run all cleanup options") print("5. Exit") choice = input("\nSelect option (1-5): ") return choice def cleanup_old_emails(cleanup: GmailCleanup, config: Config): """Cleanup 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 preview_emails(cleanup, emails, limit=3) if confirm_deletion(len(emails)): for email in emails: cleanup.delete_email(email['id']) print(f"✅ Deleted {len(emails)} old emails") def cleanup_large_attachments(cleanup: GmailCleanup, config: Config): """Cleanup large attachments.""" print(f"\nFinding emails with attachments > {config.min_attachment_mb} MB...") emails = cleanup.find_large_attachments(config.min_attachment_mb) if not emails: print("No large attachment emails found") return preview_emails(cleanup, emails, limit=3) if confirm_deletion(len(emails)): for email in emails: cleanup.delete_email(email['id']) print(f"✅ Deleted {len(emails)} emails with large attachments") def cleanup_spam(cleanup: GmailCleanup): """Cleanup spam emails.""" spam_senders = [ "newsletter@promotions.com", "marketing@ads.com" ] total = 0 for sender in spam_senders: emails = cleanup.find_from_sender(sender) if emails and confirm_deletion(len(emails)): for email in emails: cleanup.delete_email(email['id']) total += len(emails) print(f"✅ Deleted {len(emails)} from {sender}") def preview_emails(cleanup: GmailCleanup, emails: list, limit: int = 3): """Show preview.""" print(f"\nPREVIEW ({min(limit, len(emails))} of {len(emails)}):") print("-" * 60) for email in emails[:limit]: details = cleanup.get_email_details(email['id']) if details: print(f"Subject: {details['subject'][:40]}") print(f"Size: {details['size_mb']:.2f} MB") def confirm_deletion(count: int) -> bool: """Ask confirmation.""" response = input(f"\nDelete {count} emails? (yes/no): ") return response.lower() == 'yes' def main(): """Main menu loop.""" print("=" * 60) print("🚀 GMAIL CLEANUP UTILITY") print("=" * 60) # Setup config = Config() if not config.validate(): return False service = get_gmail_service() cleanup = GmailCleanup(service, config) # Menu loop while True: choice = show_menu() if choice == '1': cleanup_old_emails(cleanup, config) elif choice == '2': cleanup_large_attachments(cleanup, config) elif choice == '3': cleanup_spam(cleanup) elif choice == '4': cleanup_old_emails(cleanup, config) cleanup_large_attachments(cleanup, config) cleanup_spam(cleanup) elif choice == '5': break else: print("Invalid choice") # Final report cleanup.print_report() return True if __name__ == "__main__": success = main() sys.exit(0 if success else 1) ================================================================================