Skip to main content

Twitter/X API 客户端库 (Rust 实现, 提供 Python 绑定)

Project description

x-api-rs

X/Twitter Web 私有 API 的 Rust 核心库、Python 绑定和 CLI 工具。

当前架构

本仓库已经拆分为 Rust workspace:

src/core  # x-api-core:真实业务实现
src/py    # x-api-py:Python 绑定
src/cli   # x-api-cli:命令行工具,二进制名 x-api

核心协议边界统一放在 x_api_core::web。XChat 是 Web 协议下的复杂子域,位于 web::xchat

Rust 用法

use x_api_core::web::{WebClient, WebProfile};

#[tokio::main]
async fn main() -> x_api_core::Result<()> {
    let cookies = "ct0=xxx; auth_token=yyy; twid=u%3D123456789";
    let client = WebClient::builder()
        .cookies(cookies)
        .profile(WebProfile::default_latest())
        .build()
        .await?;

    let result = client.dm().send_message("123456789", "hello", None).await?;
    println!("{result:?}");
    Ok(())
}

Python 用法

from x_api_rs.web import Client, WebProfile

client = await Client.create(
    cookies,
    proxy_url=None,
    profile=WebProfile.default_latest(),
)

Web 环境快照

WebEnvironmentSnapshot 可把 cookies、CSRF、auth token、user id、代理、WebProfile、UA/请求头策略、ClientTransaction 材料和可选 XChat keystore 材料导出为 JSON,适合存入数据库后下次离线还原客户端。

快照是明文敏感数据,包含可直接恢复账号会话的 cookies/token,启用 XChat 时还可能包含私钥材料。生产环境应由调用方使用数据库加密、KMS 或应用层加密后存储。

let snapshot_json = client.export_environment()?.to_json()?;
let restored = WebClient::restore_environment(WebEnvironmentSnapshot::from_json(&snapshot_json)?)?;
snapshot_json = client.export_environment_json()
restored = Client.restore_environment_json(snapshot_json)

CLI 用法

x-api --cookies-file cookies.txt dm send --user-id 123 --text "hello"
x-api --cookies-file cookies.txt environment export --allow-plaintext-secrets > state.jsonl
x-api environment restore-check --state-file state.json
x-api version-full
x-api output-schema

CLI 输出保持 JSONL envelope,适合 agent、脚本和管道消费。

验证命令

cargo check --workspace --all-features
cargo test -p x-api-core --all-features --no-run
cargo test -p x-api-cli --all-features

Python wheel 构建需要安装 maturin

python3 -m maturin build --manifest-path src/py/Cargo.toml --features "dm upload inbox user posts communities settings search xchat"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

x_api_rs-3.0.4.tar.gz (244.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

x_api_rs-3.0.4-cp38-abi3-manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.28+ x86-64

x_api_rs-3.0.4-cp38-abi3-manylinux_2_28_aarch64.whl (5.1 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.28+ ARM64

x_api_rs-3.0.4-cp38-abi3-macosx_11_0_arm64.whl (4.6 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file x_api_rs-3.0.4.tar.gz.

File metadata

  • Download URL: x_api_rs-3.0.4.tar.gz
  • Upload date:
  • Size: 244.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for x_api_rs-3.0.4.tar.gz
Algorithm Hash digest
SHA256 21c8054f92bdf0f00388458854ff483fbd973663c6a7e9e5bc122381d307814f
MD5 27d3b2b17f378b1c85e46d142aa5f646
BLAKE2b-256 9cb451098cdebc58b700d8966a3cea1956026e1952065127c4fdf4477d07a994

See more details on using hashes here.

Provenance

The following attestation bundles were made for x_api_rs-3.0.4.tar.gz:

Publisher: build-and-release.yml on open-luban/x-api-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file x_api_rs-3.0.4-cp38-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for x_api_rs-3.0.4-cp38-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cf75bdefcd8ef7116029d24f27f6eabe81679d16eec6af594d1d10c66aa0d9e3
MD5 4d446c834ab8fd71e6217add38819388
BLAKE2b-256 b6ac288b6b10a53a2538a53d36beca5fe73d859b3ce709d1ce0dc5067e52f037

See more details on using hashes here.

Provenance

The following attestation bundles were made for x_api_rs-3.0.4-cp38-abi3-manylinux_2_28_x86_64.whl:

Publisher: build-and-release.yml on open-luban/x-api-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file x_api_rs-3.0.4-cp38-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for x_api_rs-3.0.4-cp38-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 d790a3bb85510b4fed5a6648aecbca0ceccd65e1bc5e1369be7b86020c2b2e9f
MD5 123f108acbaa73117caf4af3077711b0
BLAKE2b-256 8e35b60fc64425f56162640e03d7b65d87fc4acc83de76d910e5f03834952691

See more details on using hashes here.

Provenance

The following attestation bundles were made for x_api_rs-3.0.4-cp38-abi3-manylinux_2_28_aarch64.whl:

Publisher: build-and-release.yml on open-luban/x-api-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file x_api_rs-3.0.4-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for x_api_rs-3.0.4-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b1bbc35f1496634f957d2cf631bf04699a60ace55b285a8a9f3d82692dada4bc
MD5 8100472cdbcb3ecc8c8176c2739f5219
BLAKE2b-256 21b4239ae062be5158265d83176ad199109d0eef755606b7d5beabed2aef1554

See more details on using hashes here.

Provenance

The following attestation bundles were made for x_api_rs-3.0.4-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: build-and-release.yml on open-luban/x-api-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page