res-x

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

X/Twitter Fetch & Search

X/Twitter 内容抓取与搜索

Fetch tweet content by URL and search X posts using xAI Responses API with Grok's
x_search
tool. Solves the problem of X/Twitter blocking WebFetch with "JavaScript disabled" errors.
借助Grok的
x_search
工具,通过xAI Responses API实现通过URL抓取推文内容、搜索X帖子的功能,解决了X/Twitter以「JavaScript未启用」错误拦截WebFetch抓取的问题。

Architecture

架构

CapabilityAPI ToolCost
Fetch tweet by URLxAI
x_search
~$0.005/call
Search XxAI
x_search
~$0.005/call
Fetching batches URLs in groups of 3 per API call to reduce cost.
能力API工具成本
按URL抓取推文xAI
x_search
单次调用约$0.005
搜索X内容xAI
x_search
单次调用约$0.005
每次API调用将URL按3个一组批量处理以降低成本。

Prerequisites

前置条件

Required Tools

所需工具

ToolPurposeInstall
uvPython package manager (handles dependencies)
curl -LsSf https://astral.sh/uv/install.sh | sh
工具用途安装方式
uvPython包管理器(处理依赖)
curl -LsSf https://astral.sh/uv/install.sh | sh

API Key

API密钥

ServicePurposeRequiredGet Key
xAIX/Twitter access via GrokYeshttps://console.x.ai
This skill requires an xAI API key. There is no fallback mode.
服务用途是否必填密钥获取地址
xAI通过Grok访问X/Twitterhttps://console.x.ai
本工具需要xAI API密钥,无备用运行模式。

Keychain Setup (One-Time)

密钥链设置(仅需配置一次)

bash
undefined
bash
undefined

1. Create a dedicated keychain (skip if already exists)

1. 创建专用密钥链(如果已存在可跳过)

security create-keychain -p 'YourPassword' ~/Library/Keychains/claude-keys.keychain-db
security create-keychain -p 'YourPassword' ~/Library/Keychains/claude-keys.keychain-db

2. Add keychain to search list

2. 将密钥链添加到搜索列表

security list-keychains -s ~/Library/Keychains/claude-keys.keychain-db ~/Library/Keychains/login.keychain-db /Library/Keychains/System.keychain
security list-keychains -s ~/Library/Keychains/claude-keys.keychain-db ~/Library/Keychains/login.keychain-db /Library/Keychains/System.keychain

3. Store your xAI API key

3. 存储你的xAI API密钥

echo -n "Enter xAI API key: " && read -s key && security add-generic-password -s "xai-api" -a "$USER" -w "$key" ~/Library/Keychains/claude-keys.keychain-db && unset key && echo

Before using: `security unlock-keychain ~/Library/Keychains/claude-keys.keychain-db`
echo -n "Enter xAI API key: " && read -s key && security add-generic-password -s "xai-api" -a "$USER" -w "$key" ~/Library/Keychains/claude-keys.keychain-db && unset key && echo

使用前执行:`security unlock-keychain ~/Library/Keychains/claude-keys.keychain-db`

Script Usage

脚本用法

bash
undefined
bash
undefined

Fetch single tweet

抓取单条推文

uv run scripts/x_fetch.py fetch "https://x.com/user/status/123456"
uv run scripts/x_fetch.py fetch "https://x.com/user/status/123456"

Fetch multiple tweets (batched, 3 per API call)

抓取多条推文(批量处理,每次API调用处理3条)

uv run scripts/x_fetch.py fetch "url1" "url2" "url3" "url4" "url5"
uv run scripts/x_fetch.py fetch "url1" "url2" "url3" "url4" "url5"

Force one-per-URL for max fidelity

强制每个URL单独调用以获得最高保真度

uv run scripts/x_fetch.py fetch "url1" "url2" --single
uv run scripts/x_fetch.py fetch "url1" "url2" --single

Search X

搜索X内容

uv run scripts/x_fetch.py search "query terms" uv run scripts/x_fetch.py search "query terms" --quick
uv run scripts/x_fetch.py search "query terms" uv run scripts/x_fetch.py search "query terms" --quick

JSON output (both commands)

输出JSON格式结果(上述两个命令均支持)

uv run scripts/x_fetch.py fetch "url" --json uv run scripts/x_fetch.py search "query" --json
undefined
uv run scripts/x_fetch.py fetch "url" --json uv run scripts/x_fetch.py search "query" --json
undefined

Workflow

工作流程

Step 0: Detect xAI Key (Mandatory)

步骤0:检测xAI密钥(必填)

Run before every invocation:
bash
security find-generic-password -s "xai-api" -w ~/Library/Keychains/claude-keys.keychain-db 2>/dev/null && echo "XAI_AVAILABLE=true" || echo "XAI_AVAILABLE=false"
If
XAI_AVAILABLE=false
, report that this skill requires an xAI key and show the keychain setup instructions above.
每次调用前运行:
bash
security find-generic-password -s "xai-api" -w ~/Library/Keychains/claude-keys.keychain-db 2>/dev/null && echo "XAI_AVAILABLE=true" || echo "XAI_AVAILABLE=false"
如果
XAI_AVAILABLE=false
,提示本工具需要xAI密钥并展示上述密钥链设置指引。

Step 1: Detect Intent

步骤1:识别意图

  • URLs present in user input or referenced note -> fetch
  • Query text only -> search
  • 用户输入或引用的笔记中存在URL → 执行抓取
  • 仅包含查询文本 → 执行搜索

Step 2: Execute

步骤2:执行操作

For fetch:
  1. Extract all X/Twitter URLs from user input or referenced file
  2. Run the script with all URLs as arguments
  3. The script batches them (3 per API call) automatically
For search:
  1. Run the script with the search query
  2. Use
    --quick
    for fast overview, omit for deeper results
抓取场景:
  1. 从用户输入或引用的文件中提取所有X/Twitter URL
  2. 将所有URL作为参数运行脚本
  3. 脚本自动将URL按3个一组批量处理
搜索场景:
  1. 携带搜索查询词运行脚本
  2. 如需快速概览使用
    --quick
    参数,如需更深度的结果则省略该参数

Step 3: Present Results

步骤3:展示结果

  • For fetch: present tweet-by-tweet with full content, engagement, thread/quote context
  • For search: present as a list with engagement metrics
  • 抓取场景:逐推文展示完整内容、互动数据、推文串/引用上下文
  • 搜索场景:以列表形式展示附带互动指标的结果

URL Patterns Accepted

支持的URL格式

https://x.com/{user}/status/{id}
https://twitter.com/{user}/status/{id}
https://x.com/{user}/status/{id}?s=20
https://x.com/{user}/status/{id}?t=...&s=...
https://x.com/i/article/{id}
Tweets normalized to
https://x.com/{user}/status/{id}
, articles to
https://x.com/i/article/{id}
before processing.
https://x.com/{user}/status/{id}
https://twitter.com/{user}/status/{id}
https://x.com/{user}/status/{id}?s=20
https://x.com/{user}/status/{id}?t=...&s=...
https://x.com/i/article/{id}
处理前会将推文统一格式化为
https://x.com/{user}/status/{id}
,文章统一格式化为
https://x.com/i/article/{id}

Batch Processing

批量处理

For processing saved X links from a file (e.g., Obsidian daily note):
  1. Read the file content
  2. Extract all X/Twitter URLs (tweets:
    https?://(?:x\.com|twitter\.com)/\w+/status/\d+
    , articles:
    https?://(?:x\.com|twitter\.com)/i/article/[\w\-]+
    )
  3. Pass all URLs to the script:
    uv run scripts/x_fetch.py fetch "url1" "url2" ...
  4. Present results organized by URL
如需处理保存在文件(例如Obsidian每日笔记)中的X链接:
  1. 读取文件内容
  2. 提取所有X/Twitter URL(推文匹配规则:
    https?://(?:x\.com|twitter\.com)/\w+/status/\d+
    ,文章匹配规则:
    https?://(?:x\.com|twitter\.com)/i/article/[\w\-]+
  3. 将所有URL传递给脚本:
    uv run scripts/x_fetch.py fetch "url1" "url2" ...
  4. 按URL分类展示结果

Cost

成本说明

ActionAPI CallsCost
Fetch 1-3 tweets1~$0.005
Fetch 4-6 tweets2~$0.010
Fetch 10 tweets4~$0.020
Fetch 10 tweets (--single)10~$0.050
X search1~$0.005
X search (--quick)1~$0.005
操作API调用次数成本
抓取1-3条推文1约$0.005
抓取4-6条推文2约$0.010
抓取10条推文4约$0.020
抓取10条推文(使用--single参数)10约$0.050
X搜索1约$0.005
X搜索(使用--quick参数)1约$0.005

Constraints

使用约束

DO:
  • Run Step 0 before every invocation
  • Validate URLs before calling the script
  • Use
    --json
    when parsing results programmatically
  • Present full tweet content without truncation
DON'T:
  • Try WebFetch on X URLs (fails with JS disabled)
  • Skip the xAI key check
  • Use this for general web search (use
    res-web
    or
    res-deep
    )
允许的操作:
  • 每次调用前执行步骤0的密钥检测
  • 调用脚本前校验URL有效性
  • 程序化解析结果时使用
    --json
    参数
  • 完整展示推文内容不做截断
禁止的操作:
  • 尝试对X URL使用WebFetch(会因JavaScript未启用报错失败)
  • 跳过xAI密钥检测步骤
  • 将本工具用于通用网页搜索(请使用
    res-web
    res-deep

Troubleshooting

故障排查

xAI key not found:
bash
security find-generic-password -s "xai-api" ~/Library/Keychains/claude-keys.keychain-db
If not found, run keychain setup above.
Keychain locked:
bash
security unlock-keychain ~/Library/Keychains/claude-keys.keychain-db
Script errors: Ensure uv is installed:
which uv
找不到xAI密钥:
bash
security find-generic-password -s "xai-api" ~/Library/Keychains/claude-keys.keychain-db
如果未查询到结果,请运行上述密钥链设置步骤。
密钥链已锁定:
bash
security unlock-keychain ~/Library/Keychains/claude-keys.keychain-db
脚本报错: 确认已安装uv:
which uv