Skip to content

Commit f70cfea

Browse files
committed
feat: add yaml output for agent workflows
1 parent a5f78a0 commit f70cfea

13 files changed

Lines changed: 238 additions & 92 deletions

File tree

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ A CLI for Bilibili — browse videos, users, favorites from the terminal 📺
2020
- 📂 **Favorites** — browse favorite folders, watch-later, and watch history
2121
- 👍 **Interactions** — like, coin, triple (一键三连)
2222
- 🔐 **Smart auth** — auto-extracts cookies from Chrome/Firefox, or QR code login
23-
- 📊 **JSON output** — major query commands support `--json` for scripting
23+
- 📊 **Structured output** — major query commands support `--yaml` and `--json`
2424

2525
## Installation
2626

@@ -67,6 +67,7 @@ bili video BV1ABcsztEcY --subtitle # With subtitles
6767
bili video BV1ABcsztEcY --ai # AI summary
6868
bili video BV1ABcsztEcY --comments # Top comments
6969
bili video BV1ABcsztEcY --related # Related videos
70+
bili video BV1ABcsztEcY --yaml # Agent-friendly YAML
7071
bili video BV1ABcsztEcY --json # Raw JSON
7172

7273
# Users
@@ -127,6 +128,24 @@ Audio extraction requires the optional `audio` dependency group (`av`).
127128

128129
bilibili-cli ships with a [`SKILL.md`](./SKILL.md) that teaches AI agents how to use it.
129130

131+
### Agent Output Recommendation
132+
133+
If an AI agent needs machine-readable output, prefer `--yaml` first:
134+
135+
- `--yaml` is usually more token-efficient than pretty-printed JSON
136+
- It is still easy to parse for agents and scripts
137+
- Keep `--json` for `jq`, strict JSON-only tooling, or exact downstream schemas
138+
139+
Examples:
140+
141+
```bash
142+
bili video BV1ABcsztEcY --yaml
143+
bili hot --max 5 --yaml
144+
bili user-videos 946974 --max 3 --yaml
145+
```
146+
147+
For agent usage, also prefer narrower queries (`--max`, `--page`, `--offset`) to avoid wasting context on oversized payloads.
148+
130149
### Claude Code / Antigravity
131150

132151
```bash
@@ -169,7 +188,7 @@ All bilibili-cli commands are available in OpenClaw after installation.
169188
- 📂 **收藏** — 收藏夹浏览、稍后再看、观看历史
170189
- 👍 **互动** — 点赞、投币、一键三连
171190
- 🔐 **智能认证** — 自动提取浏览器 Cookie,或扫码登录
172-
- 📊 **JSON 输出** — 主要查询命令支持 `--json`,方便脚本调用
191+
- 📊 **结构化输出** — 主要查询命令支持 `--yaml``--json`
173192

174193
## 安装
175194

@@ -208,6 +227,8 @@ bili video BV1ABcsztEcY --subtitle # 显示字幕
208227
bili video BV1ABcsztEcY --ai # AI 总结
209228
bili video BV1ABcsztEcY --comments # 热门评论
210229
bili video BV1ABcsztEcY --related # 相关推荐
230+
bili video BV1ABcsztEcY --yaml # 适合 AI Agent 的 YAML
231+
bili video BV1ABcsztEcY --json # 原始 JSON
211232

212233
# 用户
213234
bili user 946974 # UP 主资料
@@ -259,6 +280,24 @@ bilibili-cli 采用三级认证策略:
259280

260281
大部分命令无需登录。字幕、收藏夹、动态和互动操作需要登录。写操作(like/coin/triple/unfollow/dynamic-post/dynamic-delete)需要可写凭证(包含 `bili_jct`)。
261282

283+
## AI Agent 使用建议
284+
285+
如果 AI Agent 需要机器可读输出,默认优先 `--yaml`
286+
287+
- `--yaml` 通常比格式化 JSON 更省 token
288+
- 对 agent 来说仍然容易解析
289+
- 只有在要配合 `jq` 或下游必须是 JSON 时,再使用 `--json`
290+
291+
示例:
292+
293+
```bash
294+
bili video BV1ABcsztEcY --yaml
295+
bili hot --max 5 --yaml
296+
bili user-videos 946974 --max 3 --yaml
297+
```
298+
299+
另外,agent 应尽量配合 `--max``--page``--offset` 缩小结果集,避免把不必要的数据带进上下文。
300+
262301
音频提取功能需要安装可选依赖组 `audio`(即 `av`)。
263302

264303
## 作为 AI Agent Skill 使用

SKILL.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: bilibili-cli
3-
description: CLI skill for Bilibili (哔哩哔哩, B站) to browse videos, users, search, trending, dynamics, favorites, and interactions from the terminal
3+
description: CLI skill for Bilibili (哔哩哔哩, B站) with token-efficient YAML output for AI agents to browse videos, users, search, trending, dynamics, favorites, and interactions from the terminal
44
author: jackwener
55
version: "1.0.0"
66
tags:
@@ -16,6 +16,15 @@ tags:
1616

1717
A CLI tool for interacting with Bilibili (哔哩哔哩). Use it to fetch video info, search content, browse user profiles, and perform interactions like liking or triple-clicking.
1818

19+
## Agent Defaults
20+
21+
When you need machine-readable output:
22+
23+
1. Prefer `--yaml` first because it is usually more token-efficient than pretty JSON.
24+
2. Use `--json` only when downstream tooling strictly requires JSON.
25+
3. Keep result sets small with `--max`, `--page`, or `--offset`.
26+
4. Prefer specific commands over broad ones. Example: use `bili user-videos 946974 --max 3 --yaml` instead of fetching large timelines.
27+
1928
## Prerequisites
2029

2130
```bash
@@ -53,6 +62,7 @@ bili video BV1ABcsztEcY --subtitle # Show subtitles (AI or uploaded)
5362
bili video BV1ABcsztEcY --ai # Show B站 AI summary
5463
bili video BV1ABcsztEcY --comments # Show top comments
5564
bili video BV1ABcsztEcY --related # Show related videos
65+
bili video BV1ABcsztEcY --yaml # Token-efficient YAML output
5666
bili video BV1ABcsztEcY --json # Raw JSON output
5767
```
5868

@@ -65,7 +75,7 @@ bili user "影视飓风"
6575

6676
# List user's videos
6777
bili user-videos 946974 --max 20
68-
bili user-videos "影视飓风" --json
78+
bili user-videos "影视飓风" --yaml
6979
```
7080

7181
### Search
@@ -134,19 +144,20 @@ bili unfollow 946974 # Unfollow by UID
134144
```bash
135145
bili status # Quick login check
136146
bili whoami # Detailed profile info
147+
bili whoami --yaml # Profile as YAML
137148
bili whoami --json # Profile as JSON
138149
bili login # QR code login
139150
bili logout # Clear credentials
140151
```
141152

142-
## JSON Output
153+
## Structured Output
143154

144-
Major query commands support `--json` for machine-readable output:
155+
Major query commands support both `--yaml` and `--json` for machine-readable output. Prefer YAML for agent use:
145156

146157
```bash
147-
bili video BV1ABcsztEcY --json | jq '.stat.view' # Get view count
148-
bili hot --json | jq '.list[0].title' # First trending title
149-
bili user 946974 --json | jq '.user_info.name' # Username
158+
bili video BV1ABcsztEcY --yaml # Preferred for AI agents
159+
bili hot --max 5 --yaml # Smaller, token-efficient payload
160+
bili user 946974 --json | jq '.user_info.name' # JSON when jq is needed
150161
```
151162

152163
## Debugging
@@ -171,14 +182,14 @@ bili video BV1ABcsztEcY --comments
171182
# Segments are saved to /tmp/bilibili-cli/{title}/seg_000.wav, seg_001.wav, ...
172183
bili audio BV1ABcsztEcY --segment 25
173184

174-
# Find a user's latest video BV ID
175-
bili user-videos 946974 --max 1 --json | python3 -c "import sys,json; print(json.load(sys.stdin)[0]['bvid'])"
185+
# Find a user's latest video BV ID with minimal payload
186+
bili user-videos 946974 --max 1 --yaml
176187

177188
# Check if logged in before performing actions
178189
bili status && bili like BV1ABcsztEcY
179190

180-
# Search and get first result
181-
bili search "topic" --type video --json | python3 -c "import sys,json; r=json.load(sys.stdin); print(r[0]['bvid'] if r else 'not found')"
191+
# Search and inspect the first few results
192+
bili search "topic" --type video --max 3 --yaml
182193
```
183194

184195
### Workflow: Video Content Analysis

bili_cli/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
Usage:
44
bili login / logout / status / whoami
5-
bili video <BV号或URL> [--subtitle] [--ai] [--comments] [--related] [--json]
5+
bili video <BV号或URL> [--subtitle] [--ai] [--comments] [--related] [--yaml|--json]
66
bili user <UID或用户名> bili user-videos <UID> [--max N]
7-
bili search <关键词> [--type user|video] [--json]
7+
bili search <关键词> [--type user|video] [--yaml|--json]
88
bili hot / rank / feed / my-dynamics / following / history / watch-later / favorites
99
bili dynamic-post <TEXT> / dynamic-delete <动态ID>
1010
bili like / coin / triple <BV号> / unfollow <UID>

bili_cli/commands/account.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from __future__ import annotations
44

5-
import json
6-
75
import click
86
from rich.panel import Panel
97

@@ -42,11 +40,14 @@ def status():
4240

4341

4442
@click.command()
45-
@click.option("--json", "as_json", is_flag=True, help="输出原始 JSON。")
46-
def whoami(as_json: bool):
43+
@click.option("--json", "as_json", is_flag=True, help="输出 JSON。")
44+
@click.option("--yaml", "as_yaml", is_flag=True, help="输出 YAML,推荐给 AI Agent。")
45+
def whoami(as_json: bool, as_yaml: bool):
4746
"""查看当前登录用户的详细信息。"""
4847
from .. import client
4948

49+
output_format = common.resolve_output_format(as_json=as_json, as_yaml=as_yaml)
50+
5051
cred = common.require_login(message="未登录。使用 [bold]bili login[/bold] 登录。")
5152

5253
info = common.run_or_exit(client.get_self_info(cred), "获取用户信息失败")
@@ -56,8 +57,7 @@ def whoami(as_json: bool):
5657
"获取用户信息失败",
5758
)
5859

59-
if as_json:
60-
click.echo(json.dumps({"info": info, "relation": relation}, ensure_ascii=False, indent=2))
60+
if common.emit_structured({"info": info, "relation": relation}, output_format):
6161
return
6262

6363
name = info.get("name", "unknown")

0 commit comments

Comments
 (0)