import nest_asyncio import re import os from flask import Flask from protonmail import ProtonMail, exceptions # REMOVED webbrowser and threading as they are not needed on the server # Apply the patch needed for asyncio nest_asyncio.apply() # --- Configuration --- # SECURE: We now read your credentials from Hugging Face Space Secrets PROTONMAIL_USERNAME = "totalleecher@proton.me" PROTONMAIL_PASSWORD = "htz175039" # --- 1. The Core Proton Mail Logic --- def find_verification_codes(): """Logs into Proton Mail and extracts verification codes.""" pm = None found_codes = [] if not PROTONMAIL_USERNAME or not PROTONMAIL_PASSWORD: print("ERROR: Credentials not found in Space Secrets.") return [{"sender": "Configuration Error", "subject": "Please set secrets in your Space settings.", "code": "!!!"}] try: pm = ProtonMail() print("Attempting to log in...") pm.login(PROTONMAIL_USERNAME, PROTONMAIL_PASSWORD) print("Login successful!") print("Fetching latest emails...") messages = pm.get_messages(limit=15) keywords = ['verify', 'code', 'authenticate', 'verification'] for msg in messages: is_verification_email = any(keyword in msg.subject.lower() for keyword in keywords) or \ any(keyword in msg.sender.name.lower() for keyword in keywords) if is_verification_email: code = None subject_match = re.search(r'\d{6}', msg.subject) if subject_match: code = subject_match.group(0) else: body_match = re.search(r'\d{6}', msg.body) if body_match: code = body_match.group(0) if code: found_codes.append({ "sender": msg.sender.name, "subject": msg.subject, "code": code }) except exceptions.ProtonMailException as e: print(f"A Proton Mail error occurred: {e}") return [{"sender": "Error", "subject": "Could not log in or fetch mail.", "code": "Error"}] except Exception as e: print(f"A general error occurred: {e}") return [{"sender": "Error", "subject": "An unexpected error occurred.", "code": "Error"}] finally: if pm and hasattr(pm, 'user') and pm.user: print("Session will time out automatically.") pass unique_codes = list({v['code']: v for v in found_codes}.values()) return sorted(unique_codes, key=lambda x: x['subject'], reverse=True)[:5] # --- 2. The HTML, CSS, and JavaScript as Python Strings --- def build_html_page(codes): """Generates the full HTML page with embedded CSS and JS.""" cards_html = "" if codes: for item in codes: cards_html += f"""

From: {item['sender']}

Subject: {item['subject']}

{item['code']}
""" else: cards_html = '

No new verification codes found.

' # The entire page is built using an f-string return f""" Verification Codes

Proton Mail Verification Codes

Showing the latest unique codes found. Refresh the page to check again.


{cards_html}
""" # --- 3. The Flask Web Server --- app = Flask(__name__) @app.route('/') def index(): """Main page route that fetches codes and builds the HTML.""" codes = find_verification_codes() return build_html_page(codes) if __name__ == '__main__': app.run(host="0.0.0.0", port=7860)