This commit is contained in:
熊潇 2025-05-03 00:57:07 +08:00
parent ea09fc9b26
commit e7b645d795
5 changed files with 26 additions and 75 deletions

23
Dockerfile Normal file
View File

@ -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"]

View File

View File

@ -3,8 +3,9 @@ module.exports = {
apps: [ apps: [
{ {
name: 'xhs-api-server', name: 'xhs-api-server',
script: 'server.py', // 替换为您的Python脚本路径 script: 'app.py', // 替换为您的Python脚本路径
interpreter: 'python', // 替换为您的Python解释器路径 interpreter: 'python', // 替换为您的Python解释器路径
args: ['flask', 'run', '--port', PORT], // 传递端口参数
env: { env: {
XHS_API_PORT: PORT, // 从环境变量获取端口不存在则使用5005 XHS_API_PORT: PORT, // 从环境变量获取端口不存在则使用5005
}, },

View File

@ -2,8 +2,7 @@
"name": "@kevisual/social-xhs-api-server", "name": "@kevisual/social-xhs-api-server",
"type": "module", "type": "module",
"scripts": { "scripts": {
"pm2": "pm2 start ecosystem.config.mjs", "pm2": "pm2 start ecosystem.config.cjs",
"start": "pm2 start --name xhs-api-server --interpreter python3 --env XHS_API_PORT=5006 -- server.py ",
"py": "XHS_API_PORT=5006 python server.py" "py": "XHS_API_PORT=5006 python server.py"
} }
} }

View File

@ -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)