你是否厌倦了在浏览器中频繁切换标签页来刷推特?网页版不仅效率低下,还常被广告和推荐干扰。现在,一款名为 x-cli 的 开源项目 或许能解决你的烦恼。它是一个直接对话 X/Twitter API v2 的命令行工具。无论是发推、搜索、管理书签,还是查看粉丝数据,全部在你的终端中完成。如果你已经在用 x-mcp,它甚至可以实现零配置启动,真正的极客范儿!
x-cli 并不是一个简单的脚本,它封装了丰富的 API 交互能力。它默认使用 rich 库提供紧凑的彩色面板输出,让数据展示既清晰又美观。以下是它支持的核心功能矩阵,保留了所有技术细节:
| Category |
Commands |
Examples |
| Post |
tweet post, tweet reply, tweet quote, tweet delete |
x-cli tweet post "hello world" |
| Read |
tweet get, tweet search, user timeline, me mentions |
x-cli tweet search "from:elonmusk" |
| Users |
user get, user followers, user following |
x-cli user get openai |
| Engage |
like, retweet |
x-cli like <tweet-url> |
| Bookmarks |
me bookmarks, me bookmark, me unbookmark |
x-cli me bookmarks --max 20 |
| Analytics |
tweet metrics |
x-cli tweet metrics <tweet-id> |
设计亮点:
- 智能识别:推文 URL 和 ID 互通,既可以粘贴
https://x.com/user/status/123,也可以直接输入 123。
- 多模式输出:支持人类可读、原始 JSON、TSV 和 Markdown 格式,方便开发者利用管道(Pipe)进行二次处理。
实战演示:从安装到上手
1. 安装
推荐使用 uv 进行安装,速度极快:
# from source
git clone https://github.com/INFATOSHI/x-cli.git
cd x-cli
uv tool install .
# or from PyPI (once published)
uv tool install x-cli
2. 认证配置
你需要从 X Developer Portal 获取 5 个核心凭证。如果你已经配置过 x-mcp,可以直接软链接配置文件,实现秒开:
mkdir -p ~/.config/x-cli
ln -s /path/to/x-mcp/.env ~/.config/x-cli/.env
如果是全新配置,请在 ~/.config/x-cli/.env 中填入以下内容:
X_API_KEY=your_consumer_key
X_API_SECRET=your_secret_key
X_BEARER_TOKEN=your_bearer_token
X_ACCESS_TOKEN=your_access_token
X_ACCESS_TOKEN_SECRET=your_access_token_secret
3. 核心命令使用
推文操作与搜索:
x-cli tweet post "Hello world"
x-cli tweet post --poll "Yes,No" "Do you like polls?"
x-cli tweet get <id-or-url>
x-cli tweet delete <id-or-url>
x-cli tweet reply <id-or-url> "nice post"
x-cli tweet quote <id-or-url> "this is important"
x-cli tweet search "machine learning" --max 20
x-cli tweet metrics <id-or-url>
用户管理:
x-cli user get elonmusk
x-cli user timeline elonmusk --max 10
x-cli user followers elonmusk --max 50
x-cli user following elonmusk
个人中心与快捷操作:
# 查看提及和书签
x-cli me mentions --max 20
x-cli me bookmarks
x-cli me bookmark <id-or-url>
x-cli me unbookmark <id-or-url>
# 快速点赞和转推
x-cli like <id-or-url>
x-cli retweet <id-or-url>
4. 高级输出模式
除了默认的彩色面板,你还可以组合使用参数来适配不同场景(例如脚本自动化):
x-cli tweet get <id> # human-readable (default)
x-cli -j tweet get <id> # raw JSON, pipe to jq
x-cli -p user get elonmusk # TSV, pipe to awk/cut
x-cli -md tweet get <id> # markdown
x-cli -j tweet search "ai" | jq '.data[].text'
如果想看到时间戳、指标和分页令牌等元数据,加上 -v 参数即可:
x-cli -v tweet get <id> # human + timestamps, metrics, pagination tokens
x-cli -v -md user get elonmusk # markdown + join date, location
x-cli -v -j tweet get <id> # full JSON (includes, meta, everything)
避坑指南与总结
在使用过程中,你可能会遇到以下常见问题,请对照排查:
-
403 “oauth1-permissions” when posting:这是因为你的 Access Token 是在开启写权限之前生成的。请前往 Portal 将权限设为 “Read and write”,然后务必重新生成 Access Token 和 Secret。
-
401 Unauthorized:请仔细检查 .env 文件中的 5 个凭证,确保没有多余的空格或换行符。
-
429 Rate Limited:遇到限流错误时,错误信息中会包含重置时间戳(reset timestamp),请耐心等待直到该时间点。
-
“Missing env var” on startup:x-cli 会按优先级查找凭证:~/.config/x-cli/.env -> 当前目录的 .env -> 环境变量。请确保至少有一个来源包含了完整的 5 个值。
总结: x-cli 是专为习惯在终端中生活的开发者打造的工具,它不仅是 Twitter 的 CLI 客户端,更是一个展示如何优雅包装 REST API 的教学范例。如果你是重度 Twitter 用户且热爱命令行操作,这个项目绝对值得一试。
License: MIT
GitHub开源地址: https://github.com/Infatoshi/x-cli
想探索更多 Python 工具和开发技巧?欢迎在 云栈社区 交流分享。