feat: add full pagination support to likes (parity with bookmarks)

Library changes:
- Add getAllLikes(options) method for fetching all likes with cursor support
- Refactor getLikes to use private getLikesPaged helper
- Return nextCursor for resumable pagination
- Support maxPages option to limit pages fetched

CLI changes:
- Add --all flag to fetch all likes
- Add --max-pages to limit pagination
- Add --cursor to resume from previous fetch
- Output nextCursor in JSON mode for resumable pagination

This brings likes to feature parity with bookmarks.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

# Conflicts:
#	src/commands/users.ts
This commit is contained in:
Peter Steinberger
2026-01-12 07:06:18 +00:00
parent 9cfeda13b2
commit bc744a3bf9
2 changed files with 90 additions and 38 deletions
+38 -5
View File
@@ -328,13 +328,25 @@ export function registerUserCommands(program: Command, ctx: CliContext): void {
.command('likes')
.description('Get your liked tweets')
.option('-n, --count <number>', 'Number of likes to fetch', '20')
.option('--all', 'Fetch all likes (paged)')
.option('--max-pages <number>', 'Stop after N pages when using --all')
.option('--cursor <string>', 'Resume pagination from a cursor')
.option('--json', 'Output as JSON')
.option('--json-full', 'Output as JSON with full raw API response in _raw field')
.action(async (cmdOpts: { count?: string; json?: boolean; jsonFull?: boolean }) => {
.action(
async (cmdOpts: {
count?: string;
json?: boolean;
jsonFull?: boolean;
all?: boolean;
maxPages?: string;
cursor?: string;
}) => {
const opts = program.opts();
const timeoutMs = ctx.resolveTimeoutFromOptions(opts);
const quoteDepth = ctx.resolveQuoteDepthFromOptions(opts);
const count = Number.parseInt(cmdOpts.count || '20', 10);
const maxPages = cmdOpts.maxPages ? Number.parseInt(cmdOpts.maxPages, 10) : undefined;
const { cookies, warnings } = await ctx.resolveCredentialsFromOptions(opts);
@@ -347,20 +359,41 @@ export function registerUserCommands(program: Command, ctx: CliContext): void {
process.exit(1);
}
const usePagination = cmdOpts.all || cmdOpts.cursor;
if (maxPages !== undefined && !usePagination) {
console.error(`${ctx.p('err')}--max-pages requires --all or --cursor.`);
process.exit(1);
}
if (!usePagination && (!Number.isFinite(count) || count <= 0)) {
console.error(`${ctx.p('err')}Invalid --count. Expected a positive integer.`);
process.exit(1);
}
if (maxPages !== undefined && (!Number.isFinite(maxPages) || maxPages <= 0)) {
console.error(`${ctx.p('err')}Invalid --max-pages. Expected a positive integer.`);
process.exit(1);
}
const client = new TwitterClient({ cookies, timeoutMs, quoteDepth });
const includeRaw = cmdOpts.jsonFull ?? false;
const result = await client.getLikes(count, { includeRaw });
const timelineOptions = { includeRaw };
const paginationOptions = { includeRaw, maxPages, cursor: cmdOpts.cursor };
const result = usePagination
? await client.getAllLikes(paginationOptions)
: await client.getLikes(count, timelineOptions);
if (result.success) {
ctx.printTweets(result.tweets, {
json: cmdOpts.json || cmdOpts.jsonFull,
const isJson = Boolean(cmdOpts.json || cmdOpts.jsonFull);
ctx.printTweetsResult(result, {
json: isJson,
usePagination: Boolean(usePagination),
emptyMessage: 'No liked tweets found.',
});
} else {
console.error(`${ctx.p('err')}Failed to fetch likes: ${result.error}`);
process.exit(1);
}
});
},
);
program
.command('whoami')
+31 -12
View File
@@ -21,6 +21,7 @@ export interface TwitterClientTimelineMethods {
getBookmarks(count?: number, options?: TimelineFetchOptions): Promise<SearchResult>;
getAllBookmarks(options?: TimelinePaginationOptions): Promise<SearchResult>;
getLikes(count?: number, options?: TimelineFetchOptions): Promise<SearchResult>;
getAllLikes(options?: TimelinePaginationOptions): Promise<SearchResult>;
getBookmarkFolderTimeline(folderId: string, count?: number, options?: TimelineFetchOptions): Promise<SearchResult>;
getAllBookmarkFolderTimeline(folderId: string, options?: TimelinePaginationOptions): Promise<SearchResult>;
}
@@ -75,18 +76,27 @@ export function withTimelines<TBase extends AbstractConstructor<TwitterClientBas
* Get the authenticated user's liked tweets
*/
async getLikes(count = 20, options: TimelineFetchOptions = {}): Promise<SearchResult> {
const { includeRaw = false } = options;
return this.getLikesPaged(count, options);
}
async getAllLikes(options?: TimelinePaginationOptions): Promise<SearchResult> {
return this.getLikesPaged(Number.POSITIVE_INFINITY, options);
}
private async getLikesPaged(limit: number, options: TimelinePaginationOptions = {}): Promise<SearchResult> {
const userResult = await this.getCurrentUser();
if (!userResult.success || !userResult.user) {
return { success: false, error: userResult.error ?? 'Could not determine current user' };
}
const userId = userResult.user.id;
const features = buildLikesFeatures();
const pageSize = 20;
const seen = new Set<string>();
const tweets: TweetData[] = [];
let cursor: string | undefined;
const features = buildLikesFeatures();
let cursor: string | undefined = options.cursor;
let nextCursor: string | undefined;
let pagesFetched = 0;
const { includeRaw = false, maxPages } = options;
const fetchPage = async (pageCount: number, pageCursor?: string) => {
let lastError: string | undefined;
@@ -164,9 +174,9 @@ export function withTimelines<TBase extends AbstractConstructor<TwitterClientBas
}
}
const pageTweets = parseTweetsFromInstructions(instructions, { quoteDepth: this.quoteDepth, includeRaw });
const nextCursor = extractCursorFromInstructions(instructions);
const extractedCursor = extractCursorFromInstructions(instructions);
return { success: true as const, tweets: pageTweets, cursor: nextCursor, had404 };
return { success: true as const, tweets: pageTweets, cursor: extractedCursor, had404 };
} catch (error) {
lastError = error instanceof Error ? error.message : String(error);
}
@@ -194,12 +204,14 @@ export function withTimelines<TBase extends AbstractConstructor<TwitterClientBas
return { success: false as const, error: firstAttempt.error };
};
while (tweets.length < count) {
const pageCount = Math.min(pageSize, count - tweets.length);
const unlimited = !Number.isFinite(limit);
while (unlimited || tweets.length < limit) {
const pageCount = unlimited ? pageSize : Math.min(pageSize, limit - tweets.length);
const page = await fetchWithRefresh(pageCount, cursor);
if (!page.success) {
return { success: false, error: page.error };
}
pagesFetched += 1;
let added = 0;
for (const tweet of page.tweets) {
@@ -209,18 +221,25 @@ export function withTimelines<TBase extends AbstractConstructor<TwitterClientBas
seen.add(tweet.id);
tweets.push(tweet);
added += 1;
if (tweets.length >= count) {
if (!unlimited && tweets.length >= limit) {
break;
}
}
if (!page.cursor || page.cursor === cursor || page.tweets.length === 0 || added === 0) {
const pageCursor = page.cursor;
if (!pageCursor || pageCursor === cursor || page.tweets.length === 0 || added === 0) {
nextCursor = undefined;
break;
}
cursor = page.cursor;
if (maxPages && pagesFetched >= maxPages) {
nextCursor = pageCursor;
break;
}
cursor = pageCursor;
nextCursor = pageCursor;
}
return { success: true, tweets };
return { success: true, tweets, nextCursor };
}
/**