From e7b645d7957851d2379b499df58106ec73a91b1a Mon Sep 17 00:00:00 2001 From: abearxiong Date: Sat, 3 May 2025 00:57:07 +0800 Subject: [PATCH] temp --- Dockerfile | 23 ++++++++++++++ server.py => app.py | 0 ecosystem.config.cjs | 3 +- package.json | 3 +- xha-api/app.py | 72 -------------------------------------------- 5 files changed, 26 insertions(+), 75 deletions(-) create mode 100644 Dockerfile rename server.py => app.py (100%) delete mode 100644 xha-api/app.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2058add --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM --platform=$TARGETPLATFORM mcr.microsoft.com/playwright/python:v1.38.0-jammy + +LABEL authors="abearxiong" +LABEL mail="xiongxiao@xiongxiao.me" + +WORKDIR /app +COPY server.py . + +RUN set -ex \ + && apt-get update \ + && apt-get install -y --no-install-recommends curl + + +# reference -> https://playwright.dev/python/docs/ci#via-containers +RUN python -m pip install --upgrade pip \ + && pip install Flask gevent xhs playwright \ + && rm -rf /var/lib/apt/lists/*ç + +RUN curl --insecure -L -o stealth.min.js https://cdn.jsdelivr.net/gh/requireCool/stealth.min.js/stealth.min.js + +EXPOSE 5005 + +CMD [ "python", "-m" , "flask", "run", "--host=0.0.0.0", "--port=5005"] \ No newline at end of file diff --git a/server.py b/app.py similarity index 100% rename from server.py rename to app.py diff --git a/ecosystem.config.cjs b/ecosystem.config.cjs index 63e4dc7..9f19124 100644 --- a/ecosystem.config.cjs +++ b/ecosystem.config.cjs @@ -3,8 +3,9 @@ module.exports = { apps: [ { name: 'xhs-api-server', - script: 'server.py', // 替换为您的Python脚本路径 + script: 'app.py', // 替换为您的Python脚本路径 interpreter: 'python', // 替换为您的Python解释器路径 + args: ['flask', 'run', '--port', PORT], // 传递端口参数 env: { XHS_API_PORT: PORT, // 从环境变量获取端口,不存在则使用5005 }, diff --git a/package.json b/package.json index 8c6ddec..2f906b8 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,7 @@ "name": "@kevisual/social-xhs-api-server", "type": "module", "scripts": { - "pm2": "pm2 start ecosystem.config.mjs", - "start": "pm2 start --name xhs-api-server --interpreter python3 --env XHS_API_PORT=5006 -- server.py ", + "pm2": "pm2 start ecosystem.config.cjs", "py": "XHS_API_PORT=5006 python server.py" } } \ No newline at end of file diff --git a/xha-api/app.py b/xha-api/app.py deleted file mode 100644 index b155ea2..0000000 --- a/xha-api/app.py +++ /dev/null @@ -1,72 +0,0 @@ -import time - -from flask import Flask, request -from gevent import monkey -from playwright.sync_api import sync_playwright - -monkey.patch_all() - -app = Flask(__name__) - -global_a1 = "" - - -def get_context_page(instance, stealth_js_path): - chromium = instance.chromium - browser = chromium.launch(headless=True) - context = browser.new_context() - context.add_init_script(path=stealth_js_path) - page = context.new_page() - return context, page - - -stealth_js_path = "stealth.min.js" -print("正在启动 playwright") -playwright = sync_playwright().start() -browser_context, context_page = get_context_page(playwright, stealth_js_path) -context_page.goto("https://www.xiaohongshu.com") -print("正在跳转至小红书首页") -time.sleep(5) -context_page.reload() -time.sleep(1) -cookies = browser_context.cookies() -for cookie in cookies: - if cookie["name"] == "a1": - global_a1 = cookie["value"] - print("当前浏览器中 a1 值为:" + global_a1 + ",请将您的 cookie 中的 a1 也设置成一样,方可签名成功") -print("跳转小红书首页成功,等待调用") - - -def sign(uri, data, a1, web_session): - global global_a1 - if a1 != global_a1: - browser_context.add_cookies([ - {'name': 'a1', 'value': a1, 'domain': ".xiaohongshu.com", 'path': "/"} - ]) - context_page.reload() - time.sleep(1) - global_a1 = a1 - encrypt_params = context_page.evaluate("([url, data]) => window._webmsxyw(url, data)", [uri, data]) - return { - "x-s": encrypt_params["X-s"], - "x-t": str(encrypt_params["X-t"]) - } - - -@app.route("/sign", methods=["POST"]) -def hello_world(): - json = request.json - uri = json["uri"] - data = json["data"] - a1 = json["a1"] - web_session = json["web_session"] - return sign(uri, data, a1, web_session) - - -@app.route("/a1", methods=["GET"]) -def get_a1(): - return {'a1': global_a1} - - -if __name__ == '__main__': - app.run(host="0.0.0.0", port=5005) \ No newline at end of file