Update nb2.py
Browse files
nb2.py
CHANGED
|
@@ -85,7 +85,6 @@ class MailGw:
|
|
| 85 |
except Exception as e:
|
| 86 |
logger.error(f"创建邮箱时发生错误: {str(e)}")
|
| 87 |
return None
|
| 88 |
-
|
| 89 |
def get_verification_code(self, max_retries=10, delay=2):
|
| 90 |
if not self.token:
|
| 91 |
logger.error("Token不存在")
|
|
@@ -131,8 +130,9 @@ class MailGw:
|
|
| 131 |
|
| 132 |
logger.error("未能获取到验证码")
|
| 133 |
return None
|
|
|
|
| 134 |
def get_cookie(driver):
|
| 135 |
-
try:
|
| 136 |
cookie_script = """
|
| 137 |
var cookieString = document.cookie;
|
| 138 |
var essentialCookies = cookieString.split('; ').filter(function(cookie) {
|
|
@@ -190,7 +190,6 @@ def wait_for_email_input(driver, timeout=10):
|
|
| 190 |
time.sleep(0.5)
|
| 191 |
logger.error("邮箱输入框未出现")
|
| 192 |
return False
|
| 193 |
-
|
| 194 |
def input_email(driver, email):
|
| 195 |
"""使用JavaScript输入邮箱"""
|
| 196 |
try:
|
|
@@ -292,4 +291,101 @@ def registration_process(driver, wait):
|
|
| 292 |
|
| 293 |
except Exception as e:
|
| 294 |
logger.error(f"注册过程错误: {str(e)}")
|
| 295 |
-
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
logger.error(f"创建邮箱时发生错误: {str(e)}")
|
| 87 |
return None
|
|
|
|
| 88 |
def get_verification_code(self, max_retries=10, delay=2):
|
| 89 |
if not self.token:
|
| 90 |
logger.error("Token不存在")
|
|
|
|
| 130 |
|
| 131 |
logger.error("未能获取到验证码")
|
| 132 |
return None
|
| 133 |
+
|
| 134 |
def get_cookie(driver):
|
| 135 |
+
try:
|
| 136 |
cookie_script = """
|
| 137 |
var cookieString = document.cookie;
|
| 138 |
var essentialCookies = cookieString.split('; ').filter(function(cookie) {
|
|
|
|
| 190 |
time.sleep(0.5)
|
| 191 |
logger.error("邮箱输入框未出现")
|
| 192 |
return False
|
|
|
|
| 193 |
def input_email(driver, email):
|
| 194 |
"""使用JavaScript输入邮箱"""
|
| 195 |
try:
|
|
|
|
| 291 |
|
| 292 |
except Exception as e:
|
| 293 |
logger.error(f"注册过程错误: {str(e)}")
|
| 294 |
+
return False
|
| 295 |
+
|
| 296 |
+
app = FastAPI()
|
| 297 |
+
cookie_generator = None
|
| 298 |
+
|
| 299 |
+
class CookieGenerator:
|
| 300 |
+
def __init__(self):
|
| 301 |
+
self.driver = None
|
| 302 |
+
self.running = False
|
| 303 |
+
self.cookies = []
|
| 304 |
+
|
| 305 |
+
async def start_generation(self):
|
| 306 |
+
if self.running:
|
| 307 |
+
return
|
| 308 |
+
|
| 309 |
+
self.running = True
|
| 310 |
+
try:
|
| 311 |
+
chrome_options = Options()
|
| 312 |
+
chrome_options.add_argument('--headless=new')
|
| 313 |
+
chrome_options.add_argument('--no-sandbox')
|
| 314 |
+
chrome_options.add_argument('--disable-dev-shm-usage')
|
| 315 |
+
chrome_options.add_argument('--disable-gpu')
|
| 316 |
+
chrome_options.add_argument('--disable-software-rasterizer')
|
| 317 |
+
chrome_options.add_argument('--disable-extensions')
|
| 318 |
+
chrome_options.add_argument('--disable-setuid-sandbox')
|
| 319 |
+
chrome_options.add_argument('--disable-web-security')
|
| 320 |
+
chrome_options.add_argument('--no-first-run')
|
| 321 |
+
chrome_options.add_argument('--no-default-browser-check')
|
| 322 |
+
chrome_options.add_argument('--allow-running-insecure-content')
|
| 323 |
+
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
|
| 324 |
+
chrome_options.add_argument('--window-size=1920,1080')
|
| 325 |
+
chrome_options.add_argument('--user-data-dir=/tmp/chrome-data')
|
| 326 |
+
chrome_options.add_argument('--disable-features=VizDisplayCompositor')
|
| 327 |
+
chrome_options.add_argument('--disable-dev-tools')
|
| 328 |
+
chrome_options.binary_location = os.getenv('CHROME_BIN', '/usr/bin/chromium')
|
| 329 |
+
|
| 330 |
+
service = Service(
|
| 331 |
+
os.getenv('CHROMEDRIVER_PATH', '/usr/bin/chromedriver')
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
self.driver = webdriver.Chrome(service=service, options=chrome_options)
|
| 335 |
+
wait = WebDriverWait(self.driver, 20)
|
| 336 |
+
|
| 337 |
+
while self.running:
|
| 338 |
+
success = registration_process(self.driver, wait)
|
| 339 |
+
if success:
|
| 340 |
+
logger.info("本次循环成功")
|
| 341 |
+
else:
|
| 342 |
+
logger.error("本次循环失败")
|
| 343 |
+
await asyncio.sleep(5)
|
| 344 |
+
|
| 345 |
+
except Exception as e:
|
| 346 |
+
logger.error(f"Cookie生成错误: {str(e)}")
|
| 347 |
+
if hasattr(e, 'msg'):
|
| 348 |
+
logger.error(f"错误消息: {e.msg}")
|
| 349 |
+
finally:
|
| 350 |
+
self.running = False
|
| 351 |
+
if self.driver:
|
| 352 |
+
self.driver.quit()
|
| 353 |
+
|
| 354 |
+
@app.on_event("startup")
|
| 355 |
+
async def startup_event():
|
| 356 |
+
global cookie_generator
|
| 357 |
+
cookie_generator = CookieGenerator()
|
| 358 |
+
|
| 359 |
+
@app.get("/")
|
| 360 |
+
async def read_root():
|
| 361 |
+
return {"status": "running"}
|
| 362 |
+
|
| 363 |
+
@app.get("/start")
|
| 364 |
+
async def start_generation(background_tasks: BackgroundTasks):
|
| 365 |
+
global cookie_generator
|
| 366 |
+
if not cookie_generator.running:
|
| 367 |
+
background_tasks.add_task(cookie_generator.start_generation)
|
| 368 |
+
return {"status": "started"}
|
| 369 |
+
return {"status": "already running"}
|
| 370 |
+
|
| 371 |
+
@app.get("/stop")
|
| 372 |
+
async def stop_generation():
|
| 373 |
+
global cookie_generator
|
| 374 |
+
if cookie_generator.running:
|
| 375 |
+
cookie_generator.running = False
|
| 376 |
+
return {"status": "stopping"}
|
| 377 |
+
return {"status": "not running"}
|
| 378 |
+
|
| 379 |
+
@app.get("/cookies")
|
| 380 |
+
async def get_cookies():
|
| 381 |
+
try:
|
| 382 |
+
if os.path.exists('cookies.txt'):
|
| 383 |
+
with open('cookies.txt', 'r') as file:
|
| 384 |
+
cookies = file.readlines()
|
| 385 |
+
return {"cookies": cookies}
|
| 386 |
+
return {"cookies": []}
|
| 387 |
+
except Exception as e:
|
| 388 |
+
return {"error": str(e)}
|
| 389 |
+
|
| 390 |
+
if __name__ == "__main__":
|
| 391 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|