fix(twitter): support both twitter-cli and bird CLI, never uninstall user tools

CRITICAL FIX: update.md was telling agents to uninstall bird CLI,
which broke users who had working bird installations. Now:

- twitter.py: prefers twitter-cli, falls back to bird/birdx if installed
- update.md: removed "clean up deprecated tools" step entirely
- Added explicit rule: "Never uninstall any existing tools the user already has"
- Tests cover twitter-cli primary + bird fallback + preference order

78 tests passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pnant
2026-03-31 23:01:09 +08:00
parent 7acf3c1367
commit e178470320
3 changed files with 94 additions and 35 deletions
+35 -6
View File
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Twitter/X — check if twitter-cli (public-clis/twitter-cli) is available."""
"""Twitter/X — check if twitter-cli or bird CLI is available."""
import shutil
import subprocess
@@ -9,7 +9,7 @@ from .base import Channel
class TwitterChannel(Channel):
name = "twitter"
description = "Twitter/X 推文"
backends = ["twitter-cli"]
backends = ["twitter-cli", "bird CLI (legacy)"]
tier = 1
def can_handle(self, url: str) -> bool:
@@ -18,24 +18,32 @@ class TwitterChannel(Channel):
return "x.com" in d or "twitter.com" in d
def check(self, config=None):
# Prefer twitter-cli, fallback to bird/birdx
twitter = shutil.which("twitter")
if not twitter:
bird = shutil.which("bird") or shutil.which("birdx")
if twitter:
return self._check_twitter_cli(twitter)
elif bird:
return self._check_bird(bird)
else:
return "warn", (
"twitter-cli 未安装。安装方式:\n"
"Twitter CLI 未安装。安装方式:\n"
" pipx install twitter-cli\n"
"或:\n"
" uv tool install twitter-cli"
)
def _check_twitter_cli(self, binary: str):
try:
r = subprocess.run(
[twitter, "status"], capture_output=True,
[binary, "status"], capture_output=True,
encoding="utf-8", errors="replace", timeout=10
)
output = (r.stdout or "") + (r.stderr or "")
if r.returncode == 0 and "ok: true" in output:
return "ok", (
"完整可用(搜索、读推文、时间线、长文/Article、"
"twitter-cli 完整可用(搜索、读推文、时间线、长文/Article、"
"用户查询、Thread"
)
if "not_authenticated" in output:
@@ -51,3 +59,24 @@ class TwitterChannel(Channel):
)
except Exception:
return "warn", "twitter-cli 已安装但连接失败"
def _check_bird(self, binary: str):
try:
r = subprocess.run(
[binary, "check"], capture_output=True,
encoding="utf-8", errors="replace", timeout=10
)
output = (r.stdout or "") + (r.stderr or "")
if r.returncode == 0:
return "ok", "bird CLI 可用(读取、搜索推文,含长文/X Article)"
if "Missing credentials" in output or "missing" in output.lower():
return "warn", (
"bird CLI 已安装但未配置认证。设置环境变量:\n"
" export AUTH_TOKEN=\"xxx\"\n"
" export CT0=\"yyy\""
)
return "warn", (
"bird CLI 已安装但认证检查失败。"
)
except Exception:
return "warn", "bird CLI 已安装但连接失败"