增加pppid参数
This commit is contained in:
parent
ef2b0e4e72
commit
7d27ff386c
19
main.py
19
main.py
|
|
@ -4,6 +4,8 @@ import asyncio
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
import re
|
||||||
|
from typing import Optional
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from fastapi import FastAPI, HTTPException, Request
|
from fastapi import FastAPI, HTTPException, Request
|
||||||
from playwright.async_api import async_playwright
|
from playwright.async_api import async_playwright
|
||||||
|
|
@ -60,7 +62,7 @@ async def log_request_time(request: Request, call_next):
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
async def get_fingerprint():
|
async def get_fingerprint(target_url: str = TARGET_PAGE):
|
||||||
page = await browser.new_page()
|
page = await browser.new_page()
|
||||||
try:
|
try:
|
||||||
# [新增] 拦截无用资源,极大提升加载速度
|
# [新增] 拦截无用资源,极大提升加载速度
|
||||||
|
|
@ -114,7 +116,7 @@ async def get_fingerprint():
|
||||||
page.on("request", handle_request)
|
page.on("request", handle_request)
|
||||||
|
|
||||||
# [修改] 并发执行 goto 和 wait_for。拿到结果就立刻返回,不再死等 goto 结束
|
# [修改] 并发执行 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)
|
fingerprint = await asyncio.wait_for(fingerprint_future, timeout=15)
|
||||||
return fingerprint
|
return fingerprint
|
||||||
|
|
@ -138,10 +140,19 @@ async def health():
|
||||||
return {"status": "healthy"}
|
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")
|
@app.get("/fingerprint")
|
||||||
async def get_fingerprint_endpoint():
|
async def get_fingerprint_endpoint(pppid: Optional[str] = None):
|
||||||
try:
|
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:
|
if fingerprint:
|
||||||
return {
|
return {
|
||||||
"status": "success",
|
"status": "success",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue