Twitter/X API 客户端库 (Rust 实现, 提供 Python 绑定)
Project description
X API - Twitter API Python 客户端
高性能的 Twitter (X) API Python 客户端,使用 Rust + PyO3 实现,提供完整的 Twitter 平台功能支持。
✨ 特性
- 🚀 高性能 - Rust 实现,提供原生性能
- 🔒 安全可靠 - 内置 JA3/TLS 指纹模拟,增强反检测能力
- 🐍 Python 友好 - 完整的异步 API,符合 Python 惯用风格
- 📦 模块化设计 - 清晰的模块划分,易于使用和扩展
- 💪 强类型 - 完整的类型提示支持
- 📝 详细日志 - 完整的操作日志和错误追踪
📋 功能模块
| 模块 | 功能 | 状态 |
|---|---|---|
| dm | 私信发送(单条、批量、自定义文案) | ✅ 已完成 |
| upload | 图片和视频上传 | ✅ 已完成 |
| posts | 发帖、删帖、点赞、转发 | ✅ 已完成 |
| user | 用户资料查询和编辑 | ✅ 已完成 |
| inbox | 收件箱查询 | ✅ 已完成 |
| communities | 社区搜索和加入 | ✅ 已完成 |
📥 安装
从 PyPI 安装(推荐)
pip install x-api-rs
从源码安装
# 克隆仓库
git clone https://github.com/Robin528919/x-api-rs.git
cd x-api-rs
# 安装 maturin(Python 打包工具)
pip install maturin
# 开发模式安装(支持修改代码后热更新)
maturin develop --features python
# 或生产构建
maturin build --release --features python
pip install target/wheels/x_api_rs-*.whl
🚀 快速开始
基础使用
import asyncio
from x_api_rs import Twitter
async def main():
# 创建客户端(使用 cookies 认证)
client = await Twitter.create(cookies="your_cookies_here")
# 发送私信
result = await client.dm.send_message("123456", "Hello!")
if result.success:
print(f"发送成功,事件ID: {result.event_id}")
# 上传图片
with open("image.jpg", "rb") as f:
image_bytes = f.read()
upload_result = await client.upload.image(image_bytes, "tweet_image")
# 发帖(带图片)
tweet_result = await client.posts.create_tweet(
text="Hello World!",
media_ids=[upload_result.media_id_string]
)
# 获取用户资料
user = await client.user.get_profile("elonmusk")
print(f"用户: {user.name}, 粉丝: {user.followers_count}")
asyncio.run(main())
批量操作
# 批量发送私信(相同内容)
user_ids = ["123", "456", "789"]
result = await client.dm.send_batch(user_ids, "批量消息")
print(f"成功: {result.success_count}, 失败: {result.failure_count}")
# 批量发送自定义文案
user_ids = ["123", "456"]
texts = ["你好,张三!", "你好,李四!"]
result = await client.dm.send_batch_with_custom_texts(user_ids, texts)
使用代理
# 创建客户端时指定代理
client = await Twitter.create(
cookies="your_cookies_here",
proxy_url="http://proxy:8080"
)
auth_token 转换
# 将 auth_token 转换为完整的 cookies
result = await Twitter.auth_token_to_cookies(
"your_auth_token",
proxy_url="http://proxy:8080" # 可选
)
print(f"用户 ID: {result.user_id}")
print(f"Cookies: {result.cookies}")
# 使用转换后的 cookies 创建客户端
client = await Twitter.create(result.cookies)
📚 详细文档
在线文档: https://x-api-rs.es007.com
🏗️ 架构设计
本项目采用四层架构设计:
┌──────────────────────────────────────────────────────────────┐
│ Twitter (Facade 层) │
│ ┌──────┬────────┬────────┬──────────┬──────────────────┐ │
│ │ dm │ posts │ user │ inbox │ future modules │ │
│ └───┬──┴────┬───┴────┬───┴────┬─────┴──────────────────┘ │
└──────┼───────┼────────┼────────┼─────────────────────────────┘
│ │ │ │
│ │ │ │ Service 层(业务模块)
┌───▼──┐ ┌──▼───┐ ┌─▼────┐ ┌▼────────┐
│ DM │ │Posts │ │User │ │ Inbox │
│Client│ │Client│ │Client│ │ Client │
└───┬──┘ └──┬───┘ └─┬────┘ └┬────────┘
│ │ │ │
└───┬───┴───┬───┴────┬───┘
│ │ │
┌────▼───────▼────────▼─────┐ Capability 层(共享能力)
│ MediaUpload Capability │
└────────────┬──────────────┘
│
┌─────────▼──────────────┐ Common 层(通用基础)
│ Auth + HttpClient │
└────────────────────────┘
🔧 开发
环境准备
# 安装 Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# 安装 Python 3.8+
# 安装 maturin
pip install maturin
本地开发
# 开发模式构建
maturin develop --features python
# 运行测试
cargo test
# 运行 Python 测试
pytest python_tests/
# 运行示例
python examples/python/async_example.py
代码规范
- Rust 代码:遵循
rustfmt和clippy规范 - Python 代码:遵循 PEP 8 规范
- 提交信息:使用 Conventional Commits 格式
详细开发指南请参考:开发文档
🧪 测试
# 运行 Rust 单元测试
cargo test
# 运行 Rust 集成测试(需要配置 cookies)
cargo test -- --ignored
# 运行 Python 测试
pytest python_tests/ -v
# 带日志输出
RUST_LOG=debug cargo test -- --nocapture
详细测试说明请参考:测试文档
📊 性能
- 使用 Rust 实现核心逻辑,提供原生性能
- 支持异步并发操作,充分利用多核性能
- 批量操作使用 Tokio 并发执行
- HTTP 连接池复用,减少连接开销
🛡️ 安全性
- 支持 JA3/TLS 指纹模拟(Chrome 136)
- 不在日志中输出敏感信息(cookies、token)
- 完整的参数验证和错误处理
- 遵循 Twitter API 使用限制
📄 许可证
本项目采用 MIT 许可证。详见 LICENSE 文件。
🤝 贡献
欢迎提交 Issue 和 Pull Request!
在提交代码前,请确保:
- 通过所有测试
- 遵循代码规范
- 更新相关文档
- 编写清晰的提交信息
📮 联系方式
- 问题反馈:GitHub Issues
- 功能建议:GitHub Discussions
🙏 致谢
⚠️ 免责声明
本项目仅供学习和研究使用。使用本项目访问 Twitter API 时,请遵守 Twitter 的服务条款和使用政策。开发者不对使用本项目造成的任何后果负责。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file x_api_rs-1.0.3-cp38-abi3-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: x_api_rs-1.0.3-cp38-abi3-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 4.8 MB
- Tags: CPython 3.8+, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee7173e2678294ef540fdc9158edf942c12a7408a894a9a2c1506cf4865d46d5
|
|
| MD5 |
0b5b1d54d7cb4e153e3529053a3c76f8
|
|
| BLAKE2b-256 |
5d1051b9553b156c9c3850c0c8d009ddb5ec3819c09de7372946d1dc5758c563
|
Provenance
The following attestation bundles were made for x_api_rs-1.0.3-cp38-abi3-manylinux_2_28_x86_64.whl:
Publisher:
build-and-release.yml on open-luban/x-api-rs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
x_api_rs-1.0.3-cp38-abi3-manylinux_2_28_x86_64.whl -
Subject digest:
ee7173e2678294ef540fdc9158edf942c12a7408a894a9a2c1506cf4865d46d5 - Sigstore transparency entry: 1095945011
- Sigstore integration time:
-
Permalink:
open-luban/x-api-rs@9c6397b5c6c1f24d48e3bf57fa1ac120e7d45eb6 -
Branch / Tag:
refs/tags/release-v1.0.3 - Owner: https://github.com/open-luban
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-release.yml@9c6397b5c6c1f24d48e3bf57fa1ac120e7d45eb6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file x_api_rs-1.0.3-cp38-abi3-manylinux_2_28_aarch64.whl.
File metadata
- Download URL: x_api_rs-1.0.3-cp38-abi3-manylinux_2_28_aarch64.whl
- Upload date:
- Size: 4.6 MB
- Tags: CPython 3.8+, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61d0bdfc1c22e4c03f8cdde27ac042bdb2ed35cc1efdcadaf53b5bb2d060f743
|
|
| MD5 |
bcf6d2ff40d926f78e82b5cb8ce19948
|
|
| BLAKE2b-256 |
2046c94c339acea79a4dfc61c8e578863b06c4425ca20218caa3cca9a7f51cd9
|
Provenance
The following attestation bundles were made for x_api_rs-1.0.3-cp38-abi3-manylinux_2_28_aarch64.whl:
Publisher:
build-and-release.yml on open-luban/x-api-rs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
x_api_rs-1.0.3-cp38-abi3-manylinux_2_28_aarch64.whl -
Subject digest:
61d0bdfc1c22e4c03f8cdde27ac042bdb2ed35cc1efdcadaf53b5bb2d060f743 - Sigstore transparency entry: 1095945018
- Sigstore integration time:
-
Permalink:
open-luban/x-api-rs@9c6397b5c6c1f24d48e3bf57fa1ac120e7d45eb6 -
Branch / Tag:
refs/tags/release-v1.0.3 - Owner: https://github.com/open-luban
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-release.yml@9c6397b5c6c1f24d48e3bf57fa1ac120e7d45eb6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file x_api_rs-1.0.3-cp38-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl.
File metadata
- Download URL: x_api_rs-1.0.3-cp38-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl
- Upload date:
- Size: 8.6 MB
- Tags: CPython 3.8+, macOS 10.13+ universal2 (ARM64, x86-64), macOS 10.13+ x86-64, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31013cc57d88dd2151e164d66b40bebde74bf158365ae182301f63f766de8c44
|
|
| MD5 |
41b5431fcbba9a2fd4d2cbd86d6a5e02
|
|
| BLAKE2b-256 |
4574b2385dde77db619bf95a2dcf8cce894074a784a85660542ba3bd6114c111
|
Provenance
The following attestation bundles were made for x_api_rs-1.0.3-cp38-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl:
Publisher:
build-and-release.yml on open-luban/x-api-rs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
x_api_rs-1.0.3-cp38-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl -
Subject digest:
31013cc57d88dd2151e164d66b40bebde74bf158365ae182301f63f766de8c44 - Sigstore transparency entry: 1095945014
- Sigstore integration time:
-
Permalink:
open-luban/x-api-rs@9c6397b5c6c1f24d48e3bf57fa1ac120e7d45eb6 -
Branch / Tag:
refs/tags/release-v1.0.3 - Owner: https://github.com/open-luban
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-release.yml@9c6397b5c6c1f24d48e3bf57fa1ac120e7d45eb6 -
Trigger Event:
push
-
Statement type: