File size: 5,056 Bytes
45dc1fe 2e3e64f 45dc1fe 2e3e64f 45dc1fe 2e3e64f 45dc1fe 2e3e64f 45dc1fe bb4300b 45dc1fe 2e3e64f 45dc1fe 2e3e64f 45dc1fe 2e3e64f 45dc1fe 2e3e64f 45dc1fe 2e3e64f 45dc1fe 2e3e64f 45dc1fe 2e3e64f 45dc1fe 2e3e64f 45dc1fe 2e3e64f 45dc1fe f70609b 2e3e64f 45dc1fe f70609b 45dc1fe f70609b 2e3e64f 45dc1fe 2e3e64f 45dc1fe f70609b 45dc1fe f70609b 45dc1fe 2e3e64f 45dc1fe f70609b 2e3e64f f70609b 45dc1fe f70609b 45dc1fe 2e3e64f 45dc1fe | 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | import gradio as gr
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time
import random
import string
import logging
import re
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def setup_driver():
"""设置 Chrome 驱动"""
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-software-rasterizer')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options)
return driver
def get_verification_code(driver, email, max_retries=10, delay=2):
"""获取验证码"""
email_url = f'https://linshiyou.com/#/{email}'
try:
driver.get(email_url)
for attempt in range(max_retries):
try:
driver.refresh()
time.sleep(delay)
body_content = driver.find_element(By.CLASS_NAME, "body").get_attribute('outerHTML')
logger.info(f"获取到邮件内容: {body_content}")
match = re.search(r'\b\d{6}\b', body_content)
if match:
verification_code = match.group(0)
logger.info(f"获取到验证码: {verification_code}")
return verification_code
except Exception as e:
logger.error(f"第 {attempt + 1} 次获取验证码失败: {str(e)}")
logger.info(f"等待验证码,重试第 {attempt + 1} 次")
time.sleep(delay)
except Exception as e:
logger.error(f"打开邮箱页面失败: {str(e)}")
return None
def generate_random_email():
"""生成随机邮箱地址"""
try:
random_prefix = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
email = f"{random_prefix}@youxiang.dev"
logger.info(f"生成随机邮箱: {email}")
return {"status": "success", "email": email}
except Exception as e:
logger.error(f"生成邮箱失败: {str(e)}")
return {"status": "error", "message": str(e)}
def get_email_code(email: str):
"""获取指定邮箱的验证码"""
if not email:
return {"status": "error", "message": "邮箱地址不能为空"}
logger.info(f"开始获取邮箱验证码: {email}")
driver = None
try:
driver = setup_driver()
code = get_verification_code(driver, email)
if code:
logger.info(f"成功获取验证码: {code}")
return {"status": "success", "code": code}
else:
logger.error("未能获取验证码")
return {"status": "error", "message": "未能获取验证码"}
except Exception as e:
logger.error(f"获取验证码过程出错: {str(e)}")
return {"status": "error", "message": str(e)}
finally:
if driver:
try:
driver.quit()
except:
pass
# 创建 Gradio 界面
with gr.Blocks(title="邮箱验证码服务") as demo:
gr.Markdown("""
# 邮箱验证码服务
提供两个主要功能:
1. 生成随机临时邮箱
2. 获取邮箱验证码
""")
with gr.Tab("生成随机邮箱"):
with gr.Row():
email_btn = gr.Button("生成邮箱", variant="primary")
email_output = gr.JSON(label="结果")
with gr.Tab("获取验证码"):
with gr.Row():
email_input = gr.Textbox(
label="邮箱地址",
placeholder="请输入要获取验证码的邮箱地址",
info="支持 @youxiang.dev 域名的邮箱"
)
with gr.Row():
code_btn = gr.Button("获取验证码", variant="primary")
code_output = gr.JSON(label="结果")
# 绑定事件处理
email_btn.click(
fn=generate_random_email,
inputs=[],
outputs=email_output,
api_name="generate_email"
)
code_btn.click(
fn=get_email_code,
inputs=email_input,
outputs=code_output,
api_name="get_code"
)
# 启用队列处理请求
demo.queue()
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860) |