twitter-cli 0.7.0__tar.gz → 0.7.1__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.7.1}/.github/workflows/ci.yml +4 -1
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/PKG-INFO +7 -4
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/README.md +5 -2
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/pyproject.toml +3 -3
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/test_cli.py +38 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/test_search.py +27 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/test_serialization.py +15 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/cli.py +17 -14
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/client.py +6 -1
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/graphql.py +4 -1
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/parser.py +4 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/search.py +54 -5
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/serialization.py +2 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/uv.lock +24 -463
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/.github/ISSUE_TEMPLATE/feature_request.yml +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/.github/workflows/publish-clawhub.yml +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/.github/workflows/publish.yml +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/.gitignore +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/LICENSE +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/SCHEMA.md +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/SKILL.md +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/config.yaml +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/conftest.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/fixtures/followers_page.json +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/fixtures/home_timeline.json +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/fixtures/list_timeline.json +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/fixtures/search_timeline.json +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/fixtures/tweet_detail.json +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/test_auth.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/test_client.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/test_config.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/test_config_normalization.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/test_filter.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/test_parser_fixtures.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/tests/test_smoke.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/__init__.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/auth.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/config.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/constants.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/exceptions.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/filter.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/formatter.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/twitter_cli/models.py +0 -0
- {twitter_cli-0.7.0 → twitter_cli-0.7.1}/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.7.
|
|
3
|
+
Version: 0.7.1
|
|
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.
|
|
7
|
+
version = "0.7.1"
|
|
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)
|
|
@@ -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")
|
|
@@ -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
|
|
@@ -566,8 +566,8 @@ def user_posts(ctx, screen_name, max_count, as_json, as_yaml, output_file, full_
|
|
|
566
566
|
multiple=True,
|
|
567
567
|
help="Exclude content type (retweets, replies, links). Repeatable.",
|
|
568
568
|
)
|
|
569
|
-
@click.option("--min-likes", type=
|
|
570
|
-
@click.option("--min-retweets", type=
|
|
569
|
+
@click.option("--min-likes", type=click.IntRange(min=0), default=None, help="Minimum number of likes.")
|
|
570
|
+
@click.option("--min-retweets", type=click.IntRange(min=0), default=None, help="Minimum number of retweets.")
|
|
571
571
|
@click.option("--max", "-n", "max_count", type=int, default=None, help="Max number of tweets to fetch.")
|
|
572
572
|
@structured_output_options
|
|
573
573
|
@click.option("--output", "-o", "output_file", type=str, default=None, help="Save tweets to JSON file.")
|
|
@@ -590,18 +590,21 @@ def search(ctx, query, product, from_user, to_user, lang, since, until, has, exc
|
|
|
590
590
|
"""
|
|
591
591
|
from .search import build_search_query
|
|
592
592
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
593
|
+
try:
|
|
594
|
+
composed_query = build_search_query(
|
|
595
|
+
query,
|
|
596
|
+
from_user=from_user,
|
|
597
|
+
to_user=to_user,
|
|
598
|
+
lang=lang,
|
|
599
|
+
since=since,
|
|
600
|
+
until=until,
|
|
601
|
+
has=list(has) if has else None,
|
|
602
|
+
exclude=list(exclude) if exclude else None,
|
|
603
|
+
min_likes=min_likes,
|
|
604
|
+
min_retweets=min_retweets,
|
|
605
|
+
)
|
|
606
|
+
except ValueError as exc:
|
|
607
|
+
raise click.UsageError(str(exc)) from exc
|
|
605
608
|
if not composed_query:
|
|
606
609
|
raise click.UsageError("Provide a QUERY or at least one advanced filter (e.g. --from, --lang).")
|
|
607
610
|
|
|
@@ -9,7 +9,7 @@ import os
|
|
|
9
9
|
import random
|
|
10
10
|
import time
|
|
11
11
|
import urllib.parse
|
|
12
|
-
from typing import Any, Callable,
|
|
12
|
+
from typing import TYPE_CHECKING, Any, Callable, cast
|
|
13
13
|
|
|
14
14
|
import bs4
|
|
15
15
|
from curl_cffi import requests as _cffi_requests
|
|
@@ -52,6 +52,11 @@ from .parser import (
|
|
|
52
52
|
parse_user_result,
|
|
53
53
|
)
|
|
54
54
|
|
|
55
|
+
if TYPE_CHECKING:
|
|
56
|
+
from typing import Dict, List, Optional, Set, Tuple # noqa: F401
|
|
57
|
+
|
|
58
|
+
from .models import Tweet # noqa: F401
|
|
59
|
+
|
|
55
60
|
logger = logging.getLogger(__name__)
|
|
56
61
|
|
|
57
62
|
# Shared curl_cffi session (single-threaded CLI)
|
|
@@ -10,7 +10,10 @@ import json
|
|
|
10
10
|
import logging
|
|
11
11
|
import re
|
|
12
12
|
import urllib.parse
|
|
13
|
-
from typing import
|
|
13
|
+
from typing import TYPE_CHECKING
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from typing import Any, Dict, Optional # noqa: F401
|
|
14
17
|
|
|
15
18
|
from .exceptions import QueryIdError
|
|
16
19
|
|
|
@@ -7,6 +7,10 @@ Converts raw GraphQL response JSON into domain model objects
|
|
|
7
7
|
from __future__ import annotations
|
|
8
8
|
|
|
9
9
|
import logging
|
|
10
|
+
from typing import TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from typing import Any, Callable, Dict, List, Optional, Tuple # noqa: F401
|
|
10
14
|
|
|
11
15
|
from .models import Author, Metrics, Tweet, TweetMedia, UserProfile
|
|
12
16
|
|
|
@@ -8,8 +8,43 @@ Reference: https://help.x.com/en/using-x/x-advanced-search
|
|
|
8
8
|
|
|
9
9
|
from __future__ import annotations
|
|
10
10
|
|
|
11
|
+
import re
|
|
12
|
+
from datetime import date
|
|
11
13
|
from typing import List, Optional, Sequence
|
|
12
14
|
|
|
15
|
+
_LANG_PATTERN = re.compile(r"^[A-Za-z][A-Za-z-]{1,14}$")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _normalize_handle(value: Optional[str]) -> Optional[str]:
|
|
19
|
+
if value is None:
|
|
20
|
+
return None
|
|
21
|
+
text = value.strip().lstrip("@")
|
|
22
|
+
return text or None
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _normalize_lang(value: Optional[str]) -> Optional[str]:
|
|
26
|
+
if value is None:
|
|
27
|
+
return None
|
|
28
|
+
text = value.strip().lower()
|
|
29
|
+
if not text:
|
|
30
|
+
return None
|
|
31
|
+
if not _LANG_PATTERN.match(text):
|
|
32
|
+
raise ValueError("--lang must be an ISO language code like en or zh-cn")
|
|
33
|
+
return text
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _normalize_date(flag_name: str, value: Optional[str]) -> Optional[str]:
|
|
37
|
+
if value is None:
|
|
38
|
+
return None
|
|
39
|
+
text = value.strip()
|
|
40
|
+
if not text:
|
|
41
|
+
return None
|
|
42
|
+
try:
|
|
43
|
+
date.fromisoformat(text)
|
|
44
|
+
except ValueError as exc:
|
|
45
|
+
raise ValueError("%s must be in YYYY-MM-DD format" % flag_name) from exc
|
|
46
|
+
return text
|
|
47
|
+
|
|
13
48
|
|
|
14
49
|
def build_search_query(
|
|
15
50
|
query: str = "",
|
|
@@ -44,14 +79,27 @@ def build_search_query(
|
|
|
44
79
|
Composed query string ready for the rawQuery API parameter.
|
|
45
80
|
"""
|
|
46
81
|
parts: List[str] = []
|
|
82
|
+
query_text = query.strip()
|
|
83
|
+
from_user = _normalize_handle(from_user)
|
|
84
|
+
to_user = _normalize_handle(to_user)
|
|
85
|
+
lang = _normalize_lang(lang)
|
|
86
|
+
since = _normalize_date("--since", since)
|
|
87
|
+
until = _normalize_date("--until", until)
|
|
88
|
+
|
|
89
|
+
if min_likes is not None and min_likes < 0:
|
|
90
|
+
raise ValueError("--min-likes must be greater than or equal to 0")
|
|
91
|
+
if min_retweets is not None and min_retweets < 0:
|
|
92
|
+
raise ValueError("--min-retweets must be greater than or equal to 0")
|
|
93
|
+
if since and until and since > until:
|
|
94
|
+
raise ValueError("--since must be on or before --until")
|
|
47
95
|
|
|
48
|
-
if
|
|
49
|
-
parts.append(
|
|
96
|
+
if query_text:
|
|
97
|
+
parts.append(query_text)
|
|
50
98
|
|
|
51
99
|
if from_user:
|
|
52
|
-
parts.append("from:%s" % from_user
|
|
100
|
+
parts.append("from:%s" % from_user)
|
|
53
101
|
if to_user:
|
|
54
|
-
parts.append("to:%s" % to_user
|
|
102
|
+
parts.append("to:%s" % to_user)
|
|
55
103
|
if lang:
|
|
56
104
|
parts.append("lang:%s" % lang)
|
|
57
105
|
if since:
|
|
@@ -60,9 +108,10 @@ def build_search_query(
|
|
|
60
108
|
parts.append("until:%s" % until)
|
|
61
109
|
if has:
|
|
62
110
|
for item in has:
|
|
63
|
-
parts.append("filter:%s" % item)
|
|
111
|
+
parts.append("filter:%s" % item.lower())
|
|
64
112
|
if exclude:
|
|
65
113
|
for item in exclude:
|
|
114
|
+
item = item.lower()
|
|
66
115
|
if item == "retweets":
|
|
67
116
|
parts.append("-filter:retweets")
|
|
68
117
|
elif item == "replies":
|
|
@@ -125,6 +125,8 @@ def tweet_from_dict(data: Dict[str, Any]) -> Tweet:
|
|
|
125
125
|
def tweets_from_json(raw: str) -> List[Tweet]:
|
|
126
126
|
"""Parse a JSON string into Tweet objects."""
|
|
127
127
|
payload = json.loads(raw)
|
|
128
|
+
if isinstance(payload, dict) and payload.get("ok") is True and isinstance(payload.get("data"), list):
|
|
129
|
+
payload = payload["data"]
|
|
128
130
|
if not isinstance(payload, list):
|
|
129
131
|
raise ValueError("Tweet JSON payload must be a list")
|
|
130
132
|
return [tweet_from_dict(item) for item in payload if isinstance(item, dict)]
|