From 9c553e46bf18abc7a729978e37da87fce91d769c Mon Sep 17 00:00:00 2001 From: Alex Hillman Date: Tue, 30 Dec 2025 22:44:52 +0000 Subject: [PATCH] feat: add bookmarks command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `bird bookmarks` command to fetch the authenticated user's bookmarked tweets. - New `getBookmarks()` method in TwitterClient - Uses graphql_timeline_v2_bookmark_timeline feature flag - Supports `--count` and `--json` options - Follows existing patterns for query ID rotation and retry logic Usage: bird bookmarks # Get your recent bookmarks bird bookmarks -n 50 # Get 50 bookmarks bird bookmarks --json # Output as JSON 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/index.ts | 35 ++++++++++++ src/lib/twitter-client.ts | 115 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/src/index.ts b/src/index.ts index 53b393a..931064a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -138,6 +138,7 @@ const KNOWN_COMMANDS = new Set([ 'thread', 'search', 'mentions', + 'bookmarks', 'help', 'whoami', 'check', @@ -310,6 +311,7 @@ program 'TweetDetail', 'SearchTimeline', 'UserArticlesTweets', + 'Bookmarks', ]; if (cmdOpts.fresh) { @@ -671,6 +673,39 @@ program } }); +// Bookmarks command - get user's bookmarks +program + .command('bookmarks') + .description('Get your bookmarked tweets') + .option('-n, --count ', 'Number of bookmarks to fetch', '20') + .option('--json', 'Output as JSON') + .action(async (cmdOpts: { count?: string; json?: boolean }) => { + const opts = program.opts(); + const timeoutMs = resolveTimeoutFromOptions(opts); + const count = Number.parseInt(cmdOpts.count || '20', 10); + + const { cookies, warnings } = await resolveCredentialsFromOptions(opts); + + for (const warning of warnings) { + console.error(`${p('warn')}${warning}`); + } + + if (!cookies.authToken || !cookies.ct0) { + console.error(`${p('err')}Missing required credentials`); + process.exit(1); + } + + const client = new TwitterClient({ cookies, timeoutMs }); + const result = await client.getBookmarks(count); + + if (result.success && result.tweets) { + printTweets(result.tweets, { json: cmdOpts.json, emptyMessage: 'No bookmarks found.' }); + } else { + console.error(`${p('err')}Failed to fetch bookmarks: ${result.error}`); + process.exit(1); + } + }); + // Whoami command - show the logged-in account program .command('whoami') diff --git a/src/lib/twitter-client.ts b/src/lib/twitter-client.ts index d19bdad..e5f5379 100644 --- a/src/lib/twitter-client.ts +++ b/src/lib/twitter-client.ts @@ -23,6 +23,7 @@ const FALLBACK_QUERY_IDS = { TweetDetail: '97JF30KziU00483E_8elBA', SearchTimeline: 'M1jEez78PEfVfbQLvlWMvQ', UserArticlesTweets: '8zBy9h4L90aDL02RsBcCFg', + Bookmarks: 'RV1g3b8n_SGOHwkqKYSCFw', } as const; type OperationName = keyof typeof FALLBACK_QUERY_IDS; @@ -1795,4 +1796,118 @@ export class TwitterClient { return { success: true, tweets: thread }; } + + private buildBookmarksFeatures(): Record { + return { + ...this.buildSearchFeatures(), + graphql_timeline_v2_bookmark_timeline: true, + blue_business_profile_image_shape_enabled: true, + responsive_web_text_conversations_enabled: false, + tweetypie_unmention_optimization_enabled: true, + vibe_api_enabled: true, + responsive_web_twitter_blue_verified_badge_is_enabled: true, + interactive_text_enabled: true, + longform_notetweets_richtext_consumption_enabled: true, + responsive_web_media_download_video_enabled: false, + }; + } + + private async getBookmarksQueryIds(): Promise { + const primary = await this.getQueryId('Bookmarks'); + return Array.from(new Set([primary, 'RV1g3b8n_SGOHwkqKYSCFw', 'tmd4ifV8RHltzn8ymGg1aw'])); + } + + /** + * Get the authenticated user's bookmarks + */ + async getBookmarks(count = 20): Promise { + const variables = { + count, + includePromotedContent: false, + withDownvotePerspective: false, + withReactionsMetadata: false, + withReactionsPerspective: false, + }; + + const features = this.buildBookmarksFeatures(); + + const params = new URLSearchParams({ + variables: JSON.stringify(variables), + features: JSON.stringify(features), + }); + + const tryOnce = async () => { + let lastError: string | undefined; + let had404 = false; + const queryIds = await this.getBookmarksQueryIds(); + + for (const queryId of queryIds) { + const url = `${TWITTER_API_BASE}/${queryId}/Bookmarks?${params}`; + + try { + const response = await this.fetchWithTimeout(url, { + method: 'GET', + headers: this.getHeaders(), + }); + + if (response.status === 404) { + had404 = true; + lastError = `HTTP ${response.status}`; + continue; + } + + if (!response.ok) { + const text = await response.text(); + return { success: false as const, error: `HTTP ${response.status}: ${text.slice(0, 200)}`, had404 }; + } + + const data = (await response.json()) as { + data?: { + bookmark_timeline_v2?: { + timeline?: { + instructions?: Array<{ + entries?: Array<{ + content?: { + itemContent?: { + tweet_results?: { + result?: GraphqlTweetResult; + }; + }; + }; + }>; + }>; + }; + }; + }; + errors?: Array<{ message: string }>; + }; + + if (data.errors && data.errors.length > 0) { + return { success: false as const, error: data.errors.map((e) => e.message).join(', '), had404 }; + } + + const instructions = data.data?.bookmark_timeline_v2?.timeline?.instructions; + const tweets = this.parseTweetsFromInstructions(instructions); + + return { success: true as const, tweets, had404 }; + } catch (error) { + lastError = error instanceof Error ? error.message : String(error); + } + } + + return { success: false as const, error: lastError ?? 'Unknown error fetching bookmarks', had404 }; + }; + + const firstAttempt = await tryOnce(); + if (firstAttempt.success) return { success: true, tweets: firstAttempt.tweets }; + + if (firstAttempt.had404) { + await this.refreshQueryIds(); + const secondAttempt = await tryOnce(); + if (secondAttempt.success) return { success: true, tweets: secondAttempt.tweets }; + return { success: false, error: secondAttempt.error }; + } + + return { success: false, error: firstAttempt.error }; + } }