forked from chengazhen/cursor-auto-free
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_email_code.py
90 lines (71 loc) · 2.73 KB
/
get_email_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import time
import re
from config import Config
import requests
class EmailVerificationHandler:
def __init__(self):
self.username = Config().get_temp_mail()
self.session = requests.Session()
self.emailExtension = "@mailto.plus"
def get_verification_code(self):
code = None
try:
print("正在处理...")
# 等待并获取最新邮件
code, first_id = self._get_latest_mail_code()
# 清理邮件
self._cleanup_mail(first_id)
except Exception as e:
print(f"获取验证码失败: {str(e)}")
return code
# 手动输入验证码
def _get_latest_mail_code(self):
# 获取邮件列表
mail_list_url = f"https://tempmail.plus/api/mails?email={self.username}{self.emailExtension}&limit=20&epin="
mail_list_response = self.session.get(mail_list_url)
mail_list_data = mail_list_response.json()
time.sleep(0.5)
if not mail_list_data.get("result"):
return None, None
# 获取最新邮件的ID
first_id = mail_list_data.get("first_id")
if not first_id:
return None, None
# 获取具体邮件内容
mail_detail_url = f"https://tempmail.plus/api/mails/{first_id}?email={self.username}{self.emailExtension}&epin="
mail_detail_response = self.session.get(mail_detail_url)
mail_detail_data = mail_detail_response.json()
time.sleep(0.5)
if not mail_detail_data.get("result"):
return None, None
# 从邮件文本中提取6位数字验证码
mail_text = mail_detail_data.get("text", "")
# 修改正则表达式,确保 6 位数字不紧跟在字母或域名相关符号后面
code_match = re.search(r"(?<![a-zA-Z@.])\b\d{6}\b", mail_text)
if code_match:
return code_match.group(), first_id
return None, None
def _cleanup_mail(self, first_id):
# 构造删除请求的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)