From d02e5d69c87df24986f39fca19efcb3b104e371c Mon Sep 17 00:00:00 2001 From: jahv Date: Thu, 15 May 2025 00:30:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BATempMailPlusTab=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E8=BD=AE=E8=AF=A2=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=BB=A5=E6=A3=80=E6=9F=A5=E6=96=B0=E9=82=AE=E4=BB=B6=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89=E8=BD=AE=E8=AF=A2?= =?UTF-8?q?=E9=97=B4=E9=9A=94=E5=92=8C=E6=9C=80=E5=A4=A7=E5=B0=9D=E8=AF=95?= =?UTF-8?q?=E6=AC=A1=E6=95=B0=E3=80=82=E5=90=8C=E6=97=B6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E4=BB=A5=E5=8F=8D=E6=98=A0=E6=96=B0=E5=8F=82?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E5=B9=B6=E4=BC=98=E5=8C=96=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=B3=A8=E9=87=8A=E7=9A=84=E7=BF=BB=E8=AF=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- email_tabs/tempmail_plus_tab.py | 68 +++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/email_tabs/tempmail_plus_tab.py b/email_tabs/tempmail_plus_tab.py index 69efc41..521d1b4 100644 --- a/email_tabs/tempmail_plus_tab.py +++ b/email_tabs/tempmail_plus_tab.py @@ -1,19 +1,23 @@ import requests import re import datetime +import time from typing import Optional from .email_tab_interface import EmailTabInterface class TempMailPlusTab(EmailTabInterface): """Implementation of EmailTabInterface for tempmail.plus""" - def __init__(self, email: str, epin: str, translator=None): + def __init__(self, email: str, epin: str, translator=None, + polling_interval: int = 2, max_attempts: int = 10): """Initialize TempMailPlusTab Args: email: The email address to check epin: The epin token for authentication translator: Optional translator for internationalization + polling_interval: Time in seconds between polling attempts + max_attempts: Maximum number of polling attempts """ self.email = email self.epin = epin @@ -35,8 +39,13 @@ class TempMailPlusTab(EmailTabInterface): 'x-requested-with': 'XMLHttpRequest' } self.cookies = {'email': email} - self._cached_mail_id = None # 缓存mail_id - self._cached_verification_code = None # 缓存验证码 + self._cached_mail_id = None # Cache for mail_id + self._cached_verification_code = None # Cache for verification code + + # Polling configuration + self.polling_interval = polling_interval + self.max_attempts = max_attempts + self.current_attempt = 0 def refresh_inbox(self) -> None: """Refresh the email inbox""" @@ -45,6 +54,42 @@ class TempMailPlusTab(EmailTabInterface): def check_for_cursor_email(self) -> bool: """Check if there is a new email and immediately retrieve verification code + Returns: + bool: True if new email found and verification code retrieved, False otherwise + """ + # Reset attempt counter + self.current_attempt = 0 + + # Polling logic + while self.current_attempt < self.max_attempts: + found = self._check_email_once() + if found: + # Successfully found email and retrieved verification code + self.current_attempt = 0 # Reset counter for next use + return True + + # Not found, continue polling + self.current_attempt += 1 + if self.current_attempt < self.max_attempts: + # Print polling status information + if self.translator: + print(self.translator.get('tempmail.polling', + attempt=self.current_attempt, + max=self.max_attempts)) + else: + print(f"Polling for email: attempt {self.current_attempt}/{self.max_attempts}") + time.sleep(self.polling_interval) + + # Exceeded maximum attempts + if self.translator: + print(self.translator.get('tempmail.max_attempts_reached')) + else: + print(f"Max attempts ({self.max_attempts}) reached. No verification email found.") + return False + + def _check_email_once(self) -> bool: + """Single attempt to check for email + Returns: bool: True if new email found and verification code retrieved, False otherwise """ @@ -63,11 +108,11 @@ class TempMailPlusTab(EmailTabInterface): data = response.json() if data.get('result') and data.get('mail_list'): - # 检查邮件列表中的第一个邮件是否为新邮件 + # Check if the first email in the list is a new email if data['mail_list'][0].get('is_new') == True: - self._cached_mail_id = data['mail_list'][0].get('mail_id') # 缓存mail_id + self._cached_mail_id = data['mail_list'][0].get('mail_id') # Cache the mail_id - # 立即获取验证码 + # Immediately retrieve verification code verification_code = self._extract_verification_code() if verification_code: self._cached_verification_code = verification_code @@ -103,7 +148,7 @@ class TempMailPlusTab(EmailTabInterface): if not data.get('result'): return "" - # 验证发件人邮箱是否包含cursor字符串 + # Verify if sender email contains cursor string from_mail = data.get('from_mail', '') if 'cursor' not in from_mail.lower(): return "" @@ -129,13 +174,12 @@ class TempMailPlusTab(EmailTabInterface): if __name__ == "__main__": import os - import time import sys import configparser from config import get_config - # 尝试导入 translator + # Try to import translator try: from main import Translator translator = Translator() @@ -150,15 +194,15 @@ if __name__ == "__main__": print(f"{translator.get('tempmail.configured_email', email=email) if translator else f'Configured email: {email}'}") - # 初始化TempMailPlusTab,传递 translator + # Initialize TempMailPlusTab, pass translator mail_tab = TempMailPlusTab(email, epin, translator) - # 检查是否有Cursor的邮件 + # Check if there is a Cursor email print(f"{translator.get('tempmail.checking_email') if translator else 'Checking for Cursor verification email...'}") if mail_tab.check_for_cursor_email(): print(f"{translator.get('tempmail.email_found') if translator else 'Found Cursor verification email'}") - # 获取验证码 + # Get verification code verification_code = mail_tab.get_verification_code() if verification_code: print(f"{translator.get('tempmail.verification_code', code=verification_code) if translator else f'Verification code: {verification_code}'}")