diff --git a/main.py b/main.py index 0796316..fc7b55d 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,8 @@ import asyncio import os import logging import time +import re +from typing import Optional from contextlib import asynccontextmanager from fastapi import FastAPI, HTTPException, Request from playwright.async_api import async_playwright @@ -60,7 +62,7 @@ async def log_request_time(request: Request, call_next): return response -async def get_fingerprint(): +async def get_fingerprint(target_url: str = TARGET_PAGE): page = await browser.new_page() try: # [新增] 拦截无用资源,极大提升加载速度 @@ -114,7 +116,7 @@ async def get_fingerprint(): page.on("request", handle_request) # [修改] 并发执行 goto 和 wait_for。拿到结果就立刻返回,不再死等 goto 结束 - goto_task = asyncio.create_task(page.goto(TARGET_PAGE, wait_until="domcontentloaded")) + goto_task = asyncio.create_task(page.goto(target_url, wait_until="domcontentloaded")) fingerprint = await asyncio.wait_for(fingerprint_future, timeout=15) return fingerprint @@ -138,10 +140,19 @@ async def health(): return {"status": "healthy"} +def replace_url_id(base_url, new_id): + return re.sub(r'id=\d+', f'id={new_id}', base_url) + + @app.get("/fingerprint") -async def get_fingerprint_endpoint(): +async def get_fingerprint_endpoint(pppid: Optional[str] = None): try: - fingerprint = await get_fingerprint() + target_url = TARGET_PAGE + if pppid: + target_url = replace_url_id(TARGET_PAGE, pppid) + logger.info(f"Using custom id: {pppid}, target URL: {target_url}") + + fingerprint = await get_fingerprint(target_url) if fingerprint: return { "status": "success",