| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * bird-search.mjs - Vendored Bird CLI search wrapper for /last30days. |
| 4 | * Subset of @steipete/bird v0.8.0 (MIT License, Peter Steinberger). |
| 5 | * |
| 6 | * Usage: |
| 7 | * node bird-search.mjs [--count N] [--timeout-ms MS] [--json] [--top] -- <query> |
| 8 | * node bird-search.mjs <query> [--count N] [--timeout-ms MS] [--json] [--top] |
| 9 | * node bird-search.mjs --whoami |
| 10 | * node bird-search.mjs --check |
| 11 | */ |
| 12 | |
| 13 | import { redactSecrets, resolveCredentials } from './lib/cookies.js'; |
| 14 | import { TwitterClientBase } from './lib/twitter-client-base.js'; |
| 15 | import { withSearch } from './lib/twitter-client-search.js'; |
| 16 | |
| 17 | // Build a search-only client (no posting, bookmarks, etc.) |
| 18 | const SearchClient = withSearch(TwitterClientBase); |
| 19 | |
| 20 | const args = process.argv.slice(2); |
| 21 | |
| 22 | function writeStdout(text) { |
| 23 | if (text) process.stdout.write(text); |
| 24 | } |
| 25 | |
| 26 | function writeStderr(text) { |
| 27 | if (text) process.stderr.write(text); |
| 28 | } |
| 29 | |
| 30 | const MAX_TIMEOUT_MS = 2_147_483_647; |
| 31 | |
| 32 | function parsePositiveCount(raw) { |
| 33 | if (typeof raw !== 'string' || !/^\d+$/.test(raw)) { |
| 34 | return null; |
| 35 | } |
| 36 | const parsed = Number.parseInt(raw, 10); |
| 37 | if (!Number.isSafeInteger(parsed) || parsed <= 0) { |
| 38 | return null; |
| 39 | } |
| 40 | return parsed; |
| 41 | } |
| 42 | |
| 43 | async function main() { |
| 44 | // Single-pass CLI parse with `--` end-of-options support. Only recognize |
| 45 | // mode and option flags before `--`; everything after `--` is positional |
| 46 | // query text so leading `-` queries or `--check`/`--whoami` cannot hijack |
| 47 | // option parsing. |
| 48 | let mode = 'search'; |
| 49 | let count = 20; |
| 50 | let timeoutMs = 30000; |
| 51 | let jsonOutput = false; |
| 52 | // --top reads X's Top tab (engagement-ranked) instead of Latest. Topic |
| 53 | // searches need it: on a busy topic Latest holds only the last few minutes. |
| 54 | let product = 'Latest'; |
| 55 | const positional = []; |
| 56 | let afterDoubleDash = false; |
| 57 | |
| 58 | for (let i = 0; i < args.length; i++) { |
| 59 | const arg = args[i]; |
| 60 | if (afterDoubleDash) { |
| 61 | positional.push(arg); |
| 62 | continue; |
| 63 | } |
| 64 | if (arg === '--') { |
| 65 | afterDoubleDash = true; |
| 66 | continue; |
| 67 | } |
| 68 | if (arg === '--check') { |
| 69 | mode = 'check'; |
| 70 | } else if (arg === '--whoami') { |
| 71 | mode = 'whoami'; |
| 72 | } else if (arg === '--count' || arg === '-n') { |
| 73 | const parsedCount = parsePositiveCount(args[i + 1]); |
| 74 | if (parsedCount === null) { |
| 75 | writeStderr(`Error: Invalid ${arg} value (expected positive integer)\n`); |
| 76 | return 1; |
| 77 | } |
| 78 | count = parsedCount; |
| 79 | i++; |
| 80 | } else if (arg === '--timeout-ms') { |
| 81 | const parsedTimeoutMs = parsePositiveCount(args[i + 1]); |
| 82 | if (parsedTimeoutMs === null || parsedTimeoutMs > MAX_TIMEOUT_MS) { |
| 83 | writeStderr(`Error: Invalid ${arg} value (expected positive integer <= ${MAX_TIMEOUT_MS})\n`); |
| 84 | return 1; |
| 85 | } |
| 86 | timeoutMs = parsedTimeoutMs; |
| 87 | i++; |
| 88 | } else if (arg === '--json') { |
| 89 | jsonOutput = true; |
| 90 | } else if (arg === '--top') { |
| 91 | product = 'Top'; |
| 92 | } else if (arg.startsWith('-')) { |
| 93 | writeStderr('Unknown option\nUsage: node bird-search.mjs [--count N] [--timeout-ms MS] [--json] [--top] -- <query>\n'); |
| 94 | return 1; |
| 95 | } else { |
| 96 | positional.push(arg); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // --check: verify that credentials can be resolved |
| 101 | if (mode === 'check') { |
| 102 | try { |
| 103 | const { cookies, warnings } = await resolveCredentials({}); |
| 104 | if (cookies.authToken && cookies.ct0) { |
| 105 | writeStdout(JSON.stringify({ authenticated: true, source: cookies.source })); |
| 106 | return 0; |
| 107 | } |
| 108 | writeStdout(JSON.stringify({ authenticated: false, warnings })); |
| 109 | return 1; |
| 110 | } catch (err) { |
| 111 | const msg = redactSecrets(err?.message || String(err)); |
| 112 | writeStdout(JSON.stringify({ authenticated: false, error: msg })); |
| 113 | return 1; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // --whoami: check auth and output source |
| 118 | if (mode === 'whoami') { |
| 119 | try { |
| 120 | const { cookies } = await resolveCredentials({}); |
| 121 | if (cookies.authToken && cookies.ct0) { |
| 122 | writeStdout(cookies.source || 'authenticated'); |
| 123 | return 0; |
| 124 | } |
| 125 | writeStderr('Not authenticated\n'); |
| 126 | return 1; |
| 127 | } catch (err) { |
| 128 | const msg = redactSecrets(err?.message || String(err)); |
| 129 | writeStderr(`Auth check failed: ${msg}\n`); |
| 130 | return 1; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | const query = positional.join(' '); |
| 135 | if (!query || !query.trim()) { |
| 136 | writeStderr('Usage: node bird-search.mjs [--count N] [--timeout-ms MS] [--json] [--top] -- <query>\n'); |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | const resolvedSecrets = []; |
| 141 | try { |
| 142 | // Resolve credentials (env vars, then browser cookies) |
| 143 | const { cookies, warnings } = await resolveCredentials({}); |
| 144 | if (cookies.authToken) resolvedSecrets.push(cookies.authToken); |
| 145 | if (cookies.ct0) resolvedSecrets.push(cookies.ct0); |
| 146 | if (cookies.cookieHeader) resolvedSecrets.push(cookies.cookieHeader); |
| 147 | |
| 148 | if (!cookies.authToken || !cookies.ct0) { |
| 149 | const msg = redactSecrets( |
| 150 | warnings.length > 0 ? warnings.join('; ') : 'No Twitter credentials found', |
| 151 | resolvedSecrets, |
| 152 | ); |
| 153 | if (jsonOutput) { |
| 154 | writeStdout(JSON.stringify({ error: msg, items: [] })); |
| 155 | } else { |
| 156 | writeStderr(`Error: ${msg}\n`); |
| 157 | } |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | const client = new SearchClient({ |
| 162 | cookies: { |
| 163 | authToken: cookies.authToken, |
| 164 | ct0: cookies.ct0, |
| 165 | cookieHeader: cookies.cookieHeader, |
| 166 | }, |
| 167 | timeoutMs, |
| 168 | }); |
| 169 | |
| 170 | const result = await client.search(query, count, { product }); |
| 171 | |
| 172 | if (!result.success) { |
| 173 | const msg = redactSecrets(result.error, resolvedSecrets); |
| 174 | const partialItems = Array.isArray(result.tweets) ? result.tweets : []; |
| 175 | if (jsonOutput) { |
| 176 | writeStdout(JSON.stringify({ error: msg, items: partialItems })); |
| 177 | } else { |
| 178 | writeStderr(`Search failed: ${msg}\n`); |
| 179 | } |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | const tweets = result.tweets || []; |
| 184 | if (jsonOutput) { |
| 185 | writeStdout(JSON.stringify(tweets)); |
| 186 | } else { |
| 187 | for (const tweet of tweets) { |
| 188 | const author = tweet.author?.username || 'unknown'; |
| 189 | writeStdout(`@${author}: ${tweet.text?.slice(0, 200)}\n\n`); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } catch (err) { |
| 195 | const msg = redactSecrets(err?.message || String(err), resolvedSecrets); |
| 196 | if (jsonOutput) { |
| 197 | writeStdout(JSON.stringify({ error: msg, items: [] })); |
| 198 | } else { |
| 199 | writeStderr(`Error: ${msg}\n`); |
| 200 | } |
| 201 | return 1; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | try { |
| 206 | const code = await main(); |
| 207 | process.exitCode = Number.isInteger(code) ? code : 1; |
| 208 | } catch (err) { |
| 209 | const msg = redactSecrets(err?.message || String(err)); |
| 210 | writeStderr(`Fatal error: ${msg}\n`); |
| 211 | process.exitCode = 1; |
| 212 | } |
| 213 |