Twitter DM 批量并发发送库 (Rust 实现, 提供 Python 绑定)
Project description
Twitter Full Feature Library
基于 Rust 开发的高性能 Twitter (X) 全功能自动化库,通过 PyO3 提供完全异步的 Python 接口。不仅支持私信批量发送,还涵盖了发帖、用户管理、媒体上传、收件箱同步等完整功能。
✨ 功能特性
- 📨 消息服务 (DM): 支持单条/批量发送私信、自定义文案、媒体附件
- 📝 帖子服务 (Posts): 发布推文、回复、引用、转发、点赞、删除
- 👥 用户服务 (User): 获取个人资料、修改资料(头像/背景/简介/名称)
- 📤 媒体服务 (Upload): 支持图片/视频上传、分片上传
- 📥 收件箱 (Inbox): 获取私信会话、消息更新
- 🔒 安全认证: 基于 cookies 的认证机制,支持 JA3/JA4 指纹绕过
- ⚡ 高性能: 使用 Tokio 实现真正的并发请求
- 🐍 Python 绑定: 提供符合 Python 习惯的完整异步 API
📦 安装
从源码构建 Python 扩展
# 安装 maturin (Python 包构建工具)
pip install maturin
# 开发模式安装 (快速测试)
maturin develop
# 或者构建 release wheel
maturin build --release
使用 Rust
在 Cargo.toml 中添加依赖:
[dependencies]
x_dm_python = { path = "." }
tokio = { version = "1", features = ["full"] }
🚀 快速开始
Python 使用示例 (异步 API)
重要: 所有操作均为异步,请确保在
async函数中运行。
import asyncio
import x_dm_python
async def main():
# 初始化客户端 (需要有效的 cookies)
cookies = "ct0=your_csrf_token; auth_token=your_auth_token; twid=u%3D123456789"
client = x_dm_python.Twitter(cookies)
# 1. 发送推文
print("--- 发布推文 ---")
tweet_result = await client.create_tweet(
text="Hello from x_dm_python! 🚀",
# reply_to="tweet_id_if_reply",
# media_ids=["media_id_1"]
)
if tweet_result.success:
print(f"推文发送成功: {tweet_result.tweet_id}")
# 点赞该推文
await client.favorite_tweet(tweet_result.tweet_id)
# 2. 修改个人资料
print("\n--- 更新资料 ---")
profile_result = await client.edit_profile(
name="Rust Python Bot",
description="Automated by x_dm_python library"
)
if profile_result.success:
print(f"资料更新成功: {profile_result.name}")
# 3. 获取用户信息
print("\n--- 获取信息 ---")
user_info = await client.get_profile("elonmusk")
if user_info.success:
print(f"用户: {user_info.name}, 粉丝数: {user_info.followers_count}")
# 4. 批量发送私信
print("\n--- 批量私信 ---")
user_ids = ["123456789", "987654321"]
batch_result = await client.send_batch_direct_messages(
user_ids=user_ids,
message="这是一条批量测试消息!"
)
print(f"成功: {batch_result.success_count}, 失败: {batch_result.failure_count}")
# 运行
asyncio.run(main())
Rust 使用示例
use x_dm_python::{Twitter, CreateTweetParams};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 初始化客户端
let cookies = "ct0=...; auth_token=...; twid=...";
let client = Twitter::new(cookies.to_string(), None)?;
// 发布推文
let params = CreateTweetParams::new("Hello from Rust!");
let result = client.create_tweet(params).await?;
println!("Sent tweet ID: {:?}", result.tweet_id);
// 获取用户资料
let profile = client.get_profile("elonmusk").await?;
println!("Elon's followers: {:?}", profile.followers_count);
Ok(())
}
📖 API 文档概览
核心类 Twitter 聚合了所有功能模块:
📝 Posts (帖子)
create_tweet(text: str, ...): 发推文/回复delete_tweet(tweet_id: str): 删除推文favorite_tweet(tweet_id: str): 点赞unfavorite_tweet(tweet_id: str): 取消点赞create_retweet(tweet_id: str): 转发delete_retweet(tweet_id: str): 取消转发get_tweets(user_id: str): 获取用户推文列表
👥 User (用户)
get_profile(screen_name: str): 通过用户名获取资料get_profile_by_id(rest_id: str): 通过 ID 获取资料edit_profile(name: str, description: str, ...): 编辑资料change_profile_image(media_id: str): 更换头像change_background_image(media_id: str): 更换背景图
📨 DM (私信)
send_direct_message(user_id: str, message: str): 发送单条send_batch_direct_messages(user_ids: List[str], ...): 批量发送
📤 Upload (上传)
upload_image(bytes: bytes, category: str): 上传图片upload_video(...): 上传视频 (Rust API)
📥 Inbox (收件箱)
get_user_updates(...): 获取消息更新
(详细参数请参考代码注释或生成的 Python 类型提示)
🔐 获取 Twitter Cookies
- 浏览器登录 Twitter (X)
- F12 打开开发者工具 -> Network
- 发送任意请求,查看 Request Headers 中的
Cookie - 确保包含:
ct0(CSRF Token),auth_token,twid
🏗️ 架构设计
本项目采用模块化组合模式:
- common: 基础 HTTP 客户端、认证、错误处理
- x (Core):
dm: 私信模块posts: 帖子/推文模块user: 用户资料模块upload: 媒体上传模块inbox: 收件箱模块
- python: PyO3 绑定层
- lib.rs: 统一入口
Twitter结构体
📜 许可证
MIT License
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_dm_python-1.1.8-cp38-abi3-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: x_dm_python-1.1.8-cp38-abi3-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 4.3 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 |
f6bda7006788569dad4e63a3460f6e95c28f781209d2deeacf7106b8d7300e17
|
|
| MD5 |
9df85829a8139ae3b95e42ca1bc2848d
|
|
| BLAKE2b-256 |
9109f879b5e0a2f1e3bd42c44b8b96df4779e08317c51cc553aa1e8ce791a714
|
Provenance
The following attestation bundles were made for x_dm_python-1.1.8-cp38-abi3-manylinux_2_28_x86_64.whl:
Publisher:
build-and-release.yml on Robin528919/x_dm_python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
x_dm_python-1.1.8-cp38-abi3-manylinux_2_28_x86_64.whl -
Subject digest:
f6bda7006788569dad4e63a3460f6e95c28f781209d2deeacf7106b8d7300e17 - Sigstore transparency entry: 799025897
- Sigstore integration time:
-
Permalink:
Robin528919/x_dm_python@c0ef56fecce63fda1dd35363d475b7d291d81ee4 -
Branch / Tag:
refs/tags/release-v1.1.8 - Owner: https://github.com/Robin528919
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-release.yml@c0ef56fecce63fda1dd35363d475b7d291d81ee4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file x_dm_python-1.1.8-cp38-abi3-manylinux_2_28_aarch64.whl.
File metadata
- Download URL: x_dm_python-1.1.8-cp38-abi3-manylinux_2_28_aarch64.whl
- Upload date:
- Size: 3.9 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 |
dedfeaf1059a7bc94c82da0ef673b26acceec10490e289db290bdcf87053b7bd
|
|
| MD5 |
534ee2295dd9ba173f244671c048420c
|
|
| BLAKE2b-256 |
6f1e36c65df85397bbc080d8bc6054661c5823f071bbb8b529794b317216a07c
|
Provenance
The following attestation bundles were made for x_dm_python-1.1.8-cp38-abi3-manylinux_2_28_aarch64.whl:
Publisher:
build-and-release.yml on Robin528919/x_dm_python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
x_dm_python-1.1.8-cp38-abi3-manylinux_2_28_aarch64.whl -
Subject digest:
dedfeaf1059a7bc94c82da0ef673b26acceec10490e289db290bdcf87053b7bd - Sigstore transparency entry: 799025900
- Sigstore integration time:
-
Permalink:
Robin528919/x_dm_python@c0ef56fecce63fda1dd35363d475b7d291d81ee4 -
Branch / Tag:
refs/tags/release-v1.1.8 - Owner: https://github.com/Robin528919
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-release.yml@c0ef56fecce63fda1dd35363d475b7d291d81ee4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file x_dm_python-1.1.8-cp38-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl.
File metadata
- Download URL: x_dm_python-1.1.8-cp38-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl
- Upload date:
- Size: 7.7 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 |
19141984518b5f3d00954d93a3db5e546db96352751f26d67c407dea0270cebf
|
|
| MD5 |
9a97cf0e9821879f3081bf4f4bb02444
|
|
| BLAKE2b-256 |
c8d8d57951a746bbb154e34f18d5f425b6b13320943caa7fdfb9be07c1391a8e
|
Provenance
The following attestation bundles were made for x_dm_python-1.1.8-cp38-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl:
Publisher:
build-and-release.yml on Robin528919/x_dm_python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
x_dm_python-1.1.8-cp38-abi3-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl -
Subject digest:
19141984518b5f3d00954d93a3db5e546db96352751f26d67c407dea0270cebf - Sigstore transparency entry: 799025898
- Sigstore integration time:
-
Permalink:
Robin528919/x_dm_python@c0ef56fecce63fda1dd35363d475b7d291d81ee4 -
Branch / Tag:
refs/tags/release-v1.1.8 - Owner: https://github.com/Robin528919
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-release.yml@c0ef56fecce63fda1dd35363d475b7d291d81ee4 -
Trigger Event:
push
-
Statement type: