perf: 优化获取邮箱验证码的逻辑,代码来源 @frozenblackflame
This commit is contained in:
@@ -41,12 +41,13 @@ class Config:
|
|||||||
if not self.check_is_valid(self.domain):
|
if not self.check_is_valid(self.domain):
|
||||||
raise ValueError("域名未配置,请在 .env 文件中设置 DOMAIN")
|
raise ValueError("域名未配置,请在 .env 文件中设置 DOMAIN")
|
||||||
|
|
||||||
logging.info(f"\033[32m临时邮箱: {self.temp_mail}\033[0m")
|
|
||||||
logging.info(f"\033[32m域名: {self.domain}\033[0m")
|
|
||||||
|
|
||||||
def check_is_valid(self, str):
|
def check_is_valid(self, str):
|
||||||
return len(str.strip()) > 0
|
return len(str.strip()) > 0
|
||||||
|
|
||||||
|
def print_config(self):
|
||||||
|
logging.info(f"\033[32m临时邮箱: {self.temp_mail}\033[0m")
|
||||||
|
logging.info(f"\033[32m域名: {self.domain}\033[0m")
|
||||||
|
|
||||||
|
|
||||||
# 使用示例
|
# 使用示例
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -216,7 +216,9 @@ class EmailGenerator:
|
|||||||
)
|
)
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
self.domain = Config().get_domain()
|
configInstance = Config()
|
||||||
|
configInstance.print_config()
|
||||||
|
self.domain = configInstance.get_domain()
|
||||||
self.default_password = password
|
self.default_password = password
|
||||||
self.default_first_name = self.generate_random_name()
|
self.default_first_name = self.generate_random_name()
|
||||||
self.default_last_name = self.generate_random_name()
|
self.default_last_name = self.generate_random_name()
|
||||||
@@ -224,7 +226,9 @@ class EmailGenerator:
|
|||||||
def generate_random_name(self, length=6):
|
def generate_random_name(self, length=6):
|
||||||
"""生成随机用户名"""
|
"""生成随机用户名"""
|
||||||
first_letter = random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
first_letter = random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||||
rest_letters = ''.join(random.choices("abcdefghijklmnopqrstuvwxyz", k=length-1))
|
rest_letters = "".join(
|
||||||
|
random.choices("abcdefghijklmnopqrstuvwxyz", k=length - 1)
|
||||||
|
)
|
||||||
return first_letter + rest_letters
|
return first_letter + rest_letters
|
||||||
|
|
||||||
def generate_email(self, length=8):
|
def generate_email(self, length=8):
|
||||||
@@ -254,7 +258,7 @@ if __name__ == "__main__":
|
|||||||
browser = browser_manager.init_browser()
|
browser = browser_manager.init_browser()
|
||||||
|
|
||||||
logging.info("正在初始化邮箱验证模块...")
|
logging.info("正在初始化邮箱验证模块...")
|
||||||
email_handler = EmailVerificationHandler(browser)
|
email_handler = EmailVerificationHandler()
|
||||||
|
|
||||||
logging.info("\n=== 配置信息 ===")
|
logging.info("\n=== 配置信息 ===")
|
||||||
login_url = "https://authenticator.cursor.sh"
|
login_url = "https://authenticator.cursor.sh"
|
||||||
|
|||||||
@@ -1,81 +1,89 @@
|
|||||||
from DrissionPage.common import Keys
|
|
||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
from config import Config
|
from config import Config
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
class EmailVerificationHandler:
|
class EmailVerificationHandler:
|
||||||
def __init__(self, browser, mail_url="https://tempmail.plus"):
|
def __init__(self):
|
||||||
self.browser = browser
|
|
||||||
self.mail_url = mail_url
|
|
||||||
self.username = Config().get_temp_mail()
|
self.username = Config().get_temp_mail()
|
||||||
|
self.session = requests.Session()
|
||||||
|
self.emailExtension = "@mailto.plus"
|
||||||
|
|
||||||
def get_verification_code(self):
|
def get_verification_code(self):
|
||||||
code = None
|
code = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("正在处理...")
|
print("正在处理...")
|
||||||
# 打开新标签页访问临时邮箱
|
|
||||||
tab_mail = self.browser.new_tab(self.mail_url)
|
|
||||||
self.browser.activate_tab(tab_mail)
|
|
||||||
|
|
||||||
# 输入用户名
|
|
||||||
self._input_username(tab_mail)
|
|
||||||
|
|
||||||
# 等待并获取最新邮件
|
# 等待并获取最新邮件
|
||||||
code = self._get_latest_mail_code(tab_mail)
|
code, first_id = self._get_latest_mail_code()
|
||||||
|
|
||||||
# 清理邮件
|
# 清理邮件
|
||||||
self._cleanup_mail(tab_mail)
|
self._cleanup_mail(first_id)
|
||||||
|
|
||||||
# 关闭标签页
|
|
||||||
tab_mail.close()
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"获取验证码失败: {str(e)}")
|
print(f"获取验证码失败: {str(e)}")
|
||||||
|
|
||||||
return code
|
return code
|
||||||
|
|
||||||
def _input_username(self, tab):
|
# 手动输入验证码
|
||||||
while True:
|
def _get_latest_mail_code(self):
|
||||||
if tab.ele("@id=pre_button"):
|
# 获取邮件列表
|
||||||
tab.actions.click("@id=pre_button")
|
mail_list_url = f"https://tempmail.plus/api/mails?email={self.username}{self.emailExtension}&limit=20&epin="
|
||||||
time.sleep(0.5)
|
mail_list_response = self.session.get(mail_list_url)
|
||||||
tab.run_js('document.getElementById("pre_button").value = ""')
|
mail_list_data = mail_list_response.json()
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
tab.actions.input(self.username).key_down(Keys.ENTER).key_up(Keys.ENTER)
|
if not mail_list_data.get("result"):
|
||||||
break
|
return None, None
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
def _get_latest_mail_code(self, tab):
|
# 获取最新邮件的ID
|
||||||
code = None
|
first_id = mail_list_data.get("first_id")
|
||||||
while True:
|
if not first_id:
|
||||||
new_mail = tab.ele("@class=mail")
|
return None, None
|
||||||
if new_mail:
|
|
||||||
if new_mail.text:
|
|
||||||
tab.actions.click("@class=mail")
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
if tab.ele("@class=overflow-auto mb-20"):
|
# 获取具体邮件内容
|
||||||
email_content = tab.ele("@class=overflow-auto mb-20").text
|
mail_detail_url = f"https://tempmail.plus/api/mails/{first_id}?email={self.username}{self.emailExtension}&epin="
|
||||||
verification_code = re.search(
|
mail_detail_response = self.session.get(mail_detail_url)
|
||||||
r"verification code is (\d{6})", email_content
|
mail_detail_data = mail_detail_response.json()
|
||||||
)
|
time.sleep(0.5)
|
||||||
if verification_code:
|
if not mail_detail_data.get("result"):
|
||||||
code = verification_code.group(1)
|
return None, None
|
||||||
print("马上就要成功了")
|
|
||||||
else:
|
|
||||||
print("执行失败")
|
|
||||||
|
|
||||||
return code
|
# 从邮件文本中提取6位数字验证码
|
||||||
|
mail_text = mail_detail_data.get("text", "")
|
||||||
|
code_match = re.search(r"\b\d{6}\b", mail_text)
|
||||||
|
|
||||||
def _cleanup_mail(self, tab):
|
if code_match:
|
||||||
if tab.ele("@id=delete_mail"):
|
return code_match.group(), first_id
|
||||||
tab.actions.click("@id=delete_mail")
|
return None, None
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
if tab.ele("@id=confirm_mail"):
|
def _cleanup_mail(self, first_id):
|
||||||
tab.actions.click("@id=confirm_mail")
|
# 构造删除请求的URL和数据
|
||||||
|
delete_url = "https://tempmail.plus/api/mails/"
|
||||||
|
payload = {
|
||||||
|
"email": f"{self.username}{self.emailExtension}",
|
||||||
|
"first_id": first_id,
|
||||||
|
"epin": "",
|
||||||
|
}
|
||||||
|
|
||||||
|
# 最多尝试5次
|
||||||
|
for _ in range(5):
|
||||||
|
response = self.session.delete(delete_url, data=payload)
|
||||||
|
try:
|
||||||
|
result = response.json().get("result")
|
||||||
|
if result is True:
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 如果失败,等待0.5秒后重试
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
email_handler = EmailVerificationHandler()
|
||||||
|
code = email_handler.get_verification_code()
|
||||||
|
print(code)
|
||||||
|
|||||||
Reference in New Issue
Block a user