-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ pytest | |
pytest-cov | ||
twine | ||
build | ||
Flask | ||
playwright |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
FROM --platform=$TARGETPLATFORM mcr.microsoft.com/playwright/python:v1.38.0-jammy | ||
|
||
LABEL authors="ReaJason" | ||
LABEL mail="[email protected]" | ||
|
||
WORKDIR /app | ||
COPY app.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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## 多架构构建 | ||
|
||
> 参考:https://yeasy.gitbook.io/docker_practice/buildx/multi-arch-images | ||
|
||
```bash | ||
docker buildx create --name mybuilder --driver docker-container | ||
|
||
docker buildx use mybuilder | ||
|
||
docker buildx build --platform linux/arm64,linux/amd64 -t reajason/xhs-api . --push | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from flask import Flask, request | ||
from playwright.sync_api import sync_playwright | ||
from gevent import monkey | ||
import time | ||
|
||
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) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(host="0.0.0.0", port=5005) |