twitter-cli 0.7.0__tar.gz → 0.8.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.
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/.github/workflows/ci.yml +4 -1
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/PKG-INFO +7 -4
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/README.md +5 -2
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/pyproject.toml +3 -3
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/test_cli.py +41 -3
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/test_client.py +143 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/test_search.py +27 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/test_serialization.py +15 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/cli.py +63 -25
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/client.py +117 -9
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/exceptions.py +4 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/graphql.py +4 -1
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/parser.py +4 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/search.py +54 -5
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/serialization.py +2 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/uv.lock +24 -463
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/.github/ISSUE_TEMPLATE/feature_request.yml +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/.github/workflows/publish-clawhub.yml +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/.github/workflows/publish.yml +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/.gitignore +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/LICENSE +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/SCHEMA.md +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/SKILL.md +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/config.yaml +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/conftest.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/fixtures/followers_page.json +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/fixtures/home_timeline.json +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/fixtures/list_timeline.json +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/fixtures/search_timeline.json +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/fixtures/tweet_detail.json +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/test_auth.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/test_config.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/test_config_normalization.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/test_filter.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/test_parser_fixtures.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/tests/test_smoke.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/__init__.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/auth.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/config.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/constants.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/filter.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/formatter.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/models.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.8.0}/twitter_cli/output.py +0 -0
|
@@ -11,7 +11,7 @@ jobs:
|
|
|
11
11
|
runs-on: ubuntu-latest
|
|
12
12
|
strategy:
|
|
13
13
|
matrix:
|
|
14
|
-
python-version: ["3.
|
|
14
|
+
python-version: ["3.10", "3.11", "3.12"]
|
|
15
15
|
steps:
|
|
16
16
|
- name: Checkout
|
|
17
17
|
uses: actions/checkout@v4
|
|
@@ -30,5 +30,8 @@ jobs:
|
|
|
30
30
|
- name: Lint
|
|
31
31
|
run: uv run ruff check .
|
|
32
32
|
|
|
33
|
+
- name: Type check
|
|
34
|
+
run: uv run mypy twitter_cli
|
|
35
|
+
|
|
33
36
|
- name: Test
|
|
34
37
|
run: uv run pytest -q
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: twitter-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.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
|
|
@@ -15,7 +15,7 @@ Classifier: Intended Audience :: Developers
|
|
|
15
15
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
16
16
|
Classifier: Programming Language :: Python :: 3
|
|
17
17
|
Classifier: Topic :: Utilities
|
|
18
|
-
Requires-Python: >=3.
|
|
18
|
+
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
|
|
@@ -34,7 +34,7 @@ Description-Content-Type: text/markdown
|
|
|
34
34
|
|
|
35
35
|
[](https://github.com/jackwener/twitter-cli/actions/workflows/ci.yml)
|
|
36
36
|
[](https://pypi.org/project/twitter-cli/)
|
|
37
|
-
[](https://pypi.org/project/twitter-cli/)
|
|
38
38
|
|
|
39
39
|
A terminal-first CLI for Twitter/X: read timelines, bookmarks, and user profiles without API keys.
|
|
40
40
|
|
|
@@ -129,8 +129,9 @@ twitter feed --filter
|
|
|
129
129
|
# Feed
|
|
130
130
|
twitter feed --max 50
|
|
131
131
|
twitter feed --full-text
|
|
132
|
-
twitter feed --
|
|
132
|
+
twitter feed --output tweets.json
|
|
133
133
|
twitter feed --input tweets.json
|
|
134
|
+
twitter feed --json # Structured stdout for scripts/agents
|
|
134
135
|
|
|
135
136
|
# Bookmarks
|
|
136
137
|
twitter bookmarks
|
|
@@ -142,6 +143,8 @@ twitter search "Claude Code"
|
|
|
142
143
|
twitter search "AI agent" -t Latest --max 50
|
|
143
144
|
twitter search "AI agent" --full-text
|
|
144
145
|
twitter search "机器学习" --yaml
|
|
146
|
+
twitter search "python" --from elonmusk --lang en --since 2026-01-01
|
|
147
|
+
twitter search --from bbc --exclude retweets --has links
|
|
145
148
|
twitter search "topic" -o results.json # Save to file
|
|
146
149
|
twitter search "trending" --filter # Apply ranking filter
|
|
147
150
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/jackwener/twitter-cli/actions/workflows/ci.yml)
|
|
4
4
|
[](https://pypi.org/project/twitter-cli/)
|
|
5
|
-
[](https://pypi.org/project/twitter-cli/)
|
|
6
6
|
|
|
7
7
|
A terminal-first CLI for Twitter/X: read timelines, bookmarks, and user profiles without API keys.
|
|
8
8
|
|
|
@@ -97,8 +97,9 @@ twitter feed --filter
|
|
|
97
97
|
# Feed
|
|
98
98
|
twitter feed --max 50
|
|
99
99
|
twitter feed --full-text
|
|
100
|
-
twitter feed --
|
|
100
|
+
twitter feed --output tweets.json
|
|
101
101
|
twitter feed --input tweets.json
|
|
102
|
+
twitter feed --json # Structured stdout for scripts/agents
|
|
102
103
|
|
|
103
104
|
# Bookmarks
|
|
104
105
|
twitter bookmarks
|
|
@@ -110,6 +111,8 @@ twitter search "Claude Code"
|
|
|
110
111
|
twitter search "AI agent" -t Latest --max 50
|
|
111
112
|
twitter search "AI agent" --full-text
|
|
112
113
|
twitter search "机器学习" --yaml
|
|
114
|
+
twitter search "python" --from elonmusk --lang en --since 2026-01-01
|
|
115
|
+
twitter search --from bbc --exclude retweets --has links
|
|
113
116
|
twitter search "topic" -o results.json # Save to file
|
|
114
117
|
twitter search "trending" --filter # Apply ranking filter
|
|
115
118
|
|
|
@@ -4,11 +4,11 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "twitter-cli"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.8.0"
|
|
8
8
|
description = "A CLI for Twitter/X — feed, bookmarks, and user timeline in terminal"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|
|
11
|
-
requires-python = ">=3.
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
12
|
authors = [{ name = "jackwener", email = "jakevingoo@gmail.com" }]
|
|
13
13
|
keywords = ["twitter", "x", "cli", "feed", "timeline"]
|
|
14
14
|
classifiers = [
|
|
@@ -57,7 +57,7 @@ markers = [
|
|
|
57
57
|
line-length = 100
|
|
58
58
|
|
|
59
59
|
[tool.mypy]
|
|
60
|
-
python_version = "3.
|
|
60
|
+
python_version = "3.10"
|
|
61
61
|
ignore_missing_imports = true
|
|
62
62
|
check_untyped_defs = true
|
|
63
63
|
warn_unused_ignores = true
|
|
@@ -44,6 +44,26 @@ def test_cli_feed_json_input_path(tmp_path, tweet_factory) -> None:
|
|
|
44
44
|
assert '"id": "1"' in result.output
|
|
45
45
|
|
|
46
46
|
|
|
47
|
+
def test_cli_feed_input_accepts_structured_json_envelope(tmp_path, tweet_factory) -> None:
|
|
48
|
+
json_path = tmp_path / "tweets.json"
|
|
49
|
+
json_path.write_text(
|
|
50
|
+
(
|
|
51
|
+
"{\n"
|
|
52
|
+
' "ok": true,\n'
|
|
53
|
+
' "schema_version": "1",\n'
|
|
54
|
+
' "data": %s\n'
|
|
55
|
+
"}\n"
|
|
56
|
+
)
|
|
57
|
+
% tweets_to_json([tweet_factory("1")]),
|
|
58
|
+
encoding="utf-8",
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
runner = CliRunner()
|
|
62
|
+
result = runner.invoke(cli, ["feed", "--input", str(json_path), "--json"])
|
|
63
|
+
assert result.exit_code == 0
|
|
64
|
+
assert '"id": "1"' in result.output
|
|
65
|
+
|
|
66
|
+
|
|
47
67
|
def test_print_tweet_table_truncates_text_by_default(tweet_factory) -> None:
|
|
48
68
|
long_text = "A" * 140
|
|
49
69
|
console = Console(record=True, width=400)
|
|
@@ -301,7 +321,7 @@ def test_cli_reply_command(monkeypatch) -> None:
|
|
|
301
321
|
calls = []
|
|
302
322
|
|
|
303
323
|
class FakeClient:
|
|
304
|
-
def create_tweet(self, text: str, reply_to_id=None) -> str:
|
|
324
|
+
def create_tweet(self, text: str, reply_to_id=None, media_ids=None) -> str:
|
|
305
325
|
calls.append({"text": text, "reply_to_id": reply_to_id})
|
|
306
326
|
return "999"
|
|
307
327
|
|
|
@@ -318,7 +338,7 @@ def test_cli_quote_command(monkeypatch) -> None:
|
|
|
318
338
|
calls = []
|
|
319
339
|
|
|
320
340
|
class FakeClient:
|
|
321
|
-
def quote_tweet(self, tweet_id: str, text: str) -> str:
|
|
341
|
+
def quote_tweet(self, tweet_id: str, text: str, media_ids=None) -> str:
|
|
322
342
|
calls.append({"tweet_id": tweet_id, "text": text})
|
|
323
343
|
return "888"
|
|
324
344
|
|
|
@@ -333,7 +353,7 @@ def test_cli_quote_command(monkeypatch) -> None:
|
|
|
333
353
|
|
|
334
354
|
def test_cli_post_json_output(monkeypatch) -> None:
|
|
335
355
|
class FakeClient:
|
|
336
|
-
def create_tweet(self, text: str, reply_to_id=None) -> str:
|
|
356
|
+
def create_tweet(self, text: str, reply_to_id=None, media_ids=None) -> str:
|
|
337
357
|
assert text == "hello"
|
|
338
358
|
assert reply_to_id is None
|
|
339
359
|
return "999"
|
|
@@ -489,6 +509,24 @@ def test_cli_search_empty_query_no_options() -> None:
|
|
|
489
509
|
assert "Provide a QUERY" in result.output
|
|
490
510
|
|
|
491
511
|
|
|
512
|
+
def test_cli_search_invalid_date_rejected(monkeypatch) -> None:
|
|
513
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: None)
|
|
514
|
+
runner = CliRunner()
|
|
515
|
+
|
|
516
|
+
result = runner.invoke(cli, ["search", "python", "--since", "not-a-date"])
|
|
517
|
+
assert result.exit_code != 0
|
|
518
|
+
assert "--since must be in YYYY-MM-DD format" in result.output
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
def test_cli_search_rejects_reversed_date_range(monkeypatch) -> None:
|
|
522
|
+
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: None)
|
|
523
|
+
runner = CliRunner()
|
|
524
|
+
|
|
525
|
+
result = runner.invoke(cli, ["search", "python", "--since", "2026-03-02", "--until", "2026-03-01"])
|
|
526
|
+
assert result.exit_code != 0
|
|
527
|
+
assert "--since must be on or before --until" in result.output
|
|
528
|
+
|
|
529
|
+
|
|
492
530
|
def test_cli_compact_mode(tmp_path, tweet_factory) -> None:
|
|
493
531
|
json_path = tmp_path / "tweets.json"
|
|
494
532
|
json_path.write_text(tweets_to_json([tweet_factory("1")]), encoding="utf-8")
|
|
@@ -9,6 +9,8 @@ from __future__ import annotations
|
|
|
9
9
|
import copy
|
|
10
10
|
from unittest.mock import MagicMock, patch
|
|
11
11
|
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
12
14
|
|
|
13
15
|
from twitter_cli.client import (
|
|
14
16
|
FEATURES,
|
|
@@ -540,3 +542,144 @@ class TestParseUserResult:
|
|
|
540
542
|
assert user.following_count == 56
|
|
541
543
|
assert user.tweets_count == 78
|
|
542
544
|
assert user.likes_count == 0
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
# ── upload_media ─────────────────────────────────────────────────────────
|
|
548
|
+
|
|
549
|
+
class TestUploadMedia:
|
|
550
|
+
"""Tests for TwitterClient.upload_media()."""
|
|
551
|
+
|
|
552
|
+
def _make_client(self):
|
|
553
|
+
client = TwitterClient.__new__(TwitterClient)
|
|
554
|
+
client._auth_token = "tok"
|
|
555
|
+
client._ct0 = "ct0"
|
|
556
|
+
client._cookie_string = None
|
|
557
|
+
client._request_delay = 0
|
|
558
|
+
client._max_retries = 3
|
|
559
|
+
client._retry_base_delay = 5.0
|
|
560
|
+
client._max_count = 200
|
|
561
|
+
client._client_transaction = None
|
|
562
|
+
client._ct_init_attempted = True
|
|
563
|
+
return client
|
|
564
|
+
|
|
565
|
+
@patch("twitter_cli.client._get_cffi_session")
|
|
566
|
+
def test_upload_media_init_append_finalize(self, mock_session, tmp_path):
|
|
567
|
+
"""Happy path: INIT → APPEND → FINALIZE returns media_id."""
|
|
568
|
+
img = tmp_path / "photo.jpg"
|
|
569
|
+
img.write_bytes(b"\xff\xd8\xff\xe0" + b"\x00" * 100) # fake JPEG
|
|
570
|
+
|
|
571
|
+
mock_resp_init = MagicMock()
|
|
572
|
+
mock_resp_init.status_code = 200
|
|
573
|
+
mock_resp_init.text = '{"media_id_string": "12345"}'
|
|
574
|
+
|
|
575
|
+
mock_resp_append = MagicMock()
|
|
576
|
+
mock_resp_append.status_code = 200
|
|
577
|
+
mock_resp_append.text = ""
|
|
578
|
+
|
|
579
|
+
mock_resp_finalize = MagicMock()
|
|
580
|
+
mock_resp_finalize.status_code = 200
|
|
581
|
+
mock_resp_finalize.text = '{"media_id_string": "12345"}'
|
|
582
|
+
|
|
583
|
+
sess = MagicMock()
|
|
584
|
+
sess.post = MagicMock(side_effect=[mock_resp_init, mock_resp_append, mock_resp_finalize])
|
|
585
|
+
mock_session.return_value = sess
|
|
586
|
+
|
|
587
|
+
client = self._make_client()
|
|
588
|
+
media_id = client.upload_media(str(img))
|
|
589
|
+
|
|
590
|
+
assert media_id == "12345"
|
|
591
|
+
assert sess.post.call_count == 3
|
|
592
|
+
|
|
593
|
+
def test_upload_media_file_not_found(self):
|
|
594
|
+
from twitter_cli.exceptions import MediaUploadError
|
|
595
|
+
|
|
596
|
+
client = self._make_client()
|
|
597
|
+
with pytest.raises(MediaUploadError, match="File not found"):
|
|
598
|
+
client.upload_media("/nonexistent/file.jpg")
|
|
599
|
+
|
|
600
|
+
def test_upload_media_too_large(self, tmp_path):
|
|
601
|
+
from twitter_cli.exceptions import MediaUploadError
|
|
602
|
+
|
|
603
|
+
img = tmp_path / "big.jpg"
|
|
604
|
+
img.write_bytes(b"\xff\xd8\xff\xe0" + b"\x00" * (6 * 1024 * 1024)) # 6 MB
|
|
605
|
+
|
|
606
|
+
client = self._make_client()
|
|
607
|
+
with pytest.raises(MediaUploadError, match="File too large"):
|
|
608
|
+
client.upload_media(str(img))
|
|
609
|
+
|
|
610
|
+
def test_upload_media_unsupported_format(self, tmp_path):
|
|
611
|
+
from twitter_cli.exceptions import MediaUploadError
|
|
612
|
+
|
|
613
|
+
txt = tmp_path / "notes.txt"
|
|
614
|
+
txt.write_text("hello")
|
|
615
|
+
|
|
616
|
+
client = self._make_client()
|
|
617
|
+
with pytest.raises(MediaUploadError, match="Unsupported image format"):
|
|
618
|
+
client.upload_media(str(txt))
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
# ── create_tweet with media_ids ──────────────────────────────────────────
|
|
622
|
+
|
|
623
|
+
class TestCreateTweetWithMedia:
|
|
624
|
+
"""Tests that media_ids are correctly passed into CreateTweet variables."""
|
|
625
|
+
|
|
626
|
+
@patch("twitter_cli.client._get_cffi_session")
|
|
627
|
+
def test_create_tweet_with_media_ids(self, mock_session):
|
|
628
|
+
sess = MagicMock()
|
|
629
|
+
mock_session.return_value = sess
|
|
630
|
+
|
|
631
|
+
client = TwitterClient.__new__(TwitterClient)
|
|
632
|
+
client._auth_token = "tok"
|
|
633
|
+
client._ct0 = "ct0"
|
|
634
|
+
client._cookie_string = None
|
|
635
|
+
client._request_delay = 0
|
|
636
|
+
client._max_retries = 0
|
|
637
|
+
client._retry_base_delay = 0
|
|
638
|
+
client._max_count = 200
|
|
639
|
+
client._client_transaction = None
|
|
640
|
+
client._ct_init_attempted = True
|
|
641
|
+
|
|
642
|
+
captured_body = {}
|
|
643
|
+
|
|
644
|
+
def mock_graphql_post(operation_name, variables, features=None):
|
|
645
|
+
captured_body.update(variables)
|
|
646
|
+
return {"data": {"create_tweet": {"tweet_results": {"result": {"rest_id": "99"}}}}}
|
|
647
|
+
|
|
648
|
+
client._graphql_post = mock_graphql_post
|
|
649
|
+
|
|
650
|
+
result = client.create_tweet("test", media_ids=["111", "222"])
|
|
651
|
+
assert result == "99"
|
|
652
|
+
|
|
653
|
+
entities = captured_body["media"]["media_entities"]
|
|
654
|
+
assert len(entities) == 2
|
|
655
|
+
assert entities[0]["media_id"] == "111"
|
|
656
|
+
assert entities[1]["media_id"] == "222"
|
|
657
|
+
|
|
658
|
+
@patch("twitter_cli.client._get_cffi_session")
|
|
659
|
+
def test_create_tweet_without_media_ids(self, mock_session):
|
|
660
|
+
sess = MagicMock()
|
|
661
|
+
mock_session.return_value = sess
|
|
662
|
+
|
|
663
|
+
client = TwitterClient.__new__(TwitterClient)
|
|
664
|
+
client._auth_token = "tok"
|
|
665
|
+
client._ct0 = "ct0"
|
|
666
|
+
client._cookie_string = None
|
|
667
|
+
client._request_delay = 0
|
|
668
|
+
client._max_retries = 0
|
|
669
|
+
client._retry_base_delay = 0
|
|
670
|
+
client._max_count = 200
|
|
671
|
+
client._client_transaction = None
|
|
672
|
+
client._ct_init_attempted = True
|
|
673
|
+
|
|
674
|
+
captured_body = {}
|
|
675
|
+
|
|
676
|
+
def mock_graphql_post(operation_name, variables, features=None):
|
|
677
|
+
captured_body.update(variables)
|
|
678
|
+
return {"data": {"create_tweet": {"tweet_results": {"result": {"rest_id": "88"}}}}}
|
|
679
|
+
|
|
680
|
+
client._graphql_post = mock_graphql_post
|
|
681
|
+
|
|
682
|
+
result = client.create_tweet("no media")
|
|
683
|
+
assert result == "88"
|
|
684
|
+
assert captured_body["media"]["media_entities"] == []
|
|
685
|
+
|
|
@@ -24,6 +24,9 @@ class TestBuildSearchQuery:
|
|
|
24
24
|
def test_lang(self) -> None:
|
|
25
25
|
assert build_search_query("news", lang="fr") == "news lang:fr"
|
|
26
26
|
|
|
27
|
+
def test_lang_is_trimmed_and_lowercased(self) -> None:
|
|
28
|
+
assert build_search_query("news", lang=" EN ") == "news lang:en"
|
|
29
|
+
|
|
27
30
|
def test_since(self) -> None:
|
|
28
31
|
assert build_search_query("python", since="2026-01-01") == "python since:2026-01-01"
|
|
29
32
|
|
|
@@ -76,6 +79,30 @@ class TestBuildSearchQuery:
|
|
|
76
79
|
result = build_search_query("", from_user="elonmusk", since="2026-03-01")
|
|
77
80
|
assert result == "from:elonmusk since:2026-03-01"
|
|
78
81
|
|
|
82
|
+
def test_date_range_rejects_reversed_order(self) -> None:
|
|
83
|
+
try:
|
|
84
|
+
build_search_query("python", since="2026-03-02", until="2026-03-01")
|
|
85
|
+
except ValueError as exc:
|
|
86
|
+
assert "--since must be on or before --until" in str(exc)
|
|
87
|
+
else:
|
|
88
|
+
raise AssertionError("Expected ValueError for reversed date range")
|
|
89
|
+
|
|
90
|
+
def test_invalid_since_rejected(self) -> None:
|
|
91
|
+
try:
|
|
92
|
+
build_search_query("python", since="not-a-date")
|
|
93
|
+
except ValueError as exc:
|
|
94
|
+
assert "--since must be in YYYY-MM-DD format" in str(exc)
|
|
95
|
+
else:
|
|
96
|
+
raise AssertionError("Expected ValueError for invalid since date")
|
|
97
|
+
|
|
98
|
+
def test_negative_min_likes_rejected(self) -> None:
|
|
99
|
+
try:
|
|
100
|
+
build_search_query("python", min_likes=-1)
|
|
101
|
+
except ValueError as exc:
|
|
102
|
+
assert "--min-likes must be greater than or equal to 0" in str(exc)
|
|
103
|
+
else:
|
|
104
|
+
raise AssertionError("Expected ValueError for negative min_likes")
|
|
105
|
+
|
|
79
106
|
def test_whitespace_query_trimmed(self) -> None:
|
|
80
107
|
assert build_search_query(" python ", lang="en") == "python lang:en"
|
|
81
108
|
|
|
@@ -22,6 +22,21 @@ def test_tweets_json_roundtrip(tweet_factory) -> None:
|
|
|
22
22
|
assert restored[1].lang == "zh"
|
|
23
23
|
|
|
24
24
|
|
|
25
|
+
def test_tweets_from_json_accepts_structured_success_envelope(tweet_factory) -> None:
|
|
26
|
+
tweets = [tweet_factory("1")]
|
|
27
|
+
raw = (
|
|
28
|
+
"{\n"
|
|
29
|
+
' "ok": true,\n'
|
|
30
|
+
' "schema_version": "1",\n'
|
|
31
|
+
' "data": %s\n'
|
|
32
|
+
"}\n"
|
|
33
|
+
) % tweets_to_json(tweets)
|
|
34
|
+
|
|
35
|
+
restored = tweets_from_json(raw)
|
|
36
|
+
|
|
37
|
+
assert [tweet.id for tweet in restored] == ["1"]
|
|
38
|
+
|
|
39
|
+
|
|
25
40
|
def test_compact_serialization(tweet_factory) -> None:
|
|
26
41
|
from twitter_cli.serialization import tweet_to_compact_dict, tweets_to_compact_json
|
|
27
42
|
import json
|
|
@@ -18,6 +18,7 @@ Read commands:
|
|
|
18
18
|
|
|
19
19
|
Write commands:
|
|
20
20
|
twitter post "text" # post a tweet
|
|
21
|
+
twitter post "text" -i photo.jpg # post with image(s)
|
|
21
22
|
twitter reply <id> "text" # reply to a tweet
|
|
22
23
|
twitter quote <id> "text" # quote-tweet
|
|
23
24
|
twitter delete <id> # delete a tweet
|
|
@@ -566,8 +567,8 @@ def user_posts(ctx, screen_name, max_count, as_json, as_yaml, output_file, full_
|
|
|
566
567
|
multiple=True,
|
|
567
568
|
help="Exclude content type (retweets, replies, links). Repeatable.",
|
|
568
569
|
)
|
|
569
|
-
@click.option("--min-likes", type=
|
|
570
|
-
@click.option("--min-retweets", type=
|
|
570
|
+
@click.option("--min-likes", type=click.IntRange(min=0), default=None, help="Minimum number of likes.")
|
|
571
|
+
@click.option("--min-retweets", type=click.IntRange(min=0), default=None, help="Minimum number of retweets.")
|
|
571
572
|
@click.option("--max", "-n", "max_count", type=int, default=None, help="Max number of tweets to fetch.")
|
|
572
573
|
@structured_output_options
|
|
573
574
|
@click.option("--output", "-o", "output_file", type=str, default=None, help="Save tweets to JSON file.")
|
|
@@ -590,18 +591,21 @@ def search(ctx, query, product, from_user, to_user, lang, since, until, has, exc
|
|
|
590
591
|
"""
|
|
591
592
|
from .search import build_search_query
|
|
592
593
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
594
|
+
try:
|
|
595
|
+
composed_query = build_search_query(
|
|
596
|
+
query,
|
|
597
|
+
from_user=from_user,
|
|
598
|
+
to_user=to_user,
|
|
599
|
+
lang=lang,
|
|
600
|
+
since=since,
|
|
601
|
+
until=until,
|
|
602
|
+
has=list(has) if has else None,
|
|
603
|
+
exclude=list(exclude) if exclude else None,
|
|
604
|
+
min_likes=min_likes,
|
|
605
|
+
min_retweets=min_retweets,
|
|
606
|
+
)
|
|
607
|
+
except ValueError as exc:
|
|
608
|
+
raise click.UsageError(str(exc)) from exc
|
|
605
609
|
if not composed_query:
|
|
606
610
|
raise click.UsageError("Provide a QUERY or at least one advanced filter (e.g. --from, --lang).")
|
|
607
611
|
|
|
@@ -846,6 +850,24 @@ def following(screen_name, max_count, as_json, as_yaml):
|
|
|
846
850
|
|
|
847
851
|
# ── Write commands ──────────────────────────────────────────────────────
|
|
848
852
|
|
|
853
|
+
_MAX_IMAGES = 4 # Twitter allows up to 4 images per tweet
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
def _upload_images(client, image_paths, rich_output=True):
|
|
857
|
+
# type: (TwitterClient, tuple, bool) -> list
|
|
858
|
+
"""Upload images and return list of media_id strings."""
|
|
859
|
+
if not image_paths:
|
|
860
|
+
return []
|
|
861
|
+
if len(image_paths) > _MAX_IMAGES:
|
|
862
|
+
raise click.UsageError("Too many images: max %d, got %d" % (_MAX_IMAGES, len(image_paths)))
|
|
863
|
+
media_ids = []
|
|
864
|
+
for i, path in enumerate(image_paths, 1):
|
|
865
|
+
if rich_output:
|
|
866
|
+
console.print("📤 Uploading image %d/%d: %s" % (i, len(image_paths), path))
|
|
867
|
+
media_ids.append(client.upload_media(path))
|
|
868
|
+
return media_ids
|
|
869
|
+
|
|
870
|
+
|
|
849
871
|
def _write_action(emoji, action_desc, client_method, tweet_id, as_json=False, as_yaml=False):
|
|
850
872
|
# type: (str, str, str, str, bool, bool) -> None
|
|
851
873
|
"""Generic write action helper to reduce CLI command boilerplate.
|
|
@@ -871,14 +893,24 @@ def _write_action(emoji, action_desc, client_method, tweet_id, as_json=False, as
|
|
|
871
893
|
@cli.command()
|
|
872
894
|
@click.argument("text")
|
|
873
895
|
@click.option("--reply-to", "-r", default=None, help="Reply to this tweet ID.")
|
|
896
|
+
@click.option("--image", "-i", "images", multiple=True, type=click.Path(exists=True), help="Attach image (up to 4). Repeatable.")
|
|
874
897
|
@structured_output_options
|
|
875
|
-
def post(text, reply_to, as_json, as_yaml):
|
|
876
|
-
# type: (str, Optional[str], bool, bool) -> None
|
|
877
|
-
"""Post a new tweet. TEXT is the tweet content.
|
|
898
|
+
def post(text, reply_to, images, as_json, as_yaml):
|
|
899
|
+
# type: (str, Optional[str], tuple, bool, bool) -> None
|
|
900
|
+
"""Post a new tweet. TEXT is the tweet content.
|
|
901
|
+
|
|
902
|
+
Attach images with --image / -i (up to 4):
|
|
903
|
+
|
|
904
|
+
\b
|
|
905
|
+
twitter post "Hello!" --image photo.jpg
|
|
906
|
+
twitter post "Gallery" -i a.png -i b.png -i c.jpg
|
|
907
|
+
"""
|
|
878
908
|
action = "Replying to %s" % reply_to if reply_to else "Posting tweet"
|
|
909
|
+
rich_output = not _structured_mode(as_json=as_json, as_yaml=as_yaml)
|
|
879
910
|
|
|
880
911
|
def operation(client: TwitterClient) -> WritePayload:
|
|
881
|
-
|
|
912
|
+
media_ids = _upload_images(client, images, rich_output=rich_output)
|
|
913
|
+
tweet_id = client.create_tweet(text, reply_to_id=reply_to, media_ids=media_ids or None)
|
|
882
914
|
return {"success": True, "action": "post", "id": tweet_id, "url": "https://x.com/i/status/%s" % tweet_id}
|
|
883
915
|
|
|
884
916
|
payload = _run_write_command(
|
|
@@ -896,13 +928,16 @@ def post(text, reply_to, as_json, as_yaml):
|
|
|
896
928
|
@cli.command(name="reply")
|
|
897
929
|
@click.argument("tweet_id")
|
|
898
930
|
@click.argument("text")
|
|
931
|
+
@click.option("--image", "-i", "images", multiple=True, type=click.Path(exists=True), help="Attach image (up to 4). Repeatable.")
|
|
899
932
|
@structured_output_options
|
|
900
|
-
def reply_tweet(tweet_id, text, as_json, as_yaml):
|
|
901
|
-
# type: (str, str, bool, bool) -> None
|
|
933
|
+
def reply_tweet(tweet_id, text, images, as_json, as_yaml):
|
|
934
|
+
# type: (str, str, tuple, bool, bool) -> None
|
|
902
935
|
"""Reply to a tweet. TWEET_ID is the tweet to reply to, TEXT is the reply content."""
|
|
903
936
|
tweet_id = _normalize_tweet_id(tweet_id)
|
|
937
|
+
rich_output = not _structured_mode(as_json=as_json, as_yaml=as_yaml)
|
|
904
938
|
def operation(client: TwitterClient) -> WritePayload:
|
|
905
|
-
|
|
939
|
+
media_ids = _upload_images(client, images, rich_output=rich_output)
|
|
940
|
+
new_id = client.create_tweet(text, reply_to_id=tweet_id, media_ids=media_ids or None)
|
|
906
941
|
return {
|
|
907
942
|
"success": True,
|
|
908
943
|
"action": "reply",
|
|
@@ -926,13 +961,16 @@ def reply_tweet(tweet_id, text, as_json, as_yaml):
|
|
|
926
961
|
@cli.command(name="quote")
|
|
927
962
|
@click.argument("tweet_id")
|
|
928
963
|
@click.argument("text")
|
|
964
|
+
@click.option("--image", "-i", "images", multiple=True, type=click.Path(exists=True), help="Attach image (up to 4). Repeatable.")
|
|
929
965
|
@structured_output_options
|
|
930
|
-
def quote_tweet(tweet_id, text, as_json, as_yaml):
|
|
931
|
-
# type: (str, str, bool, bool) -> None
|
|
966
|
+
def quote_tweet(tweet_id, text, images, as_json, as_yaml):
|
|
967
|
+
# type: (str, str, tuple, bool, bool) -> None
|
|
932
968
|
"""Quote-tweet a tweet. TWEET_ID is the tweet to quote, TEXT is the commentary."""
|
|
933
969
|
tweet_id = _normalize_tweet_id(tweet_id)
|
|
970
|
+
rich_output = not _structured_mode(as_json=as_json, as_yaml=as_yaml)
|
|
934
971
|
def operation(client: TwitterClient) -> WritePayload:
|
|
935
|
-
|
|
972
|
+
media_ids = _upload_images(client, images, rich_output=rich_output)
|
|
973
|
+
new_id = client.quote_tweet(tweet_id, text, media_ids=media_ids or None)
|
|
936
974
|
return {
|
|
937
975
|
"success": True,
|
|
938
976
|
"action": "quote",
|
|
@@ -950,7 +988,7 @@ def quote_tweet(tweet_id, text, as_json, as_yaml):
|
|
|
950
988
|
error_details={"action": "quote", "quotedId": tweet_id},
|
|
951
989
|
)
|
|
952
990
|
if payload and not _structured_mode(as_json=as_json, as_yaml=as_yaml):
|
|
953
|
-
console.print("🔗 %s" % payload["url"])
|
|
991
|
+
console.print("🔗 %s" % payload["url"])
|
|
954
992
|
|
|
955
993
|
|
|
956
994
|
@cli.command(name="status")
|