Compare commits

..

13 Commits

Author SHA1 Message Date
3c6c9f9fbf fix: add every hour restart 2025-06-26 13:03:45 +08:00
4909143938 up 2025-05-04 15:19:27 +08:00
e591d2cb7c fix a1 is mani browser 2025-05-04 15:02:58 +08:00
ea8632bfd6 fix excepiton 2025-05-04 14:53:36 +08:00
8f0b81ebd9 fix 2025-05-03 18:01:54 +08:00
2718d24977 学习fastapi 2025-05-03 02:07:44 +08:00
774b843d27 fix bug 2025-05-03 01:57:00 +08:00
7f667f4dc2 fix port 2025-05-03 01:53:00 +08:00
f5561f4fa9 "fix: 支持环境变量配置端口并修正生态配置" 2025-05-03 01:50:52 +08:00
1141ddee3b fix 2025-05-03 01:42:55 +08:00
969413b142 fix eco 2025-05-03 01:37:15 +08:00
010985d990 fix fast async startup 2025-05-03 01:33:19 +08:00
9cacdc1532 fix 2025-05-03 01:18:29 +08:00
3 changed files with 92 additions and 29 deletions

99
app.py
View File

@@ -5,11 +5,20 @@ from playwright.async_api import async_playwright # 改用异步 API
from dotenv import load_dotenv from dotenv import load_dotenv
import os import os
from typing import Optional, Dict, Any from typing import Optional, Dict, Any
from contextlib import asynccontextmanager
# 加载环境变量 # 加载环境变量
load_dotenv() load_dotenv()
app = FastAPI() @asynccontextmanager
async def lifespan(app: FastAPI):
await initialize_playwright()
yield
# Clean up the ML models and release the resources
# await shutdown_event()
print("关闭")
app = FastAPI(lifespan=lifespan)
global_a1 = "" global_a1 = ""
# 确保在模块级别声明全局变量 # 确保在模块级别声明全局变量
@@ -52,14 +61,30 @@ async def initialize_playwright():
await loginModal.evaluate("el => el.click()") await loginModal.evaluate("el => el.click()")
print("登录弹窗已关闭") print("登录弹窗已关闭")
print("跳转小红书首页成功,等待调用") print("跳转小红书首页成功,等待调用")
async def close_playwright():
global playwright_instance, browser_context, context_page
if context_page:
await context_page.close()
if browser_context:
await browser_context.close()
if playwright_instance:
await playwright_instance.stop()
print("Playwright 已关闭")
async def show_cookies():
global browser_context, context_page, global_a1 # 声明全局变量
cookies = await browser_context.cookies()
for cookie in cookies:
if cookie["name"] == "a1":
global_a1 = cookie["value"]
print("当前浏览器 cookie 中 a1 值为:" + cookie["value"] + ",请将需要使用的 a1 设置成一样方可签名成功")
async def setCookie(a1: str) -> Dict[str, Any]: async def setCookie(a1: str) -> Dict[str, Any]:
global browser_context, context_page # 声明全局变量 global browser_context, context_page # 声明全局变量
global global_a1 global global_a1
try: try:
# 确保页面仍然有效,如果页面已关闭则重新初始化 # 确保页面仍然有效,如果页面已关闭则重新初始化
if context_page is None or await context_page.is_closed(): if context_page is None or context_page.is_closed():
browser_context, context_page = await get_context_page(playwright_instance, "stealth.min.js") browser_context, context_page = await get_context_page(playwright_instance, "stealth.min.js")
await context_page.goto("https://www.xiaohongshu.com") await context_page.goto("https://www.xiaohongshu.com")
await asyncio.sleep(5) await asyncio.sleep(5)
@@ -87,35 +112,66 @@ async def setCookie(a1: str) -> Dict[str, Any]:
print(f"Error during setCookie operation: {e}") print(f"Error during setCookie operation: {e}")
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))
async def reload_browser():
global browser_context, context_page # 声明全局变量
global playwright_instance
try:
await close_playwright()
# 重新初始化 Playwright 和浏览器上下文
await initialize_playwright()
# 重新加载页面
print("浏览器已重新加载")
await show_cookies()
except Exception as e:
print(f"Error during reload_browser operation: {e}")
raise HTTPException(status_code=500, detail=str(e))
async def sign(uri: str, data: Dict[str, Any], a1: str, web_session: str) -> Dict[str, Any]: async def sign(uri: str, data: Dict[str, Any], a1: str, web_session: str) -> Dict[str, Any]:
global browser_context, context_page # 声明全局变量 global browser_context, context_page # 声明全局变量
global global_a1 global global_a1
try: try:
# 确保页面仍然有效,如果页面已关闭则重新初始化 # 确保页面仍然有效,如果页面已关闭则重新初始化
if context_page is None or await context_page.is_closed(): if context_page is None or context_page.is_closed():
browser_context, context_page = await get_context_page(playwright_instance, "stealth.min.js") browser_context, context_page = await get_context_page(playwright_instance, "stealth.min.js")
await context_page.goto("https://www.xiaohongshu.com") await context_page.goto("https://www.xiaohongshu.com")
await asyncio.sleep(5) await asyncio.sleep(5)
await context_page.reload() await context_page.reload()
await asyncio.sleep(1) await asyncio.sleep(1)
if a1 != global_a1:
await setCookie(a1)
# 执行 JavaScript 函数 # 执行 JavaScript 函数
# localStorage.getItem("b1")
b1 = await context_page.evaluate("() => localStorage.getItem('b1')") b1 = await context_page.evaluate("() => localStorage.getItem('b1')")
b1b1 = await context_page.evaluate("() => localStorage.getItem('b1b1')") b1b1 = await context_page.evaluate("() => localStorage.getItem('b1b1')")
encrypt_params = await context_page.evaluate("([url, data]) => window._webmsxyw(url, data)", [uri, data]) encrypt_params = await context_page.evaluate("([url, data]) => window._webmsxyw(url, data)", [uri, data])
if not encrypt_params or not isinstance(encrypt_params, dict):
raise HTTPException(status_code=500, detail="Failed to retrieve encryption parameters")
return { return {
"x-s": encrypt_params["X-s"], "x-s": encrypt_params["X-s"],
"x-t": str(encrypt_params["X-t"]), "x-t": str(encrypt_params["X-t"]),
"b1": b1, "b1": b1,
"a1": global_a1,
"b1b1": b1b1, "b1b1": b1b1,
} }
except Exception as e: except Exception as e:
print(f"Error during sign operation: {e}") # 检测页面崩溃错误并重新初始化
raise HTTPException(status_code=500, detail=str(e)) if "Target crashed" in str(e):
print("页面崩溃,正在重新初始化浏览器上下文和页面...")
try:
await reload_browser()
# 重试签名操作
return await sign(uri, data, a1, web_session)
except Exception as reinit_error:
print(f"重新初始化失败: {reinit_error}")
raise HTTPException(status_code=500, detail="Failed to recover from page crash")
else:
print(f"Error during sign operation: {e}")
try:
await reload_browser()
# 重试签名操作
return await sign(uri, data, a1, web_session)
except Exception as reinit_error:
print(f"重新初始化失败: {reinit_error}")
raise HTTPException(status_code=500, detail="Failed to recover from page crash")
@app.post("/sign") @app.post("/sign")
@@ -125,10 +181,8 @@ async def sign_endpoint(request: Request):
data = json_data.get("data") data = json_data.get("data")
a1 = json_data.get("a1") a1 = json_data.get("a1")
web_session = json_data.get("web_session") web_session = json_data.get("web_session")
if not uri or not a1:
if not all([uri, data, a1, web_session]): raise HTTPException(status_code=400, detail="Missing required parameters")
raise HTTPException(status_code=400, detail="Missing required parameters")
me = await sign(uri, data, a1, web_session) me = await sign(uri, data, a1, web_session)
return { return {
"a1": a1, "a1": a1,
@@ -148,14 +202,7 @@ async def get_a1(a1: Optional[str] = None):
return {'a1': global_a1} return {'a1': global_a1}
# 在应用启动时初始化 Playwright
@app.on_event("startup")
async def startup_event():
await initialize_playwright()
# 在应用关闭时清理资源
@app.on_event("shutdown")
async def shutdown_event(): async def shutdown_event():
global playwright_instance, browser_context, context_page global playwright_instance, browser_context, context_page
if context_page: if context_page:
@@ -168,4 +215,12 @@ async def shutdown_event():
if __name__ == '__main__': if __name__ == '__main__':
import uvicorn import uvicorn
uvicorn.run(app, host="0.0.0.0", port=5005) port = os.getenv("XHS_API_PORT", 5005)
# 不是数字的话转为 int
try:
port = int(port)
except ValueError:
print(f"Invalid port number: {port}. Using default port 5005.")
port = 5005
# 启动 FastAPI 应用
uvicorn.run(app, host="0.0.0.0", port=port)

View File

@@ -1,11 +1,18 @@
const PORT = 5006; const PORT = 5006;
// python -m uvicorn app:app --host 0.0.0.0 --port 5006 // python -m uvicorn app:app --host 0.0.0.0 --port 5006{
module.exports = { module.exports = {
apps: [ apps: [
{ {
name: 'xhs-api-server', name: 'xhs-api-server',
script: 'app.py', // 替换为您的Python脚本路径
interpreter: 'python', // 替换为您的Python解释器路径 interpreter: 'python', // 替换为您的Python解释器路径
args: ['-m uvicorn app:app --host 0.0.0.0 --port', PORT], // 传递端口参数 instances: 1, // 启动一个实例
autorestart: true, // 自动重启
cron_restart: '0 * * * *', // 每小时重启一次
env: {
XHS_API_PORT: PORT,
NODE_ENV: 'production',
},
}, },
], ],
}; };

View File

@@ -1,5 +1,6 @@
playwright dotenv==0.9.9
xhs fastapi==0.115.12
fastapi playwright==1.52.0
dotenv requests==2.32.3
uvicorn uvicorn==0.34.2
xhs==0.2.13