Merge pull request #227 from Chelebii/fix/windows-bird-x-runtime

fix(windows): stabilize bundled Bird X search
This commit is contained in:
Matt Van Horn
2026-04-13 22:15:21 -04:00
committed by GitHub
4 changed files with 126 additions and 102 deletions
+5
View File
@@ -33,6 +33,11 @@ def ensure_supported_python(version_info: tuple[int, int, int] | object | None =
ensure_supported_python()
if os.name == "nt":
for stream in (sys.stdout, sys.stderr):
if hasattr(stream, "reconfigure"):
stream.reconfigure(encoding="utf-8", errors="replace")
SCRIPT_DIR = Path(__file__).parent.resolve()
sys.path.insert(0, str(SCRIPT_DIR))
+4
View File
@@ -177,6 +177,8 @@ def _run_bird_search(query: str, count: int, timeout: int) -> Dict[str, Any]:
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding="utf-8",
errors="replace",
preexec_fn=preexec,
env=_subprocess_env(),
)
@@ -336,6 +338,8 @@ def search_handles(
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding="utf-8",
errors="replace",
preexec_fn=preexec,
env=_subprocess_env(),
)
+2
View File
@@ -579,6 +579,8 @@ def get_x_source_status(config: dict[str, Any]) -> dict[str, Any]:
"""
from . import bird_x
if config.get('AUTH_TOKEN') and config.get('CT0'):
bird_x.set_credentials(config.get('AUTH_TOKEN'), config.get('CT0'))
bird_status = bird_x.get_bird_status()
xai_available = bool(config.get('XAI_API_KEY'))
+44 -31
View File
@@ -18,20 +18,28 @@ const SearchClient = withSearch(TwitterClientBase);
const args = process.argv.slice(2);
function writeStdout(text) {
if (text) process.stdout.write(text);
}
function writeStderr(text) {
if (text) process.stderr.write(text);
}
async function main() {
// --check: verify that credentials can be resolved
if (args.includes('--check')) {
try {
const { cookies, warnings } = await resolveCredentials({});
if (cookies.authToken && cookies.ct0) {
process.stdout.write(JSON.stringify({ authenticated: true, source: cookies.source }));
process.exit(0);
} else {
process.stdout.write(JSON.stringify({ authenticated: false, warnings }));
process.exit(1);
writeStdout(JSON.stringify({ authenticated: true, source: cookies.source }));
return 0;
}
writeStdout(JSON.stringify({ authenticated: false, warnings }));
return 1;
} catch (err) {
process.stdout.write(JSON.stringify({ authenticated: false, error: err.message }));
process.exit(1);
writeStdout(JSON.stringify({ authenticated: false, error: err.message }));
return 1;
}
}
@@ -40,15 +48,14 @@ if (args.includes('--whoami')) {
try {
const { cookies } = await resolveCredentials({});
if (cookies.authToken && cookies.ct0) {
process.stdout.write(cookies.source || 'authenticated');
process.exit(0);
} else {
process.stderr.write('Not authenticated\n');
process.exit(1);
writeStdout(cookies.source || 'authenticated');
return 0;
}
writeStderr('Not authenticated\n');
return 1;
} catch (err) {
process.stderr.write(`Auth check failed: ${err.message}\n`);
process.exit(1);
writeStderr(`Auth check failed: ${err.message}\n`);
return 1;
}
}
@@ -72,8 +79,8 @@ for (let i = 0; i < args.length; i++) {
}
if (!query) {
process.stderr.write('Usage: node bird-search.mjs <query> [--count N] [--json]\n');
process.exit(1);
writeStderr('Usage: node bird-search.mjs <query> [--count N] [--json]\n');
return 1;
}
try {
@@ -83,14 +90,13 @@ try {
if (!cookies.authToken || !cookies.ct0) {
const msg = warnings.length > 0 ? warnings.join('; ') : 'No Twitter credentials found';
if (jsonOutput) {
process.stdout.write(JSON.stringify({ error: msg, items: [] }));
writeStdout(JSON.stringify({ error: msg, items: [] }));
} else {
process.stderr.write(`Error: ${msg}\n`);
writeStderr(`Error: ${msg}\n`);
}
process.exit(1);
return 1;
}
// Create search client
const client = new SearchClient({
cookies: {
authToken: cookies.authToken,
@@ -100,35 +106,42 @@ try {
timeoutMs: 30000,
});
// Run search
const result = await client.search(query, count);
if (!result.success) {
if (jsonOutput) {
process.stdout.write(JSON.stringify({ error: result.error, items: [] }));
writeStdout(JSON.stringify({ error: result.error, items: [] }));
} else {
process.stderr.write(`Search failed: ${result.error}\n`);
writeStderr(`Search failed: ${result.error}\n`);
}
process.exit(1);
return 1;
}
// Output results
const tweets = result.tweets || [];
if (jsonOutput) {
process.stdout.write(JSON.stringify(tweets));
writeStdout(JSON.stringify(tweets));
} else {
for (const tweet of tweets) {
const author = tweet.author?.username || 'unknown';
process.stdout.write(`@${author}: ${tweet.text?.slice(0, 200)}\n\n`);
writeStdout(`@${author}: ${tweet.text?.slice(0, 200)}\n\n`);
}
}
process.exit(0);
return 0;
} catch (err) {
if (jsonOutput) {
process.stdout.write(JSON.stringify({ error: err.message, items: [] }));
writeStdout(JSON.stringify({ error: err.message, items: [] }));
} else {
process.stderr.write(`Error: ${err.message}\n`);
writeStderr(`Error: ${err.message}\n`);
}
process.exit(1);
return 1;
}
}
try {
const code = await main();
process.exitCode = Number.isInteger(code) ? code : 1;
} catch (err) {
writeStderr(`Fatal error: ${err?.message || err}\n`);
process.exitCode = 1;
}