Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from selenium import webdriver
|
| 3 |
+
from selenium.webdriver.chrome.service import Service
|
| 4 |
+
from selenium.webdriver.chrome.options import Options
|
| 5 |
+
from selenium.webdriver.common.by import By
|
| 6 |
+
from selenium.webdriver.support.ui import WebDriverWait
|
| 7 |
+
from selenium.webdriver.support import expected_conditions as EC
|
| 8 |
+
import time
|
| 9 |
+
import random
|
| 10 |
+
import string
|
| 11 |
+
import re
|
| 12 |
+
from pydantic import BaseModel
|
| 13 |
+
from typing import Optional
|
| 14 |
+
import logging
|
| 15 |
+
|
| 16 |
+
# 配置日志
|
| 17 |
+
logging.basicConfig(
|
| 18 |
+
level=logging.INFO,
|
| 19 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 20 |
+
)
|
| 21 |
+
logger = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
+
app = FastAPI()
|
| 24 |
+
|
| 25 |
+
class EmailResponse(BaseModel):
|
| 26 |
+
email: str
|
| 27 |
+
verification_code: Optional[str] = None
|
| 28 |
+
error: Optional[str] = None
|
| 29 |
+
|
| 30 |
+
def setup_driver():
|
| 31 |
+
chrome_options = Options()
|
| 32 |
+
chrome_options.add_argument('--no-sandbox')
|
| 33 |
+
chrome_options.add_argument('--headless')
|
| 34 |
+
chrome_options.add_argument('--disable-dev-shm-usage')
|
| 35 |
+
chrome_options.add_argument('--disable-gpu')
|
| 36 |
+
chrome_options.binary_location = "/usr/bin/chromium"
|
| 37 |
+
|
| 38 |
+
driver = webdriver.Chrome(options=chrome_options)
|
| 39 |
+
return driver
|
| 40 |
+
|
| 41 |
+
def generate_email():
|
| 42 |
+
"""生成随机邮箱地址"""
|
| 43 |
+
random_prefix = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
|
| 44 |
+
return f"{random_prefix}@youxiang.dev"
|
| 45 |
+
|
| 46 |
+
def get_verification_code(driver, email, max_retries=10, delay=2):
|
| 47 |
+
try:
|
| 48 |
+
email_url = f'https://linshiyou.com/#/{email}'
|
| 49 |
+
|
| 50 |
+
# 打开新标签页并访问邮箱
|
| 51 |
+
driver.execute_script(f"window.open('{email_url}', '_blank')")
|
| 52 |
+
driver.switch_to.window(driver.window_handles[-1])
|
| 53 |
+
logger.info(f"已打开邮箱页面: {email}")
|
| 54 |
+
|
| 55 |
+
for attempt in range(max_retries):
|
| 56 |
+
try:
|
| 57 |
+
# 刷新页面
|
| 58 |
+
driver.refresh()
|
| 59 |
+
time.sleep(2)
|
| 60 |
+
|
| 61 |
+
# 获取邮件内容
|
| 62 |
+
body_content = driver.find_element(By.CLASS_NAME, "body").get_attribute('outerHTML')
|
| 63 |
+
logger.info(f"获取到邮件内容: {body_content}")
|
| 64 |
+
|
| 65 |
+
# 匹配验证码
|
| 66 |
+
match = re.search(r'\b\d{6}\b', body_content)
|
| 67 |
+
if match:
|
| 68 |
+
verification_code = match.group(0)
|
| 69 |
+
logger.info(f"成功获取验证码: {verification_code}")
|
| 70 |
+
|
| 71 |
+
# 关闭邮箱标签页
|
| 72 |
+
driver.close()
|
| 73 |
+
driver.switch_to.window(driver.window_handles[0])
|
| 74 |
+
|
| 75 |
+
return verification_code
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
logger.error(f"第 {attempt + 1} 次尝试失败: {str(e)}")
|
| 79 |
+
if attempt == max_retries - 1:
|
| 80 |
+
raise
|
| 81 |
+
|
| 82 |
+
time.sleep(delay)
|
| 83 |
+
logger.info(f"等待验证码,重试第 {attempt + 1} 次")
|
| 84 |
+
|
| 85 |
+
# 如果获取失败,确保回到主标签页
|
| 86 |
+
try:
|
| 87 |
+
driver.close()
|
| 88 |
+
driver.switch_to.window(driver.window_handles[0])
|
| 89 |
+
except:
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
+
return None
|
| 93 |
+
|
| 94 |
+
except Exception as e:
|
| 95 |
+
logger.error(f"获取验证码时发生错误: {str(e)}")
|
| 96 |
+
# 确保清理标签页
|
| 97 |
+
try:
|
| 98 |
+
driver.close()
|
| 99 |
+
driver.switch_to.window(driver.window_handles[0])
|
| 100 |
+
except:
|
| 101 |
+
pass
|
| 102 |
+
raise
|
| 103 |
+
|
| 104 |
+
@app.get("/")
|
| 105 |
+
def read_root():
|
| 106 |
+
return {"message": "临时邮箱服务运行中"}
|
| 107 |
+
|
| 108 |
+
@app.post("/generate-email")
|
| 109 |
+
def create_email():
|
| 110 |
+
email = generate_email()
|
| 111 |
+
logger.info(f"生成新邮箱: {email}")
|
| 112 |
+
return EmailResponse(email=email)
|
| 113 |
+
|
| 114 |
+
@app.get("/get-verification-code/{email}")
|
| 115 |
+
def get_code(email: str):
|
| 116 |
+
driver = None
|
| 117 |
+
try:
|
| 118 |
+
logger.info(f"开始获取邮箱验证码: {email}")
|
| 119 |
+
driver = setup_driver()
|
| 120 |
+
code = get_verification_code(driver, email)
|
| 121 |
+
if code:
|
| 122 |
+
return EmailResponse(email=email, verification_code=code)
|
| 123 |
+
raise HTTPException(status_code=404, detail="未找到验证码")
|
| 124 |
+
except Exception as e:
|
| 125 |
+
logger.error(f"获取验证码失败: {str(e)}")
|
| 126 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 127 |
+
finally:
|
| 128 |
+
if driver:
|
| 129 |
+
driver.quit()
|
| 130 |
+
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
import uvicorn
|
| 133 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|