### Prepare development environment Source: https://github.com/cloxl/xhshow/blob/master/README.md Commands to install dependencies and set up the project. ```bash # 安装uv包管理器 curl -LsSf https://astral.sh/uv/install.sh | sh # 克隆项目 git clone https://github.com/Cloxl/xhshow cd xhshow # 安装依赖 uv sync --dev ``` -------------------------------- ### Generate GET Request Signature Headers Source: https://context7.com/cloxl/xhshow/llms.txt Generates all necessary signature headers for a GET request, including x-s, x-s-common, x-t, x-b3-traceid, and x-xray-traceid. This is the recommended method for GET requests. Ensure correct URI, cookies, and parameters are provided. ```python from xhshow import Xhshow import requests client = Xhshow() # 准备 cookies(支持字典或字符串格式) cookies = { "a1": "your_a1_value", "web_session": "your_web_session", "webId": "your_web_id" } # 生成 GET 请求签名头 headers = client.sign_headers_get( uri="https://edith.xiaohongshu.com/api/sns/web/v1/user_posted", cookies=cookies, params={"num": "30", "cursor": "", "user_id": "123"} ) # 返回的 headers 包含: # { # "x-s": "XYS_...", # "x-s-common": "...", # "x-t": "1234567890", # "x-b3-traceid": "...", # "x-xray-traceid": "..." # } # 发送请求 base_headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/142.0.0.0", "Content-Type": "application/json" } base_headers.update(headers) response = requests.get( "https://edith.xiaohongshu.com/api/sns/web/v1/user_posted", params={"num": "30", "cursor": "", "user_id": "123"}, headers=base_headers, cookies=cookies ) print(response.json()) ``` -------------------------------- ### sign_headers_get - GET Request Signing Source: https://context7.com/cloxl/xhshow/llms.txt Generates all required signature headers for a GET request, including x-s, x-s-common, x-t, x-b3-traceid, and x-xray-traceid. ```APIDOC ## GET sign_headers_get ### Description Generates the complete set of signature headers required for a GET request to the Xiaohongshu API. ### Method GET ### Parameters #### Request Body - **uri** (string) - Required - The full request URI. - **cookies** (dict/string) - Required - The user session cookies (a1, web_session, webId). - **params** (dict) - Optional - The query parameters for the request. ### Response #### Success Response (200) - **headers** (dict) - A dictionary containing the generated signature headers (x-s, x-s-common, x-t, x-b3-traceid, x-xray-traceid). ``` -------------------------------- ### Generate x-s Signature for GET Request Source: https://context7.com/cloxl/xhshow/llms.txt Generates only the x-s signature component for a GET request. This is useful for fine-grained control over signature generation or when only the x-s value is needed. Requires the URI, the 'a1' cookie value, and request parameters. ```python from xhshow import Xhshow client = Xhshow() # 仅生成 x-s 签名(只需要 a1 值) x_s = client.sign_xs_get( uri="https://edith.xiaohongshu.com/api/sns/web/v1/user_posted", a1_value="your_a1_cookie_value", params={"num": "30", "cursor": "", "user_id": "123"} ) print(x_s) # 输出: XYS_2UQhPsHCH0c1Pjh9HjIj2erjwjQhyoPT... ``` -------------------------------- ### build_url - Construct Request URL Source: https://context7.com/cloxl/xhshow/llms.txt Builds a GET request URL compliant with Xiaohongshu platform specifications, using platform-specific parameter encoding rules. ```APIDOC ## build_url ### Description Constructs a GET request URL that conforms to the Xiaohongshu platform's specifications, employing platform-specific parameter encoding rules. ### Method `client.build_url(base_url: str, params: dict) -> str` ### Parameters #### Query Parameters - **base_url** (str) - Required - The base URL for the request. - **params** (dict) - Required - A dictionary of query parameters. Values can be strings, lists, or other types that can be stringified. Special characters in values will be URL-encoded. ### Request Example ```python from xhshow import Xhshow client = Xhshow() # Basic usage url = client.build_url( base_url="https://edith.xiaohongshu.com/api/sns/web/v1/user_posted", params={"num": "30", "cursor": "", "user_id": "123"} ) print(url) # Output: https://edith.xiaohongshu.com/api/sns/web/v1/user_posted?num=30&cursor=&user_id=123 # Handling special characters url = client.build_url("/api/path", {"key": "value=test"}) print(url) # Output: /api/path?key=value%3Dtest # Handling list parameters url = client.build_url("/api/path", {"tags": ["tech", "python"]}) print(url) # Output: /api/path?tags=tech,python ``` ### Response #### Success Response (200) - **url** (str) - The constructed URL with properly encoded query parameters. ``` -------------------------------- ### Build Request URL Source: https://context7.com/cloxl/xhshow/llms.txt Constructs a GET request URL adhering to platform specifications, including custom parameter encoding. Handles special characters and list parameters. ```python from xhshow import Xhshow client = Xhshow() # 基本用法 url = client.build_url( base_url="https://edith.xiaohongshu.com/api/sns/web/v1/user_posted", params={"num": "30", "cursor": "", "user_id": "123"} ) print(url) # 处理特殊字符 url = client.build_url("/api/path", {"key": "value=test"}) print(url) # 处理列表参数 url = client.build_url("/api/path", {"tags": ["tech", "python"]}) print(url) ``` -------------------------------- ### Generate Signature Headers with Generic Method Source: https://context7.com/cloxl/xhshow/llms.txt A versatile method to generate signature headers by explicitly specifying the request method (GET or POST). Useful when the request method is determined dynamically. Requires URI, cookies, and either parameters or a payload. ```python from xhshow import Xhshow client = Xhshow() cookies = {"a1": "your_a1_value", "web_session": "...", "webId": "..."} # GET 请求 headers_get = client.sign_headers( method="GET", uri="/api/sns/web/v1/user_posted", cookies=cookies, params={"num": "30"} ) # POST 请求 headers_post = client.sign_headers( method="POST", uri="/api/sns/web/v1/login", cookies=cookies, payload={"username": "test", "password": "123456"} ) print(headers_get.keys()) # dict_keys(['x-s', 'x-s-common', 'x-t', 'x-b3-traceid', 'x-xray-traceid']) ``` -------------------------------- ### Generate POST Request Signature Headers Source: https://context7.com/cloxl/xhshow/llms.txt Generates all necessary signature headers for a POST request, similar to the GET method. This is suitable for actions like login or content posting that require POST requests. Ensure correct URI, cookies, and payload are provided. ```python from xhshow import Xhshow import requests client = Xhshow() cookies = { "a1": "your_a1_value", "web_session": "your_web_session", "webId": "your_web_id" } # 生成 POST 请求签名头 headers = client.sign_headers_post( uri="https://edith.xiaohongshu.com/api/sns/web/v1/login", cookies=cookies, payload={"username": "test", "password": "123456"} ) # 发送 POST 请求 base_headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/142.0.0.0", "Content-Type": "application/json" } base_headers.update(headers) response = requests.post( "https://edith.xiaohongshu.com/api/sns/web/v1/login", json={"username": "test", "password": "123456"}, headers=base_headers, cookies=cookies ) print(response.json()) ``` -------------------------------- ### Get X-T with Timestamp Source: https://context7.com/cloxl/xhshow/llms.txt Retrieves the 'x_t' value using a specified timestamp in seconds. Ensure the timestamp is provided in the correct format. ```python x_t = client.get_x_t(timestamp=1764896636.081) print(x_t) ``` -------------------------------- ### sign_xs_get / sign_xs_post - Individual x-s Generation Source: https://context7.com/cloxl/xhshow/llms.txt Generates only the x-s signature field for GET or POST requests, useful for granular control. ```APIDOC ## POST/GET sign_xs_* ### Description Generates the x-s signature field specifically. Requires the a1 cookie value. ### Parameters #### Request Body - **uri** (string) - Required - The request URI. - **a1_value** (string) - Required - The value of the 'a1' cookie. - **params/payload** (dict) - Required - The request parameters or body depending on the method. ### Response #### Success Response (200) - **x-s** (string) - The generated x-s signature string. ``` -------------------------------- ### get_x_t - Get Timestamp Source: https://context7.com/cloxl/xhshow/llms.txt Generates a timestamp value, optionally using a provided timestamp. ```APIDOC ## get_x_t ### Description Generates a timestamp value. If a timestamp is provided, it uses that; otherwise, it uses the current time. ### Method `client.get_x_t(timestamp: float = None) -> float` ### Parameters #### Query Parameters - **timestamp** (float) - Optional - The timestamp in seconds to use. If not provided, the current time is used. ### Request Example ```python from xhshow import Xhshow client = Xhshow() # Using a specified timestamp x_t = client.get_x_t(timestamp=1764896636.081) print(x_t) # Output: 1764896636081 # Using the current timestamp x_t_current = client.get_x_t() print(x_t_current) ``` ### Response #### Success Response (200) - **x_t** (float) - The generated timestamp value. ``` -------------------------------- ### Get Timestamp for x-t Header Source: https://context7.com/cloxl/xhshow/llms.txt Generates the value for the x-t header, which is a millisecond-precision Unix timestamp. Allows for specifying a custom timestamp to ensure signature consistency if needed. ```python from xhshow import Xhshow client = Xhshow() # 使用当前时间 x_t = client.get_x_t() print(x_t) # 输出: 1764902784843 ``` -------------------------------- ### Initialize Xhshow Client Source: https://context7.com/cloxl/xhshow/llms.txt Instantiate the Xhshow client with default or custom cryptographic configurations. Custom configurations allow modification of parameters like X3_PREFIX and SEQUENCE_VALUE range. ```python from xhshow import Xhshow, CryptoConfig # 使用默认配置创建客户端 client = Xhshow() # 使用自定义配置创建客户端 custom_config = CryptoConfig().with_overrides( X3_PREFIX="custom_", SEQUENCE_VALUE_MIN=20, SEQUENCE_VALUE_MAX=60 ) client_custom = Xhshow(config=custom_config) ``` -------------------------------- ### Run development tasks Source: https://github.com/cloxl/xhshow/blob/master/README.md Commands for testing, linting, formatting, and building the package. ```bash # 运行测试 uv run pytest tests/ -v # 代码检查 uv run ruff check src/ tests/ --ignore=UP036,E501 # 代码格式化 uv run ruff format src/ tests/ # 构建包 uv build ``` -------------------------------- ### Sign requests with Cookie string Source: https://github.com/cloxl/xhshow/blob/master/README.md Demonstrates how to sign requests using a cookie string format. ```python # 方式3: 支持 Cookie 字符串格式 cookie_string = "a1=your_a1_value; web_session=your_web_session; webId=your_web_id" xs_common = client.sign_xsc(cookie_dict=cookie_string) # 使用在请求中 headers = { "x-s-common": xs_common, # ... 其他 headers } ``` -------------------------------- ### Custom Configuration Source: https://github.com/cloxl/xhshow/blob/master/README.md Configuring the cryptographic behavior of the Xhshow client. ```APIDOC ## CryptoConfig ### Description Allows overriding default cryptographic settings such as prefixes, templates, and sequence ranges. ### Request Example custom_config = CryptoConfig().with_overrides(X3_PREFIX="custom_", SEQUENCE_VALUE_MIN=20) ``` -------------------------------- ### Configure custom crypto settings Source: https://github.com/cloxl/xhshow/blob/master/README.md Customize signature templates and sequence ranges using CryptoConfig. ```python from xhshow import CryptoConfig, Xhshow custom_config = CryptoConfig().with_overrides( X3_PREFIX="custom_", SIGNATURE_DATA_TEMPLATE={"x0": "4.2.6", "x1": "xhs-pc-web", "x2": "Windows", "x3": "", "x4": ""}, SEQUENCE_VALUE_MIN=20, SEQUENCE_VALUE_MAX=60 ) client = Xhshow(config=custom_config) ``` -------------------------------- ### Manage Git workflow Source: https://github.com/cloxl/xhshow/blob/master/README.md Standard commands for creating features and committing code. ```bash # 创建功能分支 git checkout -b feat/your-feature # 提交代码(遵循conventional commits规范) git commit -m "feat(client): 添加新功能描述" # 推送到远程 git push origin feat/your-feature ``` -------------------------------- ### Manage multiple account sessions Source: https://github.com/cloxl/xhshow/blob/master/README.md Create independent SessionManager instances for each account when handling multiple accounts. ```python accounts = [ {"a1": "account1_a1", "web_session": "session1"}, {"a1": "account2_a1", "web_session": "session2"}, ] # 为每个账户创建独立的 session sessions = {} for account in accounts: sessions[account["a1"]] = SessionManager() # 使用时匹配账户和对应的 session for account in accounts: headers = client.sign_headers_get( uri="/api/sns/web/v1/user_posted", cookies=account, params={"num": "30"}, session=sessions[account["a1"]] # 使用对应账户的 session ) ``` -------------------------------- ### CryptoConfig - Custom Configuration Source: https://context7.com/cloxl/xhshow/llms.txt Allows customization of signature generation parameters, including prefixes, random value ranges, and signature templates, via the `CryptoConfig` class. ```APIDOC ## CryptoConfig ### Description Provides a way to customize various parameters used in signature generation, such as prefixes, the range for random values, and the template for the signature data. This is achieved using the `CryptoConfig` class. ### Method `Xhshow(config: CryptoConfig = None)` ### Parameters #### Constructor Parameters - **config** (CryptoConfig) - Optional - A `CryptoConfig` object with custom settings. If not provided, default configurations are used. ### `CryptoConfig` Customization Methods - **`with_overrides(**kwargs)`**: Allows overriding specific configuration values. - `X3_PREFIX` (str): Custom prefix for x3. - `XYS_PREFIX` (str): Custom prefix for XYS. - `SIGNATURE_DATA_TEMPLATE` (dict): A template dictionary for signature data. - `SEQUENCE_VALUE_MIN` (int): Minimum value for the sequence number. - `SEQUENCE_VALUE_MAX` (int): Maximum value for the sequence number. - `WINDOW_PROPS_LENGTH_MIN` (int): Minimum length for window properties. - `WINDOW_PROPS_LENGTH_MAX` (int): Maximum length for window properties. ### Request Example ```python from xhshow import CryptoConfig, Xhshow # Create a custom configuration custom_config = CryptoConfig().with_overrides( X3_PREFIX="custom_", XYS_PREFIX="CUSTOM_", SIGNATURE_DATA_TEMPLATE={ "x0": "4.2.6", "x1": "xhs-pc-web", "x2": "Windows", "x3": "", "x4": "" }, SEQUENCE_VALUE_MIN=20, SEQUENCE_VALUE_MAX=60, WINDOW_PROPS_LENGTH_MIN=1100, WINDOW_PROPS_LENGTH_MAX=1300 ) # Create a client using the custom configuration client = Xhshow(config=custom_config) # Signatures will now use the custom configuration cookies = {"a1": "your_a1", "web_session": "...", "webId": "..."} headers = client.sign_headers_get( uri="/api/sns/web/v1/user_posted", cookies=cookies, params={"num": "30"} ) ``` ``` -------------------------------- ### Request Signing and Session Management Source: https://github.com/cloxl/xhshow/blob/master/README.md Methods for generating request headers with signatures and managing stateful sessions for multiple accounts. ```APIDOC ## sign_headers_get ### Description Generates signed headers for GET requests, optionally using a SessionManager to maintain state. ### Parameters #### Path Parameters - **uri** (string) - Required - The request URI or full URL. #### Query Parameters - **cookies** (dict/string) - Required - Cookie dictionary or string containing a1, web_session, and webId. - **params** (dict) - Optional - GET request parameters. - **session** (SessionManager) - Optional - Session manager instance for stateful signing. ### Request Example client.sign_headers_get(uri="/api/sns/web/v1/user_posted", cookies=cookies, params={"num": "30"}, session=session) ``` -------------------------------- ### Multi-Account Session Management Source: https://context7.com/cloxl/xhshow/llms.txt Enables account pool management by creating independent SessionManager instances for multiple accounts. Ensures distinct session states per account. ```python from xhshow import Xhshow, SessionManager client = Xhshow() accounts = [ {"a1": "account1_a1", "web_session": "session1", "webId": "id1"}, {"a1": "account2_a1", "web_session": "session2", "webId": "id2"}, ] # 为每个账户创建独立的 session sessions = {} for account in accounts: sessions[account["a1"]] = SessionManager() # 使用时匹配账户和对应的 session for account in accounts: headers = client.sign_headers_get( uri="/api/sns/web/v1/user_posted", cookies=account, params={"num": "30"}, session=sessions[account["a1"]] # 使用对应账户的 session ) print(f"Account {account['a1']}: Generated headers") ``` -------------------------------- ### Manage user sessions Source: https://github.com/cloxl/xhshow/blob/master/README.md Use SessionManager to maintain stateful signature parameters for long-running tasks. This is an experimental feature. ```python from xhshow import Xhshow, SessionManager import requests client = Xhshow() session = SessionManager() # 创建会话管理器 cookies = {"a1": "...", "web_session": "...", "webId": "..."} # 使用 session 参数进行签名 headers = client.sign_headers_get( uri="/api/sns/web/v1/user_posted", cookies=cookies, params={"num": "30"}, session=session # 传入 session ) response = requests.get( "https://edith.xiaohongshu.com/api/sns/web/v1/user_posted", params={"num": "30"}, headers=headers, cookies=cookies ) # 同一个 session 可以在多次请求中复用 headers2 = client.sign_headers_get( uri="/api/sns/web/v1/homefeed", cookies=cookies, params={"page": "1"}, session=session # 复用同一个 session ) ``` -------------------------------- ### Using a Unified Timestamp Source: https://context7.com/cloxl/xhshow/llms.txt Ensures all signature fields use the same timestamp by manually passing a unified 'timestamp' parameter when required. ```python from xhshow import Xhshow import time client = Xhshow() ``` -------------------------------- ### Build POST JSON Body Source: https://context7.com/cloxl/xhshow/llms.txt Creates a JSON body for POST requests, formatted compactly and preserving Unicode characters as per platform standards. ```python from xhshow import Xhshow client = Xhshow() json_body = client.build_json_body( payload={"username": "test", "password": "123456", "name": "测试用户"} ) print(json_body) ``` -------------------------------- ### Custom CryptoConfig for Signatures Source: https://context7.com/cloxl/xhshow/llms.txt Allows customization of signature generation parameters, including prefixes, random value ranges, and signature templates, via the CryptoConfig class. ```python from xhshow import CryptoConfig, Xhshow # 创建自定义配置 custom_config = CryptoConfig().with_overrides( X3_PREFIX="custom_", XYS_PREFIX="CUSTOM_", SIGNATURE_DATA_TEMPLATE={ "x0": "4.2.6", "x1": "xhs-pc-web", "x2": "Windows", "x3": "", "x4": "" }, SEQUENCE_VALUE_MIN=20, SEQUENCE_VALUE_MAX=60, WINDOW_PROPS_LENGTH_MIN=1100, WINDOW_PROPS_LENGTH_MAX=1300 ) # 使用自定义配置创建客户端 client = Xhshow(config=custom_config) # 签名将使用自定义配置 cookies = {"a1": "your_a1", "web_session": "...", "webId": "..."} headers = client.sign_headers_get( uri="/api/sns/web/v1/user_posted", cookies=cookies, params={"num": "30"} ) ``` -------------------------------- ### Signature Decoding Source: https://github.com/cloxl/xhshow/blob/master/README.md Utility methods to decode x3 and XYS signatures. ```APIDOC ## decode_x3 / decode_xs ### Description Decodes signature strings into readable data. ### Parameters - **signature** (string) - Required - The signature string to decode. ### Request Example client.decode_x3("mns0101_Q2vPHtH...") ``` -------------------------------- ### Multi-Account Session Management Source: https://context7.com/cloxl/xhshow/llms.txt Enables managing multiple accounts by creating independent `SessionManager` instances for each, facilitating account pooling. ```APIDOC ## Multi-Account Session Management ### Description Allows for the management of multiple user accounts by creating separate `SessionManager` instances for each account, enabling effective account pooling. ### Usage Create a `SessionManager` for each account and use the appropriate session when signing headers. ### Request Example ```python from xhshow import Xhshow, SessionManager client = Xhshow() accounts = [ {"a1": "account1_a1", "web_session": "session1", "webId": "id1"}, {"a1": "account2_a1", "web_session": "session2", "webId": "id2"}, ] # Create independent sessions for each account sessions = {} for account in accounts: sessions[account["a1"]] = SessionManager() # Use the corresponding session when signing headers for account in accounts: headers = client.sign_headers_get( uri="/api/sns/web/v1/user_posted", cookies=account, params={"num": "30"}, session=sessions[account["a1"]] # Use the session for the specific account ) print(f"Account {account['a1']}: Generated headers") ``` ``` -------------------------------- ### sign_headers_post - POST Request Signing Source: https://context7.com/cloxl/xhshow/llms.txt Generates all required signature headers for a POST request, suitable for actions like login or content submission. ```APIDOC ## POST sign_headers_post ### Description Generates the complete set of signature headers required for a POST request to the Xiaohongshu API. ### Method POST ### Parameters #### Request Body - **uri** (string) - Required - The full request URI. - **cookies** (dict/string) - Required - The user session cookies. - **payload** (dict) - Required - The JSON body of the POST request. ### Response #### Success Response (200) - **headers** (dict) - A dictionary containing the generated signature headers. ``` -------------------------------- ### SessionManager - Session Management Source: https://context7.com/cloxl/xhshow/llms.txt A session manager for simulating real user sessions, maintaining page load timestamps and incrementing counters for long-term stability. This is an experimental feature. ```APIDOC ## SessionManager ### Description Manages the state of a user session, simulating real user behavior by maintaining page load timestamps and incrementing counters. This can improve the stability of long-running operations. This is an experimental feature. ### Usage Instantiate `SessionManager` and pass the instance to methods that require session context, such as `sign_headers_get`. ### Request Example ```python from xhshow import Xhshow, SessionManager import requests client = Xhshow() session = SessionManager() # Create a session manager cookies = {"a1": "...", "web_session": "...", "webId": "..."} # Signing headers using the session headers = client.sign_headers_get( uri="/api/sns/web/v1/user_posted", cookies=cookies, params={"num": "30"}, session=session # Pass the session instance ) response = requests.get( "https://edith.xiaohongshu.com/api/sns/web/v1/user_posted", params={"num": "30"}, headers=headers, cookies=cookies ) # The same session can be reused across multiple requests headers2 = client.sign_headers_get( uri="/api/sns/web/v1/homefeed", cookies=cookies, params={"page": "1"}, session=session # Reuse the same session ) ``` ``` -------------------------------- ### Using a Unified Timestamp Source: https://context7.com/cloxl/xhshow/llms.txt Ensures all signature fields use the same timestamp by manually passing a unified `timestamp` parameter when needed. ```APIDOC ## Using a Unified Timestamp ### Description In scenarios where it's necessary to ensure that all signature fields utilize the exact same timestamp, you can achieve this by manually providing a unified `timestamp` parameter to relevant functions. ### Usage Pass a consistent `timestamp` value to functions that generate or use timestamps for signature creation. ### Request Example ```python from xhshow import Xhshow import time client = Xhshow() # Get a consistent timestamp current_timestamp = time.time() # Use this timestamp for operations requiring it, e.g., when signing headers # Example (assuming sign_headers_get or similar function accepts timestamp): # headers = client.sign_headers_get(..., timestamp=current_timestamp) # Or when generating other time-dependent values: # x_t = client.get_x_t(timestamp=current_timestamp) # xray_id = client.get_xray_trace_id(timestamp=int(current_timestamp * 1000)) print(f"Using unified timestamp: {current_timestamp}") ``` ``` -------------------------------- ### build_json_body - Construct POST Request Body Source: https://context7.com/cloxl/xhshow/llms.txt Builds a POST request JSON body compliant with Xiaohongshu platform specifications, using compact format and preserving Unicode characters. ```APIDOC ## build_json_body ### Description Constructs a POST request JSON body that conforms to the Xiaohongshu platform's specifications. It uses a compact format and preserves Unicode characters. ### Method `client.build_json_body(payload: dict) -> str` ### Parameters #### Request Body - **payload** (dict) - Required - The dictionary containing the data for the request body. ### Request Example ```python from xhshow import Xhshow client = Xhshow() json_body = client.build_json_body( payload={"username": "test", "password": "123456", "name": "测试用户"} ) print(json_body) # Output: {"username":"test","password":"123456","name":"测试用户"} ``` ### Response #### Success Response (200) - **json_body** (str) - A JSON string representing the request body, formatted compactly and with Unicode characters preserved. ``` -------------------------------- ### Generate API Request Headers with Unified Timestamp Source: https://context7.com/cloxl/xhshow/llms.txt Uses a unified timestamp to generate consistent signature headers for Xiaohongshu API requests. ```python timestamp = time.time() x_s = client.sign_xs_get( uri="/api/sns/web/v1/user_posted", a1_value="your_a1_cookie_value", params={"num": "30"}, timestamp=timestamp # 传入统一时间戳 ) x_t = client.get_x_t(timestamp=timestamp) x_xray_traceid = client.get_xray_trace_id(timestamp=int(timestamp * 1000)) headers = { "x-s": x_s, "x-t": str(x_t), "x-b3-traceid": client.get_b3_trace_id(), "x-xray-traceid": x_xray_traceid } print(headers) ``` -------------------------------- ### Generate x-s Signature for POST Request Source: https://context7.com/cloxl/xhshow/llms.txt Generates only the x-s signature component for a POST request. Similar to `sign_xs_get`, but tailored for POST requests. Requires the URI, the 'a1' cookie value, and the request payload. ```python from xhshow import Xhshow client = Xhshow() # 仅生成 x-s 签名(只需要 a1 值) x_s = client.sign_xs_post( uri="https://edith.xiaohongshu.com/api/sns/web/v1/login", a1_value="your_a1_cookie_value", payload={"username": "test", "password": "123456"} ) print(x_s) # 输出: XYS_2UQhPsHCH0c1Pjh9HjIj2erjwjQhyoPT... ``` -------------------------------- ### decode_x3 - Decode x3 Signature Source: https://context7.com/cloxl/xhshow/llms.txt Decodes an x3 signature (Base64 format) and returns the raw byte array. Useful for in-depth analysis of the signature's binary content. ```APIDOC ## decode_x3 ### Description Decodes an x3 signature, which is provided in Base64 format, and returns the original byte array. This is useful for performing a deep analysis of the binary content of the signature. ### Method `client.decode_x3(x3_signature: str) -> bytearray` ### Parameters #### Path Parameters - **x3_signature** (str) - Required - The Base64 encoded x3 signature string. ### Request Example ```python from xhshow import Xhshow client = Xhshow() # Decode the x3 signature decoded_bytes = client.decode_x3("mns0301_Q2vPHtH+lQJYGQfhxG271BIvFFhx...") print(decoded_bytes) # Output: bytearray([121, 104, 96, 41, ...]) ``` ### Response #### Success Response (200) - **decoded_bytes** (bytearray) - The raw byte array representation of the decoded x3 signature. ``` -------------------------------- ### Decrypt signatures Source: https://github.com/cloxl/xhshow/blob/master/README.md Decode x3 and XYS signatures using the client methods. ```python # 解密 x3 签名 decoded_bytes = client.decode_x3("mns0101_Q2vPHtH+lQJYGQfhxG271BIvFFhx...") # 解密完整的 XYS 签名 decoded_data = client.decode_xs("XYS_2UQhPsHCH0c1Pjh9HjIj2erjwjQhyoPT...") ``` -------------------------------- ### Generate B3 Trace ID Source: https://context7.com/cloxl/xhshow/llms.txt Generates a 16-character hexadecimal string for the x-b3-traceid header. No additional parameters are required. ```python from xhshow import Xhshow client = Xhshow() trace_id = client.get_b3_trace_id() print(trace_id) ``` -------------------------------- ### Generate x-s-common Signature Source: https://context7.com/cloxl/xhshow/llms.txt Generates the x-s-common signature component. This method accepts cookies in either dictionary or string format. It can be called directly using `sign_xsc` or its full method name `sign_xs_common`. ```python from xhshow import Xhshow client = Xhshow() # 方式1: 使用字典格式的 cookies cookies_dict = { "a1": "your_a1_value", "web_session": "your_web_session", "webId": "your_web_id" } xs_common = client.sign_xsc(cookie_dict=cookies_dict) # 方式2: 使用字符串格式的 cookies cookie_string = "a1=your_a1_value; web_session=your_web_session; webId=your_web_id" xs_common = client.sign_xsc(cookie_dict=cookie_string) # 方式3: 使用完整方法名 xs_common = client.sign_xs_common(cookie_dict=cookies_dict) print(xs_common) ``` -------------------------------- ### decode_xs - Decode XYS Signature Source: https://context7.com/cloxl/xhshow/llms.txt Decodes a complete XYS signature, returning a dictionary containing `x0`, `x1`, `x2`, `x3`, and `x4` fields. Useful for debugging and signature analysis. ```APIDOC ## decode_xs ### Description Decodes a full XYS signature and returns a dictionary containing the components: `x0`, `x1`, `x2`, `x3`, and `x4`. This function is useful for debugging and analyzing the structure of the signature. ### Method `client.decode_xs(signature: str) -> dict` ### Parameters #### Path Parameters - **signature** (str) - Required - The complete XYS signature string to decode. ### Request Example ```python from xhshow import Xhshow client = Xhshow() # Decode a full signature decoded_data = client.decode_xs("XYS_2UQhPsHCH0c1Pjh9HjIj2erjwjQhyoPT...") print(decoded_data) # Output: {'x0': '4.2.6', 'x1': 'xhs-pc-web', 'x2': 'Windows', 'x3': '...', 'x4': ''} ``` ### Response #### Success Response (200) - **decoded_data** (dict) - A dictionary containing the decoded signature components (`x0`, `x1`, `x2`, `x3`, `x4`). ``` -------------------------------- ### sign_xsc - x-s-common Generation Source: https://context7.com/cloxl/xhshow/llms.txt Generates the x-s-common signature field based on provided session cookies. ```APIDOC ## POST sign_xsc ### Description Generates the x-s-common signature field using session cookies. ### Parameters #### Request Body - **cookie_dict** (dict/string) - Required - The session cookies as a dictionary or string. ### Response #### Success Response (200) - **x-s-common** (string) - The generated x-s-common signature string. ``` -------------------------------- ### Generate XRay Trace ID Source: https://context7.com/cloxl/xhshow/llms.txt Generates a 32-character hexadecimal string for the x-xray-traceid header. Optionally accepts a timestamp and sequence number. ```python from xhshow import Xhshow client = Xhshow() # 使用默认参数 xray_id = client.get_xray_trace_id() print(xray_id) # 使用指定参数 xray_id = client.get_xray_trace_id(timestamp=1764896636081, seq=5) print(xray_id) ``` -------------------------------- ### Decode X3 Signature Source: https://context7.com/cloxl/xhshow/llms.txt Decrypts an x3 signature (Base64 format) into its raw byte array representation, allowing for in-depth analysis of its binary content. ```python from xhshow import Xhshow client = Xhshow() # 解密 x3 签名 decoded_bytes = client.decode_x3("mns0301_Q2vPHtH+lQJYGQfhxG271BIvFFhx...") print(decoded_bytes) ``` -------------------------------- ### get_b3_trace_id - Generate B3 Trace ID Source: https://context7.com/cloxl/xhshow/llms.txt Generates a B3 trace ID, which is a 16-character hexadecimal string. ```APIDOC ## get_b3_trace_id ### Description Generates a B3 trace ID, which is a 16-character hexadecimal string used for distributed tracing. ### Method `client.get_b3_trace_id() -> str` ### Endpoint N/A (This is a utility function, not an endpoint) ### Request Example ```python from xhshow import Xhshow client = Xhshow() trace_id = client.get_b3_trace_id() print(trace_id) # Output: 63cd207ddeb2e360 ``` ### Response #### Success Response (200) - **trace_id** (str) - A 16-character hexadecimal string representing the B3 trace ID. ``` -------------------------------- ### Decode XYS Signature Source: https://context7.com/cloxl/xhshow/llms.txt Decrypts a full XYS signature, returning a dictionary containing 'x0', 'x1', 'x2', 'x3', and 'x4' fields. Useful for debugging and signature analysis. ```python from xhshow import Xhshow client = Xhshow() # 解密完整签名 decoded_data = client.decode_xs("XYS_2UQhPsHCH0c1Pjh9HjIj2erjwjQhyoPT...") print(decoded_data) ``` -------------------------------- ### get_xray_trace_id - Generate XRay Trace ID Source: https://context7.com/cloxl/xhshow/llms.txt Generates an XRay trace ID, a 32-character hexadecimal string. Supports custom timestamps and sequence numbers. ```APIDOC ## get_xray_trace_id ### Description Generates an XRay trace ID, which is a 32-character hexadecimal string. You can specify a timestamp and a sequence number. ### Method `client.get_xray_trace_id(timestamp: int = None, seq: int = None) -> str` ### Parameters #### Query Parameters - **timestamp** (int) - Optional - The timestamp in milliseconds to use. If not provided, the current time is used. - **seq** (int) - Optional - The sequence number to use. If not provided, it defaults to 0. ### Request Example ```python from xhshow import Xhshow client = Xhshow() # Using default parameters xray_id = client.get_xray_trace_id() print(xray_id) # Output: cd7621e82d9c24e90bfd937d92bbbd1b # Using specified parameters xray_id = client.get_xray_trace_id(timestamp=1764896636081, seq=5) print(xray_id) # Output: cd7604be588000051a7fb8ae74496a76 ``` ### Response #### Success Response (200) - **xray_id** (str) - A 32-character hexadecimal string representing the XRay trace ID. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.