fix(windows): stabilize bundled Bird X search

This commit is contained in:
Chelebii
2026-04-11 23:30:39 +01:00
parent 01812ec185
commit d3972a6523
4 changed files with 127 additions and 103 deletions
+6 -1
View File
@@ -33,6 +33,11 @@ def ensure_supported_python(version_info: tuple[int, int, int] | object | None =
ensure_supported_python() 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() SCRIPT_DIR = Path(__file__).parent.resolve()
sys.path.insert(0, str(SCRIPT_DIR)) sys.path.insert(0, str(SCRIPT_DIR))
@@ -103,7 +108,7 @@ def save_output(report: schema.Report, emit: str, save_dir: str, suffix: str = "
content = emit_output(report, emit) content = emit_output(report, emit)
else: else:
content = render.render_full(report) content = render.render_full(report)
out_path.write_text(content) out_path.write_text(content, encoding="utf-8")
return out_path return out_path
+4
View File
@@ -177,6 +177,8 @@ def _run_bird_search(query: str, count: int, timeout: int) -> Dict[str, Any]:
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, text=True,
encoding="utf-8",
errors="replace",
preexec_fn=preexec, preexec_fn=preexec,
env=_subprocess_env(), env=_subprocess_env(),
) )
@@ -336,6 +338,8 @@ def search_handles(
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, text=True,
encoding="utf-8",
errors="replace",
preexec_fn=preexec, preexec_fn=preexec,
env=_subprocess_env(), 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 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() bird_status = bird_x.get_bird_status()
xai_available = bool(config.get('XAI_API_KEY')) 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); 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 // --check: verify that credentials can be resolved
if (args.includes('--check')) { if (args.includes('--check')) {
try { try {
const { cookies, warnings } = await resolveCredentials({}); const { cookies, warnings } = await resolveCredentials({});
if (cookies.authToken && cookies.ct0) { if (cookies.authToken && cookies.ct0) {
process.stdout.write(JSON.stringify({ authenticated: true, source: cookies.source })); writeStdout(JSON.stringify({ authenticated: true, source: cookies.source }));
process.exit(0); return 0;
} else {
process.stdout.write(JSON.stringify({ authenticated: false, warnings }));
process.exit(1);
} }
writeStdout(JSON.stringify({ authenticated: false, warnings }));
return 1;
} catch (err) { } catch (err) {
process.stdout.write(JSON.stringify({ authenticated: false, error: err.message })); writeStdout(JSON.stringify({ authenticated: false, error: err.message }));
process.exit(1); return 1;
} }
} }
@@ -40,15 +48,14 @@ if (args.includes('--whoami')) {
try { try {
const { cookies } = await resolveCredentials({}); const { cookies } = await resolveCredentials({});
if (cookies.authToken && cookies.ct0) { if (cookies.authToken && cookies.ct0) {
process.stdout.write(cookies.source || 'authenticated'); writeStdout(cookies.source || 'authenticated');
process.exit(0); return 0;
} else {
process.stderr.write('Not authenticated\n');
process.exit(1);
} }
writeStderr('Not authenticated\n');
return 1;
} catch (err) { } catch (err) {
process.stderr.write(`Auth check failed: ${err.message}\n`); writeStderr(`Auth check failed: ${err.message}\n`);
process.exit(1); return 1;
} }
} }
@@ -72,8 +79,8 @@ for (let i = 0; i < args.length; i++) {
} }
if (!query) { if (!query) {
process.stderr.write('Usage: node bird-search.mjs <query> [--count N] [--json]\n'); writeStderr('Usage: node bird-search.mjs <query> [--count N] [--json]\n');
process.exit(1); return 1;
} }
try { try {
@@ -83,14 +90,13 @@ try {
if (!cookies.authToken || !cookies.ct0) { if (!cookies.authToken || !cookies.ct0) {
const msg = warnings.length > 0 ? warnings.join('; ') : 'No Twitter credentials found'; const msg = warnings.length > 0 ? warnings.join('; ') : 'No Twitter credentials found';
if (jsonOutput) { if (jsonOutput) {
process.stdout.write(JSON.stringify({ error: msg, items: [] })); writeStdout(JSON.stringify({ error: msg, items: [] }));
} else { } else {
process.stderr.write(`Error: ${msg}\n`); writeStderr(`Error: ${msg}\n`);
} }
process.exit(1); return 1;
} }
// Create search client
const client = new SearchClient({ const client = new SearchClient({
cookies: { cookies: {
authToken: cookies.authToken, authToken: cookies.authToken,
@@ -100,35 +106,42 @@ try {
timeoutMs: 30000, timeoutMs: 30000,
}); });
// Run search
const result = await client.search(query, count); const result = await client.search(query, count);
if (!result.success) { if (!result.success) {
if (jsonOutput) { if (jsonOutput) {
process.stdout.write(JSON.stringify({ error: result.error, items: [] })); writeStdout(JSON.stringify({ error: result.error, items: [] }));
} else { } 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 || []; const tweets = result.tweets || [];
if (jsonOutput) { if (jsonOutput) {
process.stdout.write(JSON.stringify(tweets)); writeStdout(JSON.stringify(tweets));
} else { } else {
for (const tweet of tweets) { for (const tweet of tweets) {
const author = tweet.author?.username || 'unknown'; 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) { } catch (err) {
if (jsonOutput) { if (jsonOutput) {
process.stdout.write(JSON.stringify({ error: err.message, items: [] })); writeStdout(JSON.stringify({ error: err.message, items: [] }));
} else { } 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;
} }