A CLI for Twitter/X — feed, bookmarks, and user timeline in terminal
Project description
Twitter CLI
Twitter/X 命令行工具 — 读取 Timeline、书签和用户信息。
零 API Key — 使用浏览器 Cookie 认证,免费访问 Twitter。
安装
# 推荐:uv tool(更快,隔离环境)
uv tool install twitter-cli
# 其次:pipx
pipx install twitter-cli
从源码安装:
git clone git@github.com:jackwener/twitter-cli.git
cd twitter-cli
uv sync
Quick Start
# 运行(自动从 Chrome 提取 Cookie)
twitter feed
首次运行确保 Chrome 已登录 x.com。
使用方式
读取
# 抓取首页 timeline(For You 算法推荐)
twitter feed
# 抓取关注的人的 timeline(Following 时间线)
twitter feed -t following
# 自定义抓取条数
twitter feed --max 50
# 开启筛选(按 score 排序过滤)
twitter feed --filter
# JSON 输出
twitter feed --json > tweets.json
# 从已有数据加载
twitter feed --input tweets.json
# 抓取收藏
twitter favorite
twitter favorite --max 30 --json
用户
# 查看用户资料
twitter user elonmusk
# 列出用户推文
twitter user-posts elonmusk --max 20
Pipeline
抓取 (GraphQL API) → 筛选 (Engagement Score)
50 条 top 20
筛选算法
加权评分公式,收藏权重最高(代表"值得回看"):
score = 1.0 × likes + 3.0 × retweets + 2.0 × replies
+ 5.0 × bookmarks + 0.5 × log10(views)
配置
编辑 config.yaml:
fetch:
count: 50
filter:
mode: "topN" # "topN" | "score" | "all"
topN: 20
weights:
likes: 1.0
retweets: 3.0
replies: 2.0
bookmarks: 5.0
views_log: 0.5
Cookie 配置
方式 1:自动提取(推荐) — 确保浏览器已登录 x.com,程序自动通过 browser-cookie3 按 Chrome → Edge → Firefox → Brave 顺序尝试读取。
方式 2:环境变量 — 设置:
export TWITTER_AUTH_TOKEN=your_auth_token
export TWITTER_CT0=your_ct0
可通过 Cookie-Editor 浏览器插件导出。
项目结构
twitter_cli/
├── __init__.py # 版本信息
├── cli.py # CLI 入口 (click)
├── client.py # Twitter GraphQL API Client (GET)
├── auth.py # Cookie 提取 (env / browser-cookie3)
├── filter.py # Engagement scoring + 筛选
├── formatter.py # Rich 终端输出 + JSON
├── config.py # YAML 配置加载
├── serialization.py # Tweet JSON <-> dataclass
└── models.py # 数据模型 (dataclass)
Development
# Install development tools
uv sync --extra dev
# Run tests
uv run pytest
# Lint
uv run ruff check .
注意事项
- 使用 Cookie 登录存在被平台检测的风险,建议使用专用小号
- Cookie 只存在本地,不上传不外传
- GraphQL
queryId会从 Twitter 前端 JS 自动检测,无需手动维护
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 Distribution
Built Distribution
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 twitter_cli-0.1.0.tar.gz.
File metadata
- Download URL: twitter_cli-0.1.0.tar.gz
- Upload date:
- Size: 55.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbc98c6e1a66b2611050fc779ac405346cd1a234fcb6af2e0f455f668ed89f22
|
|
| MD5 |
aa903a845ccc051addb34919c1172687
|
|
| BLAKE2b-256 |
b2fd2f97a396007a5269a5251397a26a12f711cbde65a3b3efb50a3461e33b98
|
Provenance
The following attestation bundles were made for twitter_cli-0.1.0.tar.gz:
Publisher:
publish.yml on jackwener/twitter-cli
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
twitter_cli-0.1.0.tar.gz -
Subject digest:
dbc98c6e1a66b2611050fc779ac405346cd1a234fcb6af2e0f455f668ed89f22 - Sigstore transparency entry: 1039210286
- Sigstore integration time:
-
Permalink:
jackwener/twitter-cli@34de425a4c788775a95608250bafd8caa61d606f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jackwener
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@34de425a4c788775a95608250bafd8caa61d606f -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file twitter_cli-0.1.0-py3-none-any.whl.
File metadata
- Download URL: twitter_cli-0.1.0-py3-none-any.whl
- Upload date:
- Size: 22.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edb3f49fb741f5dedbdc27c91125aec46687d154191ce98830a2b3841a349f74
|
|
| MD5 |
5e23807d64838f613eb1f4c106d64709
|
|
| BLAKE2b-256 |
5e3f6b74ac9d38ffe05a867f49215b3e2139e0ffc41185d71657d48f61d85e46
|
Provenance
The following attestation bundles were made for twitter_cli-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on jackwener/twitter-cli
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
twitter_cli-0.1.0-py3-none-any.whl -
Subject digest:
edb3f49fb741f5dedbdc27c91125aec46687d154191ce98830a2b3841a349f74 - Sigstore transparency entry: 1039210341
- Sigstore integration time:
-
Permalink:
jackwener/twitter-cli@34de425a4c788775a95608250bafd8caa61d606f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jackwener
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@34de425a4c788775a95608250bafd8caa61d606f -
Trigger Event:
workflow_dispatch
-
Statement type: