Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ twitter article 1234567890 --output article.md

# List timeline
twitter list 1539453138322673664
twitter list 1539453138322673664 --cursor "<next-cursor-from-previous-response>"
twitter list 1539453138322673664 --full-text

# User
Expand Down Expand Up @@ -458,6 +459,7 @@ twitter article 1234567890 --output article.md

# 列表时间线
twitter list 1539453138322673664
twitter list 1539453138322673664 --cursor "<上一页返回的 nextCursor>"
twitter list 1539453138322673664 --full-text

# 用户
Expand Down
1 change: 1 addition & 0 deletions SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ twitter show 2 # Open tweet #2 from last feed/search lis
twitter show 2 --full-text # Full text in reply table
twitter show 2 --json # Structured output
twitter list 1539453138322673664 # List timeline
twitter list 1539453138322673664 --cursor "<next-cursor>"
twitter list 1539453138322673664 --full-text
twitter user-posts elonmusk --max 20 # User's tweets
twitter user-posts elonmusk --full-text
Expand Down
31 changes: 31 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,37 @@ def fetch_following_feed(
assert payload["pagination"]["nextCursor"] == "cursor-next"


def test_cli_list_accepts_cursor_and_emits_pagination(monkeypatch, tweet_factory) -> None:
class FakeClient:
def fetch_list_timeline(
self,
list_id: str,
count: int,
cursor: str | None = None,
return_cursor: bool = False,
):
assert list_id == "123"
assert count == 20
assert cursor == "cursor-prev"
assert return_cursor is True
return [tweet_factory("1")], "cursor-next"

monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None, quiet=False: FakeClient())
monkeypatch.setattr(
"twitter_cli.cli.load_config",
lambda: {"fetch": {"count": 20}, "filter": {}, "rateLimit": {}},
)
runner = CliRunner()

result = runner.invoke(cli, ["list", "123", "--cursor", "cursor-prev", "--json"])

assert result.exit_code == 0
payload = json.loads(result.output)
assert payload["ok"] is True
assert payload["data"][0]["id"] == "1"
assert payload["pagination"]["nextCursor"] == "cursor-next"


def test_print_tweet_table_truncates_text_by_default(tweet_factory) -> None:
long_text = "A" * 140
console = Console(record=True, width=400)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,34 @@ def _graphql_get(operation_name, variables, features, field_toggles=None):
assert cursor == "cursor-next"
assert calls[0]["cursor"] == "cursor-prev"

def test_fetch_list_timeline_accepts_cursor_and_returns_cursor(self):
client = TwitterClient.__new__(TwitterClient)
client._request_delay = 0.0
client._max_count = 200

calls = []

def _graphql_get(operation_name, variables, features, field_toggles=None):
calls.append((operation_name, variables.copy()))
return {"page": 1}

client._graphql_get = _graphql_get

tweet = MagicMock(id="tweet-1")
with patch('twitter_cli.client.parse_timeline_response', return_value=([tweet], "cursor-next")):
tweets, cursor = client.fetch_list_timeline(
"list-1",
1,
cursor="cursor-prev",
return_cursor=True,
)

assert [item.id for item in tweets] == ["tweet-1"]
assert cursor == "cursor-next"
assert calls[0][0] == "ListLatestTweetsTimeline"
assert calls[0][1]["listId"] == "list-1"
assert calls[0][1]["cursor"] == "cursor-prev"

def test_user_list_continues_when_cursor_advances_without_new_users(self):
client = TwitterClient.__new__(TwitterClient)
client._request_delay = 0.0
Expand Down
47 changes: 41 additions & 6 deletions twitter_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,22 +1008,57 @@ def article(ctx, tweet_id, as_json, as_yaml, as_markdown, output_file):
@cli.command(name="list")
@click.argument("list_id")
@click.option("--max", "-n", "max_count", type=int, default=None, help="Max tweets to fetch.")
@click.option("--cursor", type=str, default=None, help="Pagination cursor for continuing a previous list request.")
@structured_output_options
@click.option("--filter", "do_filter", is_flag=True, help="Enable score-based filtering.")
@click.option("--full-text", is_flag=True, help="Show full tweet text in table output.")
@click.pass_context
def list_timeline(ctx, list_id, max_count, as_json, as_yaml, do_filter, full_text):
# type: (Any, str, int, bool, bool, bool, bool) -> None
def list_timeline(ctx, list_id, max_count, cursor, as_json, as_yaml, do_filter, full_text):
# type: (Any, str, int, Optional[str], bool, bool, bool, bool) -> None
"""Fetch tweets from a Twitter List. LIST_ID is the numeric list ID."""
compact = ctx.obj.get("compact", False)
config = load_config()
rich_output = use_rich_output(as_json=as_json, as_yaml=as_yaml, compact=compact)

def _run():
client = _get_client(config)
_fetch_and_display(
lambda count: client.fetch_list_timeline(list_id, count),
"list %s" % list_id, "📋", max_count, as_json, as_yaml, None, do_filter, config,
compact=compact, full_text=full_text,
try:
fetch_count = _resolve_configured_count(config, max_count)
if rich_output:
console.print("📋 Fetching list %s (%d tweets)...\n" % (list_id, fetch_count))
start = time.time()
tweets, next_cursor = client.fetch_list_timeline(
list_id,
fetch_count,
cursor=cursor,
return_cursor=True,
)
elapsed = time.time() - start
if rich_output:
console.print("✅ Fetched %d list %s in %.1fs\n" % (len(tweets), list_id, elapsed))
except (TwitterError, RuntimeError) as exc:
_exit_with_error(exc)

filtered = _apply_filter(tweets, do_filter, config, rich_output=rich_output)

if compact:
click.echo(tweets_to_compact_json(filtered))
return

save_tweet_cache(filtered)

if _emit_timeline_structured(filtered, next_cursor, as_json=as_json, as_yaml=as_yaml):
return

print_tweet_table(
filtered,
console,
title="📋 list %s — %d tweets" % (list_id, len(filtered)),
full_text=full_text,
)
_print_show_hint()
console.print()

_run_guarded(_run)


Expand Down
6 changes: 4 additions & 2 deletions twitter_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,17 @@ def fetch_article(self, tweet_id):
logger.info("fetch_article: tweet_id=%s", tweet_id)
return tweet

def fetch_list_timeline(self, list_id, count=20):
# type: (str, int) -> List[Tweet]
def fetch_list_timeline(self, list_id, count=20, cursor=None, return_cursor=False):
# type: (str, int, Optional[str], bool) -> Any
"""Fetch tweets from a Twitter List."""
return self._fetch_timeline(
"ListLatestTweetsTimeline",
count,
lambda data: _deep_get(data, "data", "list", "tweets_timeline", "timeline", "instructions"),
extra_variables={"listId": list_id},
override_base_variables=True,
start_cursor=cursor,
return_cursor=return_cursor,
)

def fetch_followers(self, user_id, count=20):
Expand Down