twitter-cli 0.8.5__tar.gz → 0.9.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/.github/workflows/ci.yml +19 -2
  2. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/PKG-INFO +164 -57
  3. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/README.md +160 -53
  4. twitter_cli-0.9.0/SCHEMA.md +71 -0
  5. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/SKILL.md +45 -36
  6. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/config.yaml +4 -1
  7. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/pyproject.toml +6 -3
  8. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/conftest.py +29 -0
  9. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/fixtures/list_timeline.json +4 -0
  10. twitter_cli-0.9.0/tests/test_article.py +53 -0
  11. twitter_cli-0.9.0/tests/test_article_parsing.py +124 -0
  12. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/test_auth.py +194 -18
  13. twitter_cli-0.9.0/tests/test_cache_permissions.py +90 -0
  14. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/test_cli.py +367 -0
  15. twitter_cli-0.9.0/tests/test_client.py +2208 -0
  16. twitter_cli-0.9.0/tests/test_completion.py +47 -0
  17. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/test_config.py +17 -0
  18. twitter_cli-0.9.0/tests/test_curl_cffi_floor.py +56 -0
  19. twitter_cli-0.9.0/tests/test_pagination_cap.py +92 -0
  20. twitter_cli-0.9.0/tests/test_parser_fixtures.py +212 -0
  21. twitter_cli-0.9.0/tests/test_public_api.py +36 -0
  22. twitter_cli-0.9.0/tests/test_review_regressions.py +460 -0
  23. twitter_cli-0.9.0/tests/test_serialization.py +164 -0
  24. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/test_smoke.py +17 -0
  25. twitter_cli-0.9.0/tests/test_text.py +40 -0
  26. twitter_cli-0.9.0/tests/test_time_style.py +113 -0
  27. twitter_cli-0.9.0/twitter_cli/__init__.py +129 -0
  28. twitter_cli-0.9.0/twitter_cli/article.py +227 -0
  29. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/auth.py +424 -115
  30. twitter_cli-0.9.0/twitter_cli/cache.py +136 -0
  31. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/cli.py +598 -93
  32. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/client.py +732 -145
  33. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/commands/__init__.py +2 -1
  34. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/config.py +13 -0
  35. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/formatter.py +43 -7
  36. twitter_cli-0.9.0/twitter_cli/graphql.py +439 -0
  37. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/models.py +21 -1
  38. twitter_cli-0.9.0/twitter_cli/parser.py +756 -0
  39. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/serialization.py +80 -18
  40. twitter_cli-0.9.0/twitter_cli/text.py +180 -0
  41. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/uv.lock +147 -100
  42. twitter_cli-0.8.5/SCHEMA.md +0 -54
  43. twitter_cli-0.8.5/tests/test_client.py +0 -691
  44. twitter_cli-0.8.5/tests/test_parser_fixtures.py +0 -93
  45. twitter_cli-0.8.5/tests/test_serialization.py +0 -79
  46. twitter_cli-0.8.5/twitter_cli/__init__.py +0 -8
  47. twitter_cli-0.8.5/twitter_cli/cache.py +0 -66
  48. twitter_cli-0.8.5/twitter_cli/graphql.py +0 -227
  49. twitter_cli-0.8.5/twitter_cli/parser.py +0 -315
  50. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
  51. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/.github/ISSUE_TEMPLATE/feature_request.yml +0 -0
  52. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/.github/workflows/publish.yml +0 -0
  53. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/.gitignore +0 -0
  54. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/AGENTS.md +0 -0
  55. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/LICENSE +0 -0
  56. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/fixtures/followers_page.json +0 -0
  57. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/fixtures/home_timeline.json +0 -0
  58. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/fixtures/search_timeline.json +0 -0
  59. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/fixtures/tweet_detail.json +0 -0
  60. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/test_cache.py +0 -0
  61. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/test_config_normalization.py +0 -0
  62. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/test_filter.py +0 -0
  63. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/test_output.py +0 -0
  64. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/test_search.py +0 -0
  65. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/tests/test_timeutil.py +0 -0
  66. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/constants.py +0 -0
  67. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/exceptions.py +0 -0
  68. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/filter.py +0 -0
  69. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/output.py +0 -0
  70. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/search.py +0 -0
  71. {twitter_cli-0.8.5 → twitter_cli-0.9.0}/twitter_cli/timeutil.py +0 -0
@@ -6,18 +6,31 @@ on:
6
6
  pull_request:
7
7
  workflow_call:
8
8
 
9
+ permissions:
10
+ contents: read
11
+
12
+ concurrency:
13
+ group: ci-${{ github.workflow }}-${{ github.ref }}
14
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
15
+
9
16
  jobs:
10
17
  test:
11
- runs-on: ubuntu-latest
18
+ runs-on: ubuntu-24.04
19
+ timeout-minutes: 20
12
20
  strategy:
21
+ fail-fast: false
13
22
  matrix:
14
- python-version: ["3.10", "3.11", "3.12"]
23
+ python-version: ["3.10", "3.11", "3.12", "3.13"]
15
24
  steps:
16
25
  - name: Checkout
17
26
  uses: actions/checkout@v4
27
+ with:
28
+ persist-credentials: false
18
29
 
19
30
  - name: Setup uv
20
31
  uses: astral-sh/setup-uv@v6
32
+ with:
33
+ enable-cache: true
21
34
 
22
35
  - name: Setup Python
23
36
  uses: actions/setup-python@v5
@@ -26,12 +39,16 @@ jobs:
26
39
 
27
40
  - name: Install dependencies
28
41
  run: uv sync --extra dev
42
+ timeout-minutes: 10
29
43
 
30
44
  - name: Lint
31
45
  run: uv run ruff check .
46
+ timeout-minutes: 5
32
47
 
33
48
  - name: Type check
34
49
  run: uv run mypy twitter_cli
50
+ timeout-minutes: 5
35
51
 
36
52
  - name: Test
37
53
  run: uv run pytest -q
54
+ timeout-minutes: 10
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: twitter-cli
3
- Version: 0.8.5
3
+ Version: 0.9.0
4
4
  Summary: A CLI for Twitter/X — feed, bookmarks, and user timeline in terminal
5
5
  Project-URL: Homepage, https://github.com/jackwener/twitter-cli
6
6
  Project-URL: Repository, https://github.com/jackwener/twitter-cli
@@ -19,10 +19,10 @@ Requires-Python: >=3.10
19
19
  Requires-Dist: beautifulsoup4>=4.12
20
20
  Requires-Dist: browser-cookie3>=0.19
21
21
  Requires-Dist: click>=8.0
22
- Requires-Dist: curl-cffi>=0.7
22
+ Requires-Dist: curl-cffi>=0.15.0
23
23
  Requires-Dist: pyyaml>=6.0
24
24
  Requires-Dist: rich>=13.0
25
- Requires-Dist: xclienttransaction>=1.0.1
25
+ Requires-Dist: xclienttransaction>=1.0.3
26
26
  Provides-Extra: dev
27
27
  Requires-Dist: mypy<1.15,>=1.14; extra == 'dev'
28
28
  Requires-Dist: pytest>=8.0; extra == 'dev'
@@ -53,22 +53,26 @@ A terminal-first CLI for Twitter/X: read timelines, bookmarks, and user profiles
53
53
 
54
54
  **Read:**
55
55
  - Timeline: fetch `for-you` and `following` feeds
56
- - Bookmarks: list saved tweets from your account
57
- - Search: find tweets by keyword with Top/Latest/Photos/Videos tabs
58
- - Tweet detail: view a tweet and its replies; use `show <N>` to open tweet #N from the last list output
59
- - Article: fetch a Twitter Article and export it as Markdown
56
+ - Bookmarks: list saved tweets (with `--cursor` pagination and bookmark folders)
57
+ - Explore News: fetch personalized stories from Explore > News (`twitter news`)
58
+ - Search: find tweets by keyword with Top/Latest/Photos/Videos tabs and advanced filters
59
+ - Tweet detail: view a tweet and its replies (with `--cursor` pagination); use `show <N>` to open tweet #N from the last list output
60
+ - Article: fetch a Twitter Article with rich Draft.js formatting (Markdown blocks, links, inline styles, embedded tweets) and export as Markdown
60
61
  - List timeline: fetch tweets from a Twitter List
61
- - User lookup: fetch user profile, tweets, likes, followers, and following
62
- - `--full-text`: disable tweet text truncation in rich table output
62
+ - User lookup: fetch user profile (by `@handle` or numeric user ID), tweets, likes, followers, and following
63
+ - Display controls: `--full-text` for untruncated tables, `--time relative|absolute|iso` for timestamps, `completion bash|zsh|fish`, and `doctor [--refresh]`
63
64
  - Structured output: export any data as YAML or JSON for scripting and AI agent integration
64
65
  - Optional scoring filter: rank tweets by engagement weights
66
+ - Python library API: import `TwitterClient`, `get_cookies`, models, and serializers directly from `twitter_cli`
65
67
  - Structured output contract: [SCHEMA.md](./SCHEMA.md)
66
68
 
67
69
  > **AI Agent Tip:** Prefer `--yaml` for structured output unless a strict JSON parser is required. Non-TTY stdout defaults to YAML automatically. Use `--max` to limit results.
68
70
 
69
71
  **Write:**
70
- - Post: create new tweets and replies, with optional image attachments (up to 4)
71
- - Quote: quote-tweet with optional images
72
+ - Post: create new tweets and replies, with optional images (up to 4) or video (`--video` / `-V`, MP4/MOV/WebM up to 512 MB)
73
+ - Long-form posts: tweets over 280 weighted characters automatically route to `CreateNoteTweet` (X Premium)
74
+ - Article publish: publish X Articles (or drafts) directly from Markdown (`twitter article-publish`)
75
+ - Quote: quote-tweet with optional images or video
72
76
  - Delete: remove your own tweets
73
77
  - Like / Unlike: manage tweet likes
74
78
  - Retweet / Unretweet: manage retweets
@@ -76,12 +80,12 @@ A terminal-first CLI for Twitter/X: read timelines, bookmarks, and user profiles
76
80
  - Write commands also support explicit `--json` / `--yaml` output now
77
81
 
78
82
  **Auth & Anti-Detection:**
79
- - Cookie auth: use browser cookies or environment variables
83
+ - Cookie auth: browser cookies (Arc, Chrome, Edge, Firefox, Brave, Vivaldi, Zen, custom Chromium dir) or env vars (`TWITTER_COOKIE_STRING` or `TWITTER_AUTH_TOKEN` + `TWITTER_CT0`)
80
84
  - Full cookie forwarding: extracts ALL browser cookies for richer browser context
81
85
  - TLS fingerprint impersonation: `curl_cffi` with dynamic Chrome version matching
82
- - `x-client-transaction-id` header generation
83
- - Request timing jitter to avoid pattern detection
84
- - Write operation delays (1.5–4s random) to mitigate rate limits
86
+ - `x-client-transaction-id` header generation with fail-closed protection on unsigned `POST` mutations
87
+ - Self-healing GraphQL `queryId` resolution with 24h local cache and `TWITTER_QID_<OP>` overrides
88
+ - Request timing jitter and write delays (1.5–4s random) to mitigate rate limits
85
89
  - Proxy support via `TWITTER_PROXY` environment variable
86
90
 
87
91
  ### Installation
@@ -129,6 +133,7 @@ twitter feed --filter
129
133
  ```bash
130
134
  # Feed
131
135
  twitter feed --max 50
136
+ twitter feed --cursor "<next-cursor-from-previous-response>"
132
137
  twitter feed --full-text
133
138
  twitter feed --output tweets.json
134
139
  twitter feed --input tweets.json
@@ -136,9 +141,14 @@ twitter feed --json # Structured stdout for scripts/agents
136
141
 
137
142
  # Bookmarks
138
143
  twitter bookmarks
144
+ twitter bookmarks --cursor "<next-cursor-from-previous-response>"
139
145
  twitter bookmarks --full-text
140
146
  twitter bookmarks --max 30 --yaml
141
147
 
148
+ # Explore News
149
+ twitter news --max 20
150
+ twitter news --json
151
+
142
152
  # Search
143
153
  twitter search "Claude Code"
144
154
  twitter search "AI agent" -t Latest --max 50
@@ -151,6 +161,7 @@ twitter search "trending" --filter # Apply ranking filter
151
161
 
152
162
  # Tweet detail (view tweet + replies)
153
163
  twitter tweet 1234567890
164
+ twitter tweet 1234567890 --cursor "<next-cursor>"
154
165
  twitter tweet 1234567890 --full-text
155
166
  twitter tweet https://x.com/user/status/1234567890
156
167
 
@@ -159,18 +170,22 @@ twitter show 2 # Open tweet #2 from last feed/search
159
170
  twitter show 2 --full-text # Full text in reply table
160
171
  twitter show 2 --json # Structured output
161
172
 
162
- # Twitter Article
173
+ # Twitter Article (read & publish)
163
174
  twitter article 1234567890
164
175
  twitter article https://x.com/user/article/1234567890 --json
165
176
  twitter article 1234567890 --markdown
166
177
  twitter article 1234567890 --output article.md
178
+ twitter article-publish post.md --title "My Article" # Publish X Article
179
+ twitter article-publish post.md --title "My Draft" --draft # Save as draft
167
180
 
168
181
  # List timeline
169
182
  twitter list 1539453138322673664
183
+ twitter list 1539453138322673664 --cursor "<next-cursor-from-previous-response>"
170
184
  twitter list 1539453138322673664 --full-text
171
185
 
172
- # User
186
+ # User (accepts @handle or numeric user ID)
173
187
  twitter user elonmusk
188
+ twitter user 44196397
174
189
  twitter user-posts elonmusk --max 20
175
190
  twitter user-posts elonmusk --full-text
176
191
  twitter user-posts elonmusk -o tweets.json
@@ -184,6 +199,7 @@ twitter following elonmusk --max 50
184
199
  twitter post "Hello from twitter-cli!"
185
200
  twitter post "Hello!" --image photo.jpg # Post with image
186
201
  twitter post "Gallery" -i a.png -i b.jpg -i c.webp # Up to 4 images
202
+ twitter post "Demo clip" --video clip.mp4 # Post with video (up to 512 MB)
187
203
  twitter post "reply text" --reply-to 1234567890
188
204
  twitter reply 1234567890 "Nice!" -i screenshot.png # Reply with image
189
205
  twitter quote 1234567890 "Look" -i chart.png # Quote with image
@@ -197,16 +213,39 @@ twitter unretweet 1234567890
197
213
  twitter bookmark 1234567890
198
214
  twitter unbookmark 1234567890
199
215
  twitter follow elonmusk --json
216
+
217
+ # Display, completion & diagnostics
218
+ twitter --time relative feed # relative | absolute | iso
219
+ twitter completion zsh # bash | zsh | fish
220
+ twitter doctor # Check deps, config, auth presence, caches
221
+ twitter doctor --refresh # Refresh cached GraphQL query IDs
222
+ ```
223
+
224
+ ### Python Library Usage
225
+
226
+ ```python
227
+ from twitter_cli import TwitterClient, get_cookies
228
+
229
+ cookies = get_cookies()
230
+ client = TwitterClient(
231
+ cookies["auth_token"],
232
+ cookies["ct0"],
233
+ cookie_string=cookies.get("cookie_string"),
234
+ )
235
+ tweets = client.fetch_home_timeline(count=20)
236
+ for tweet in tweets:
237
+ print(f"@{tweet.author.screen_name}: {tweet.text[:80]}")
200
238
  ```
201
239
 
202
240
  ### Authentication
203
241
 
204
242
  twitter-cli uses this auth priority:
205
243
 
206
- 1. **Environment variables**: `TWITTER_AUTH_TOKEN` + `TWITTER_CT0`
207
- 2. **Browser cookies** (recommended): auto-extract from Arc/Chrome/Edge/Firefox/Brave
244
+ 1. **Full cookie string**: `TWITTER_COOKIE_STRING` (full `Cookie:` header; auto-extracts `auth_token` + `ct0` while keeping all session cookies — great for remote servers, SigCLI, or Windows Chrome >=127)
245
+ 2. **Environment variables**: `TWITTER_AUTH_TOKEN` + `TWITTER_CT0`
246
+ 3. **Browser cookies** (recommended locally): auto-extract from Arc, Chrome, Edge, Firefox, Brave, Vivaldi, or Zen
208
247
 
209
- Browser extraction is recommended — it forwards ALL Twitter cookies (not just `auth_token` + `ct0`) and aligns request headers with your local runtime, which is closer to normal browser traffic than minimal cookie auth.
248
+ Browser extraction is recommended locally — it forwards ALL Twitter cookies (not just `auth_token` + `ct0`) and aligns request headers with your local runtime, which is closer to normal browser traffic than minimal cookie auth.
210
249
 
211
250
  **Chrome multi-profile**: All Chrome profiles are scanned automatically. To specify a profile:
212
251
 
@@ -214,14 +253,24 @@ Browser extraction is recommended — it forwards ALL Twitter cookies (not just
214
253
  TWITTER_CHROME_PROFILE="Profile 2" twitter feed
215
254
  ```
216
255
 
256
+ **Custom Chromium directory:** For Chromium, Opera, Brave/Chrome custom data dirs, or portable builds:
257
+
258
+ ```bash
259
+ TWITTER_CHROMIUM_USER_DATA_DIR="/path/to/User Data" twitter feed
260
+ ```
261
+
217
262
  **Browser priority:** If you have multiple browsers, set `TWITTER_BROWSER` to try a specific browser first:
218
263
 
219
264
  ```bash
220
- TWITTER_BROWSER=chrome twitter feed # Supported: arc, chrome, edge, firefox, brave
265
+ TWITTER_BROWSER=chrome twitter feed # Supported: arc, chrome, edge, firefox, brave, vivaldi, zen
221
266
  ```
222
267
 
223
268
  After loading cookies, the CLI performs lightweight verification. Commands that require account access fail fast on clear auth errors (`401/403`).
224
269
 
270
+ **Advanced GraphQL & Transaction env vars:**
271
+ - `TWITTER_QID_<OPERATION>=<queryId>` — override a specific GraphQL queryId (e.g. `TWITTER_QID_HomeTimeline=...`)
272
+ - `TWITTER_ALLOW_UNSIGNED=1` — opt-in bypass if `x-client-transaction-id` cannot be generated on write (`POST`) requests (fails closed by default to prevent 226 anti-bot flags)
273
+
225
274
  ### Proxy Support
226
275
 
227
276
  Set `TWITTER_PROXY` to route all requests through a proxy:
@@ -238,12 +287,15 @@ Using a proxy can help reduce IP-based rate limiting risks.
238
287
 
239
288
  ### Configuration
240
289
 
241
- Create `config.yaml` in your working directory:
290
+ Create `config.yaml` in your working directory or `~/.twitter-cli/config.yaml` (search order: explicit path → `./config.yaml` → `~/.twitter-cli/config.yaml` → built-in defaults):
242
291
 
243
292
  ```yaml
244
293
  fetch:
245
294
  count: 50
246
295
 
296
+ display:
297
+ time: "relative" # "relative" | "absolute" | "iso"
298
+
247
299
  filter:
248
300
  mode: "topN" # "topN" | "score" | "all"
249
301
  topN: 20
@@ -261,7 +313,7 @@ rateLimit:
261
313
  requestDelay: 2.5 # base delay between requests (randomized ×0.7–1.5)
262
314
  maxRetries: 3 # retry count on rate limit (429)
263
315
  retryBaseDelay: 5.0 # base delay for exponential backoff
264
- maxCount: 200 # hard cap on fetched items
316
+ maxCount: 200 # default pagination cap (explicit --max can exceed up to 10,000)
265
317
  ```
266
318
 
267
319
  Fetch behavior:
@@ -308,9 +360,9 @@ Mode behavior:
308
360
  ### Troubleshooting
309
361
 
310
362
  - `No Twitter cookies found`
311
- - Ensure you are logged in to `x.com` in a supported browser (Arc/Chrome/Edge/Firefox/Brave).
312
- - Or set `TWITTER_AUTH_TOKEN` and `TWITTER_CT0` manually.
313
- - Run with `-v` to see browser extraction diagnostics.
363
+ - Ensure you are logged in to `x.com` in a supported browser (Arc/Chrome/Edge/Firefox/Brave/Vivaldi/Zen) or point to a custom directory with `TWITTER_CHROMIUM_USER_DATA_DIR`.
364
+ - Or set `TWITTER_COOKIE_STRING` (recommended for headless/Windows Chrome >=127) or `TWITTER_AUTH_TOKEN` + `TWITTER_CT0` manually.
365
+ - Run with `-v` or `twitter doctor` to see diagnostics.
314
366
 
315
367
  - `Cookie expired or invalid (HTTP 401/403)`
316
368
  - Re-login to `x.com` and retry.
@@ -338,7 +390,7 @@ Mode behavior:
338
390
 
339
391
 
340
392
 
341
- Structured error codes commonly include `not_authenticated`, `not_found`, `invalid_input`, `rate_limited`, and `api_error`.
393
+ Structured error codes commonly include `not_authenticated`, `not_found`, `invalid_input`, `rate_limited`, `network_error`, `query_id_error`, `media_upload_error`, and `api_error`.
342
394
 
343
395
  ### Development
344
396
 
@@ -351,26 +403,31 @@ uv run ruff check .
351
403
  uv run pytest -q
352
404
  ```
353
405
 
354
- Current CI validates the project on Python 3.8, 3.10, and 3.12.
406
+ Current CI validates the project on Python 3.10, 3.11, 3.12, and 3.13.
355
407
 
356
408
  ### Project Structure
357
409
 
358
410
  ```text
359
411
  twitter_cli/
360
- ├── __init__.py
361
- ├── cli.py
362
- ├── client.py
363
- ├── graphql.py # GraphQL query IDs, URL building, JS bundle scanning
364
- ├── parser.py # Tweet, User, Media parsing logic
365
- ├── auth.py
366
- ├── config.py
412
+ ├── __init__.py # Public Python library exports
413
+ ├── article.py # Markdown -> Draft.js content_state builder for X Articles
414
+ ├── auth.py # Browser cookie extraction & env auth
415
+ ├── cache.py # Owner-only local cache helpers
416
+ ├── cli.py # Click CLI commands
417
+ ├── client.py # HTTP/GraphQL/media client & ClientTransaction bootstrap
418
+ ├── config.py # YAML configuration loader
367
419
  ├── constants.py
368
420
  ├── exceptions.py
369
421
  ├── filter.py
370
422
  ├── formatter.py
423
+ ├── graphql.py # GraphQL query IDs, disk cache, live JS bundle scanning
424
+ ├── models.py
371
425
  ├── output.py
426
+ ├── parser.py # Tweet, User, Media, Article Draft.js parsing
427
+ ├── search.py # Advanced search query builder
372
428
  ├── serialization.py
373
- └── models.py
429
+ ├── text.py # twitter-text v3 weighted character length
430
+ └── timeutil.py
374
431
  ```
375
432
 
376
433
  ### Use as AI Agent Skill
@@ -406,20 +463,24 @@ git clone git@github.com:jackwener/twitter-cli.git .agents/skills/twitter-cli
406
463
 
407
464
  **读取:**
408
465
  - 时间线读取:支持 `for-you` 和 `following`
409
- - 收藏读取:查看账号书签推文
410
- - 搜索:按关键词搜索推文,支持 Top/Latest/Photos/Videos
411
- - 推文详情:查看推文及其回复;用 `show <N>` 可直接打开上次列表里的第 N 条推文
412
- - 文章读取:获取 Twitter 长文,并导出为 Markdown
466
+ - 收藏读取:查看账号书签推文(支持 `--cursor` 翻页及书签文件夹)
467
+ - Explore 新闻:`twitter news` 获取个性化新闻摘要
468
+ - 搜索:按关键词搜索推文,支持 Top/Latest/Photos/Videos 与高级过滤
469
+ - 推文详情:查看推文及其回复(支持 `--cursor` 翻页);用 `show <N>` 可直接打开上次列表里的第 N 条推文
470
+ - 文章读取:获取 Twitter 长文(完整保留 Draft.js Markdown/链接/行内样式/嵌入推文),并导出为 Markdown
413
471
  - 列表时间线:获取 Twitter List 的推文
414
- - 用户查询:查看用户资料、推文、点赞、粉丝和关注
415
- - `--full-text`:在 rich table 输出里关闭推文正文截断
472
+ - 用户查询:支持 `@handle` 或纯数字 user ID 查询资料、推文、点赞、粉丝和关注
473
+ - 展示与工具:`--full-text` 关闭正文截断、`--time relative|absolute|iso` 控制时间格式、`completion bash|zsh|fish`、`doctor [--refresh]`
416
474
  - 结构化输出:支持 YAML 和 JSON,便于脚本处理和 AI agent 集成
475
+ - Python 库 API:可直接 `from twitter_cli import TwitterClient, get_cookies` 调用
417
476
 
418
477
  > **AI Agent 提示:** 需要结构化输出时优先使用 `--yaml`,除非下游必须是 JSON。stdout 不是 TTY 时默认输出 YAML。用 `--max` 控制返回数量。
419
478
 
420
479
  **写入:**
421
- - 发推:发布新推文和回复,支持附带图片(最多 4 张,支持 JPEG/PNG/GIF/WebP)
422
- - 引用推文:带评论的转发,也支持附带图片
480
+ - 发推:发布新推文和回复,支持附带图片(最多 4 张)或视频(`--video` / `-V`,MP4/MOV/WebM,最大 512 MB)
481
+ - 长推文自动适配:超过 280 加权字符时自动切换 `CreateNoteTweet`(需 X Premium)
482
+ - 长文发布:`twitter article-publish` 直接从 Markdown 发布 X Article 或保存草稿
483
+ - 引用推文:带评论的转发,支持附带图片或视频
423
484
  - 删除:删除自己的推文
424
485
  - 点赞 / 取消点赞
425
486
  - 转推 / 取消转推
@@ -427,12 +488,12 @@ git clone git@github.com:jackwener/twitter-cli.git .agents/skills/twitter-cli
427
488
  - 写操作现在也显式支持 `--json` / `--yaml`
428
489
 
429
490
  **认证与反风控:**
430
- - Cookie 认证:支持环境变量和浏览器自动提取
491
+ - Cookie 认证:支持环境变量(`TWITTER_COOKIE_STRING` 或 `TWITTER_AUTH_TOKEN` + `TWITTER_CT0`)和浏览器自动提取(Arc/Chrome/Edge/Firefox/Brave/Vivaldi/Zen 及自定义 Chromium 目录)
431
492
  - 完整 Cookie 转发:提取浏览器中所有 Twitter Cookie,保留更多浏览器上下文
432
493
  - TLS 指纹伪装:`curl_cffi` 动态匹配 Chrome 版本
433
- - `x-client-transaction-id` 请求头生成
434
- - 请求时序随机化(jitter)
435
- - 写操作随机延迟(1.5–4 秒),降低频率风控
494
+ - `x-client-transaction-id` 请求头生成,写操作缺失签名时默认 fail-closed 拦截以防 226 风控
495
+ - GraphQL `queryId` 24 小时本地缓存、实时 bundle 刷新与 `TWITTER_QID_<OP>` 覆盖
496
+ - 请求时序随机化(jitter)与写操作随机延迟(1.5–4 秒),降低频率风控
436
497
  - 代理支持:`TWITTER_PROXY` 环境变量
437
498
 
438
499
  ### 安装
@@ -459,11 +520,16 @@ twitter feed
459
520
  twitter feed -t following
460
521
  twitter feed --filter
461
522
  twitter feed --full-text
523
+ twitter feed --cursor "<上一页返回的 nextCursor>"
462
524
 
463
525
  # 收藏
464
526
  twitter bookmarks
527
+ twitter bookmarks --cursor "<上一页返回的 nextCursor>"
465
528
  twitter bookmarks --full-text
466
529
 
530
+ # Explore 新闻
531
+ twitter news --max 20
532
+
467
533
  # 搜索
468
534
  twitter search "Claude Code"
469
535
  twitter search "AI agent" -t Latest --max 50
@@ -473,6 +539,7 @@ twitter search "trending" --filter # 启用排序筛选
473
539
 
474
540
  # 推文详情
475
541
  twitter tweet 1234567890
542
+ twitter tweet 1234567890 --cursor "<nextCursor>"
476
543
  twitter tweet 1234567890 --full-text
477
544
 
478
545
  # 通过序号打开上次列表里的推文
@@ -480,18 +547,22 @@ twitter show 2 # 打开上次 feed/search 的第 2 条
480
547
  twitter show 2 --full-text # 在回复表格里显示完整正文
481
548
  twitter show 2 --json # 结构化输出
482
549
 
483
- # Twitter 长文
550
+ # Twitter 长文(读取与发布)
484
551
  twitter article 1234567890
485
552
  twitter article https://x.com/user/article/1234567890 --json
486
553
  twitter article 1234567890 --markdown
487
554
  twitter article 1234567890 --output article.md
555
+ twitter article-publish post.md --title "我的长文"
556
+ twitter article-publish post.md --title "草稿标题" --draft
488
557
 
489
558
  # 列表时间线
490
559
  twitter list 1539453138322673664
560
+ twitter list 1539453138322673664 --cursor "<上一页返回的 nextCursor>"
491
561
  twitter list 1539453138322673664 --full-text
492
562
 
493
- # 用户
563
+ # 用户(支持 @handle 或纯数字 ID)
494
564
  twitter user elonmusk
565
+ twitter user 44196397
495
566
  twitter user-posts elonmusk --max 20
496
567
  twitter user-posts elonmusk --full-text
497
568
  twitter user-posts elonmusk -o tweets.json
@@ -505,6 +576,7 @@ twitter following elonmusk
505
576
  twitter post "你好,世界!"
506
577
  twitter post "发图" --image photo.jpg # 带图发推
507
578
  twitter post "多图" -i a.png -i b.jpg -i c.webp # 最多 4 张图片
579
+ twitter post "发视频" --video clip.mp4 # 带视频发推(最大 512 MB)
508
580
  twitter post "回复内容" --reply-to 1234567890
509
581
  twitter reply 1234567890 "回复" -i screenshot.png # 带图回复
510
582
  twitter quote 1234567890 "评论" -i chart.png # 带图引用
@@ -518,14 +590,37 @@ twitter unretweet 1234567890
518
590
  twitter bookmark 1234567890
519
591
  twitter unbookmark 1234567890
520
592
  twitter follow elonmusk --json
593
+
594
+ # 展示、补全与诊断
595
+ twitter --time relative feed # relative | absolute | iso
596
+ twitter completion zsh # bash | zsh | fish
597
+ twitter doctor # 检查依赖、配置、认证状态与缓存
598
+ twitter doctor --refresh # 检查环境并刷新 GraphQL queryId 缓存
599
+ ```
600
+
601
+ ### Python 库调用
602
+
603
+ ```python
604
+ from twitter_cli import TwitterClient, get_cookies
605
+
606
+ cookies = get_cookies()
607
+ client = TwitterClient(
608
+ cookies["auth_token"],
609
+ cookies["ct0"],
610
+ cookie_string=cookies.get("cookie_string"),
611
+ )
612
+ tweets = client.fetch_home_timeline(count=20)
613
+ for tweet in tweets:
614
+ print(f"@{tweet.author.screen_name}: {tweet.text[:80]}")
521
615
  ```
522
616
 
523
617
  ### 认证说明
524
618
 
525
619
  认证优先级:
526
620
 
527
- 1. **环境变量**:`TWITTER_AUTH_TOKEN` + `TWITTER_CT0`
528
- 2. **浏览器提取**(推荐):Arc/Chrome/Edge/Firefox/Brave 全量 Cookie 提取
621
+ 1. **完整 Cookie 字符串**:`TWITTER_COOKIE_STRING`(完整 `Cookie:` 请求头,自动解析 `auth_token` + `ct0` 并保留全部会话 Cookie,适合云端/Windows Chrome >=127)
622
+ 2. **环境变量**:`TWITTER_AUTH_TOKEN` + `TWITTER_CT0`
623
+ 3. **浏览器提取**(本地推荐):Arc/Chrome/Edge/Firefox/Brave/Vivaldi/Zen 全量 Cookie 提取
529
624
 
530
625
  推荐使用浏览器提取方式,会转发所有 Twitter Cookie,并按本机运行环境生成语言和平台请求头;它比仅发送 `auth_token` + `ct0` 更接近普通浏览器流量,但不等于完整浏览器自动化。
531
626
 
@@ -535,12 +630,24 @@ twitter follow elonmusk --json
535
630
  TWITTER_CHROME_PROFILE="Profile 2" twitter feed
536
631
  ```
537
632
 
633
+ **自定义 Chromium 目录**:适用于 Chromium、Opera 或自定义数据目录:
634
+
635
+ ```bash
636
+ TWITTER_CHROMIUM_USER_DATA_DIR="/path/to/User Data" twitter feed
637
+ ```
638
+
538
639
  **浏览器优先级**:如果有多个浏览器,可通过 `TWITTER_BROWSER` 指定优先尝试的浏览器:
539
640
 
540
641
  ```bash
541
- TWITTER_BROWSER=chrome twitter feed # 支持: arc, chrome, edge, firefox, brave
642
+ TWITTER_BROWSER=chrome twitter feed # 支持: arc, chrome, edge, firefox, brave, vivaldi, zen
542
643
  ```
543
644
 
645
+ **高级 GraphQL 与签名环境变量**:
646
+ - `TWITTER_QID_<OPERATION>=<queryId>` — 运行时覆盖指定 GraphQL operation 的 queryId(例如 `TWITTER_QID_HomeTimeline=...`)
647
+ - `TWITTER_ALLOW_UNSIGNED=1` — 当写操作(`POST`)无法生成 `x-client-transaction-id` 时默认 fail-closed 拦截以防 226 风控,仅调试时可显式设此变量绕过
648
+
649
+ **配置文件**:查找顺序为显式路径 → `./config.yaml` → `~/.twitter-cli/config.yaml` → 内置默认配置。可在 `display.time` 设置默认时间格式(`relative` / `absolute` / `iso`),显式 `--max` 可突破默认 `rateLimit.maxCount`(200)直到 10,000 安全上限。结构化输出规范见 [SCHEMA.md](./SCHEMA.md)。
650
+
544
651
  ### 代理支持
545
652
 
546
653
  设置 `TWITTER_PROXY` 环境变量即可:
@@ -579,8 +686,8 @@ score = likes_w * likes
579
686
 
580
687
  ### 常见问题
581
688
 
582
- - 报错 `No Twitter cookies found`:请先登录 `x.com`,并确认浏览器为 Arc/Chrome/Edge/Firefox/Brave 之一,或手动设置环境变量。
583
- - 如需查看浏览器提取细节,可加 `-v` 打开诊断日志。
689
+ - 报错 `No Twitter cookies found`:请先登录 `x.com`,并确认浏览器为 Arc/Chrome/Edge/Firefox/Brave/Vivaldi/Zen 之一(或通过 `TWITTER_CHROMIUM_USER_DATA_DIR` 指定目录),或手动设置 `TWITTER_COOKIE_STRING` / `TWITTER_AUTH_TOKEN` + `TWITTER_CT0`。
690
+ - 如需查看浏览器提取细节或环境状态,可加 `-v` 或运行 `twitter doctor`。
584
691
  - 报错 `Cookie expired or invalid`:Cookie 过期,重新登录后重试。
585
692
  - 报错 `Unable to get key for cookie decryption`(macOS Keychain 问题):
586
693
  - **SSH 远程登录**:Keychain 默认锁定,需手动解锁:
@@ -599,7 +706,7 @@ score = likes_w * likes
599
706
 
600
707
 
601
708
 
602
- - 结构化错误码通常会区分 `not_authenticated`、`not_found`、`invalid_input`、`rate_limited`、`api_error`。
709
+ - 结构化错误码通常会区分 `not_authenticated`、`not_found`、`invalid_input`、`rate_limited`、`network_error`、`query_id_error`、`media_upload_error`、`api_error`。
603
710
 
604
711
  ### 使用建议(防封号)
605
712