Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a485655f42 | ||
|
|
618d87ec72 | ||
|
|
8b9072fa7e | ||
|
|
3661f784cb | ||
|
|
c4689d2580 | ||
|
|
fdf099ff70 | ||
|
|
2cbd181d8c | ||
|
|
5397bf4e31 |
@@ -16,3 +16,7 @@ twitter-cli.bin
|
||||
bird
|
||||
.env
|
||||
.*.bun-build
|
||||
*.zip
|
||||
*.tar
|
||||
*.tar.gz
|
||||
*.tgz
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
# Changelog
|
||||
|
||||
## 0.1.1 — 2025-12-26
|
||||
|
||||
### Changed
|
||||
- Engine default now `auto` (GraphQL primary; Sweetistics only on fallback when configured).
|
||||
|
||||
### Tests
|
||||
- Add engine resolution tests for auto/default behavior.
|
||||
|
||||
### Fixed
|
||||
- GraphQL read: rotate TweetDetail query IDs with fallback to avoid 404s.
|
||||
|
||||
## 0.1.0 — 2025-12-20
|
||||
|
||||
### Added
|
||||
|
||||
@@ -44,9 +44,9 @@ bird mentions -n 5
|
||||
|
||||
## Engines
|
||||
|
||||
- `--engine graphql` (default) — use Twitter/X GraphQL with cookies (Chrome/Firefox/env/flags).
|
||||
- `--engine auto` (default) — use GraphQL first; if a Sweetistics API key is available, fall back on errors.
|
||||
- `--engine graphql` — use Twitter/X GraphQL with cookies (Chrome/Firefox/env/flags).
|
||||
- `--engine sweetistics` — use Sweetistics API key (no browser cookies needed).
|
||||
- `--engine auto` — Sweetistics if a key is available, otherwise GraphQL.
|
||||
|
||||
Global options:
|
||||
- `--timeout <ms>`: abort requests after the given timeout (milliseconds).
|
||||
@@ -74,7 +74,7 @@ Example `~/.config/bird/config.json5`:
|
||||
|
||||
```json5
|
||||
{
|
||||
engine: "graphql",
|
||||
engine: "auto",
|
||||
firefoxProfile: "default-release",
|
||||
sweetisticsApiKey: "sweet-...",
|
||||
allowFirefox: true,
|
||||
|
||||
+7
-1
@@ -1,12 +1,18 @@
|
||||
{
|
||||
"name": "@steipete/bird",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.1",
|
||||
"description": "CLI tool for tweeting and replying via Twitter/X GraphQL API",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"bin": {
|
||||
"bird": "dist/index.js"
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"README.md",
|
||||
"CHANGELOG.md",
|
||||
"LICENSE"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "pnpm run build:dist && pnpm run build:binary",
|
||||
"build:dist": "tsc",
|
||||
|
||||
+2
-17
@@ -17,6 +17,7 @@ import JSON5 from 'json5';
|
||||
import kleur from 'kleur';
|
||||
import { resolveCliInvocation } from './lib/cli-args.js';
|
||||
import { resolveCredentials } from './lib/cookies.js';
|
||||
import { type EngineMode, resolveEngineMode, shouldUseSweetistics } from './lib/engine.js';
|
||||
import { extractTweetId } from './lib/extract-tweet-id.js';
|
||||
import { SweetisticsClient } from './lib/sweetistics-client.js';
|
||||
import { type TweetData, TwitterClient } from './lib/twitter-client.js';
|
||||
@@ -130,12 +131,10 @@ program
|
||||
.option(
|
||||
'--engine <engine>',
|
||||
'Engine: graphql | sweetistics | auto',
|
||||
process.env.BIRD_ENGINE || config.engine || 'graphql',
|
||||
process.env.BIRD_ENGINE || config.engine || 'auto',
|
||||
)
|
||||
.option('--timeout <ms>', 'Request timeout in milliseconds');
|
||||
|
||||
type EngineMode = 'graphql' | 'sweetistics' | 'auto';
|
||||
|
||||
type MediaSpec = { path: string; alt?: string; mime: string; buffer: Buffer };
|
||||
|
||||
function resolveSweetisticsConfig(options: { sweetisticsApiKey?: string; sweetisticsBaseUrl?: string }) {
|
||||
@@ -147,20 +146,6 @@ function resolveSweetisticsConfig(options: { sweetisticsApiKey?: string; sweetis
|
||||
return { apiKey, baseUrl };
|
||||
}
|
||||
|
||||
function resolveEngineMode(value?: string): EngineMode {
|
||||
const normalized = (value || 'auto').toLowerCase();
|
||||
if (normalized === 'graphql' || normalized === 'sweetistics' || normalized === 'auto') {
|
||||
return normalized;
|
||||
}
|
||||
return 'auto';
|
||||
}
|
||||
|
||||
function shouldUseSweetistics(engine: EngineMode, hasApiKey: boolean): boolean {
|
||||
if (engine === 'sweetistics') return true;
|
||||
if (engine === 'graphql') return false;
|
||||
return hasApiKey; // auto
|
||||
}
|
||||
|
||||
function resolveTimeoutMs(...values: Array<string | number | undefined | null>): number | undefined {
|
||||
for (const value of values) {
|
||||
if (value === undefined || value === null || value === '') continue;
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
export type EngineMode = 'graphql' | 'sweetistics' | 'auto';
|
||||
|
||||
export function resolveEngineMode(value?: string): EngineMode {
|
||||
const normalized = (value || 'auto').toLowerCase();
|
||||
if (normalized === 'graphql' || normalized === 'sweetistics' || normalized === 'auto') {
|
||||
return normalized;
|
||||
}
|
||||
return 'auto';
|
||||
}
|
||||
|
||||
export function shouldUseSweetistics(engine: EngineMode, _hasApiKey: boolean): boolean {
|
||||
if (engine === 'sweetistics') return true;
|
||||
if (engine === 'graphql') return false;
|
||||
return false; // auto: GraphQL primary, Sweetistics only as fallback
|
||||
}
|
||||
@@ -2,6 +2,6 @@
|
||||
"CreateTweet": "2BA2DIWTRVxHgKc4Xh2uKQ",
|
||||
"CreateRetweet": "Uf3io9zVp1DsYxrmL5FJ7g",
|
||||
"FavoriteTweet": "kuFUYP9eV1FPoEy4N-pi7w",
|
||||
"TweetDetail": "aFvUsJm2c-oDkJV75blV6g",
|
||||
"SearchTimeline": "M1jEez78PEfVfbQLvlWMvQ"
|
||||
"TweetDetail": "97JF30KziU00483E_8elBA",
|
||||
"SearchTimeline": "5h0kNbk3ii97rmfY6CdgAA"
|
||||
}
|
||||
|
||||
+75
-45
@@ -14,7 +14,7 @@ const FALLBACK_QUERY_IDS = {
|
||||
CreateTweet: 'TAJw1rBsjAtdNgTdlo2oeg',
|
||||
CreateRetweet: 'ojPdsZsimiJrUGLR1sjUtA',
|
||||
FavoriteTweet: 'lI07N6Otwv1PhnEgXILM7A',
|
||||
TweetDetail: 'nBS-WpgA6ZG0CyNHD517JQ',
|
||||
TweetDetail: '97JF30KziU00483E_8elBA',
|
||||
SearchTimeline: 'M1jEez78PEfVfbQLvlWMvQ',
|
||||
UserArticlesTweets: '8zBy9h4L90aDL02RsBcCFg',
|
||||
} as const;
|
||||
@@ -26,6 +26,10 @@ const QUERY_IDS: Record<OperationName, string> = {
|
||||
...(queryIds as Partial<Record<OperationName, string>>),
|
||||
};
|
||||
|
||||
const TWEET_DETAIL_QUERY_IDS = Array.from(
|
||||
new Set([QUERY_IDS.TweetDetail, '97JF30KziU00483E_8elBA', 'aFvUsJm2c-oDkJV75blV6g']),
|
||||
);
|
||||
|
||||
const SEARCH_TIMELINE_QUERY_IDS = Array.from(
|
||||
new Set([QUERY_IDS.SearchTimeline, 'M1jEez78PEfVfbQLvlWMvQ', '5h0kNbk3ii97rmfY6CdgAA', 'Tp1sewRU1AsZpBWhqCZicQ']),
|
||||
);
|
||||
@@ -636,6 +640,23 @@ export class TwitterClient {
|
||||
};
|
||||
}
|
||||
|
||||
private buildTweetDetailFeatures(): Record<string, boolean> {
|
||||
return {
|
||||
...this.buildArticleFeatures(),
|
||||
responsive_web_graphql_exclude_directive_enabled: true,
|
||||
communities_web_enable_tweet_community_results_fetch: true,
|
||||
responsive_web_twitter_article_plain_text_enabled: true,
|
||||
responsive_web_twitter_article_seed_tweet_detail_enabled: true,
|
||||
responsive_web_twitter_article_seed_tweet_summary_enabled: true,
|
||||
longform_notetweets_rich_text_read_enabled: true,
|
||||
longform_notetweets_inline_media_enabled: true,
|
||||
responsive_web_edit_tweet_api_enabled: true,
|
||||
tweet_awards_web_tipping_enabled: false,
|
||||
creator_subscriptions_quote_tweet_preview_enabled: false,
|
||||
verified_phone_label_enabled: false,
|
||||
};
|
||||
}
|
||||
|
||||
private buildArticleFieldToggles(): Record<string, boolean> {
|
||||
return {
|
||||
withPayments: false,
|
||||
@@ -798,33 +819,19 @@ export class TwitterClient {
|
||||
};
|
||||
|
||||
const features = {
|
||||
rweb_tipjar_consumption_enabled: true,
|
||||
responsive_web_graphql_exclude_directive_enabled: true,
|
||||
verified_phone_label_enabled: false,
|
||||
creator_subscriptions_tweet_preview_api_enabled: true,
|
||||
responsive_web_graphql_timeline_navigation_enabled: true,
|
||||
responsive_web_graphql_skip_user_profile_image_extensions_enabled: false,
|
||||
communities_web_enable_tweet_community_results_fetch: true,
|
||||
c9s_tweet_anatomy_moderator_badge_enabled: true,
|
||||
...this.buildTweetDetailFeatures(),
|
||||
articles_preview_enabled: true,
|
||||
articles_rest_api_enabled: true,
|
||||
responsive_web_edit_tweet_api_enabled: true,
|
||||
responsive_web_graphql_skip_user_profile_image_extensions_enabled: false,
|
||||
creator_subscriptions_tweet_preview_api_enabled: true,
|
||||
graphql_is_translatable_rweb_tweet_is_translatable_enabled: true,
|
||||
view_counts_everywhere_api_enabled: true,
|
||||
longform_notetweets_consumption_enabled: true,
|
||||
responsive_web_twitter_article_tweet_consumption_enabled: true,
|
||||
responsive_web_twitter_article_plain_text_enabled: true,
|
||||
responsive_web_twitter_article_seed_tweet_detail_enabled: true,
|
||||
responsive_web_twitter_article_seed_tweet_summary_enabled: true,
|
||||
tweet_awards_web_tipping_enabled: false,
|
||||
creator_subscriptions_quote_tweet_preview_enabled: false,
|
||||
freedom_of_speech_not_reach_fetch_enabled: true,
|
||||
standardized_nudges_misinfo: true,
|
||||
tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled: true,
|
||||
rweb_video_timestamps_enabled: true,
|
||||
longform_notetweets_rich_text_read_enabled: true,
|
||||
longform_notetweets_inline_media_enabled: true,
|
||||
responsive_web_enhance_cards_enabled: false,
|
||||
};
|
||||
|
||||
const params = new URLSearchParams({
|
||||
@@ -832,44 +839,67 @@ export class TwitterClient {
|
||||
features: JSON.stringify(features),
|
||||
});
|
||||
|
||||
const url = `${TWITTER_API_BASE}/${QUERY_IDS.TweetDetail}/TweetDetail?${params}`;
|
||||
|
||||
try {
|
||||
const response = await this.fetchWithTimeout(url, {
|
||||
method: 'GET',
|
||||
headers: this.getHeaders(),
|
||||
});
|
||||
const parseResponse = async (response: Response) => {
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
return { success: false as const, error: `HTTP ${response.status}: ${text.slice(0, 200)}` };
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
return { success: false, error: `HTTP ${response.status}: ${text.slice(0, 200)}` };
|
||||
}
|
||||
|
||||
const data = (await response.json()) as {
|
||||
data?: {
|
||||
tweetResult?: { result?: GraphqlTweetResult };
|
||||
threaded_conversation_with_injections_v2?: {
|
||||
instructions?: Array<{
|
||||
entries?: Array<{
|
||||
content?: {
|
||||
itemContent?: {
|
||||
tweet_results?: {
|
||||
result?: GraphqlTweetResult;
|
||||
const data = (await response.json()) as {
|
||||
data?: {
|
||||
tweetResult?: { result?: GraphqlTweetResult };
|
||||
threaded_conversation_with_injections_v2?: {
|
||||
instructions?: Array<{
|
||||
entries?: Array<{
|
||||
content?: {
|
||||
itemContent?: {
|
||||
tweet_results?: {
|
||||
result?: GraphqlTweetResult;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}>;
|
||||
}>;
|
||||
}>;
|
||||
};
|
||||
};
|
||||
errors?: Array<{ message: string; code?: number }>;
|
||||
};
|
||||
errors?: Array<{ message: string; code?: number }>;
|
||||
|
||||
if (data.errors && data.errors.length > 0) {
|
||||
return { success: false as const, error: data.errors.map((e) => e.message).join(', ') };
|
||||
}
|
||||
|
||||
return { success: true as const, data: data.data ?? {} };
|
||||
};
|
||||
|
||||
if (data.errors && data.errors.length > 0) {
|
||||
return { success: false, error: data.errors.map((e) => e.message).join(', ') };
|
||||
let lastError: string | undefined;
|
||||
|
||||
for (const queryId of TWEET_DETAIL_QUERY_IDS) {
|
||||
const url = `${TWITTER_API_BASE}/${queryId}/TweetDetail?${params}`;
|
||||
const response = await this.fetchWithTimeout(url, {
|
||||
method: 'GET',
|
||||
headers: this.getHeaders(),
|
||||
});
|
||||
|
||||
if (response.status !== 404) {
|
||||
return await parseResponse(response);
|
||||
}
|
||||
|
||||
const postResponse = await this.fetchWithTimeout(`${TWITTER_API_BASE}/${queryId}/TweetDetail`, {
|
||||
method: 'POST',
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify({ variables, features, queryId }),
|
||||
});
|
||||
|
||||
if (postResponse.status !== 404) {
|
||||
return await parseResponse(postResponse);
|
||||
}
|
||||
|
||||
lastError = 'HTTP 404';
|
||||
}
|
||||
|
||||
return { success: true, data: data.data ?? {} };
|
||||
return { success: false, error: lastError ?? 'Unknown error fetching tweet detail' };
|
||||
} catch (error) {
|
||||
return { success: false, error: error instanceof Error ? error.message : String(error) };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { resolveEngineMode, shouldUseSweetistics } from '../src/lib/engine.js';
|
||||
|
||||
describe('engine', () => {
|
||||
it('normalizes engine values and defaults to auto', () => {
|
||||
expect(resolveEngineMode()).toBe('auto');
|
||||
expect(resolveEngineMode('graphql')).toBe('graphql');
|
||||
expect(resolveEngineMode('sweetistics')).toBe('sweetistics');
|
||||
expect(resolveEngineMode('AUTO')).toBe('auto');
|
||||
expect(resolveEngineMode('nope')).toBe('auto');
|
||||
});
|
||||
|
||||
it('uses GraphQL as primary in auto mode', () => {
|
||||
expect(shouldUseSweetistics('auto', false)).toBe(false);
|
||||
expect(shouldUseSweetistics('auto', true)).toBe(false);
|
||||
});
|
||||
|
||||
it('forces engine selection when explicitly set', () => {
|
||||
expect(shouldUseSweetistics('sweetistics', false)).toBe(true);
|
||||
expect(shouldUseSweetistics('graphql', true)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -283,7 +283,7 @@ describe('TwitterClient', () => {
|
||||
});
|
||||
|
||||
it('should report HTTP errors from getTweet', async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 404,
|
||||
text: async () => 'Not Found',
|
||||
@@ -474,6 +474,38 @@ describe('TwitterClient', () => {
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.tweet?.text).toBe('Long form note content.');
|
||||
});
|
||||
|
||||
it('retries TweetDetail query id on 404', async () => {
|
||||
const payload = {
|
||||
data: {
|
||||
tweetResult: {
|
||||
result: {
|
||||
rest_id: '1',
|
||||
legacy: {
|
||||
full_text: 'hello',
|
||||
created_at: '2024-01-01T00:00:00Z',
|
||||
reply_count: 0,
|
||||
retweet_count: 0,
|
||||
favorite_count: 0,
|
||||
},
|
||||
core: { user_results: { result: { legacy: { screen_name: 'root', name: 'Root' } } } },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mockFetch
|
||||
.mockResolvedValueOnce({ ok: false, status: 404, text: async () => '' })
|
||||
.mockResolvedValueOnce({ ok: false, status: 404, text: async () => '' })
|
||||
.mockResolvedValueOnce({ ok: true, status: 200, json: async () => payload });
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const result = await client.getTweet('1');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.tweet?.id).toBe('1');
|
||||
expect(mockFetch).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCurrentUser', () => {
|
||||
|
||||
Reference in New Issue
Block a user