data-sync
构建前从外部接口拉数据写成集合 JSON:scripts/sync 运行器与数据源协议、重试退避与缓存、安全落盘,含 B 站投稿同步与软件版本数据。触发词:数据同步、下载页、版本数据、安装包、更新日志、versions.json、拉取接口、抓数据、外部API、B站、bilibili、GitHub…
by bkywksj★ 55 GitHub stars
Install data-sync in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
数据同步(next-ai-astro)
何时使用
- 站点内容来自外部平台(视频平台、代码托管、博客 RSS、商品接口)
- 要写一个新的数据源 / 改字段映射
- 同步失败、接口被限流 / 风控(B 站
-352/-412)、数据突然变少 - 要定时更新数据(本机计划任务 / 服务器 cron / CI)
- 做 B 站视频归类站、作品集同步(WBI 签名、合集映射分类)
概述
外部数据不在构建时直连,而是先由 scripts/sync 拉取、校验、落成 src/data/*.json 并提交,构建只读本地文件。
这样做的原因:构建可复现(同一提交构建出同一站点)、对方接口挂了不影响发版、数据变化能在 git diff 里审查。
外部接口 ──(pnpm sync)──▶ 数据源映射 ──▶ 校验 ──▶ 原子写入 src/data/xxx.json ──(git commit)──▶ pnpm build
失败 / 空 / 骤降 → 不写入,保留上一版
本框架现状
| 文件 | 作用 |
|---|---|
scripts/sync/run.mjs |
运行器:加载数据源、校验、原子写、稳定排序、退出码 |
scripts/sync/context.mjs |
数据源上下文:request / fetchJson / cache / env / log / sleep |
scripts/sync/sources/example.mjs |
示例数据源(enabled: false,不联网) |
.env.example |
数据源需要的环境变量模板(SYNC_*) |
.cache/sync/<源名>/ |
缓存目录(gitignore,可随时删) |
命令
pnpm sync # 运行全部 enabled !== false 的数据源
pnpm sync bilibili # 只跑点名的数据源(enabled: false 的也能点名跑)
pnpm sync bilibili --dry-run # 只拉取与校验,不写文件,报告是否有差异
pnpm sync --allow-fail # 有数据源失败也返回 0(给 CI 的「尽力同步」用)
pnpm sync bilibili --force # 跳过「骤降 50%」保护(确认是真实删减时)
数据源协议
// scripts/sync/sources/<name>.mjs
export default {
name: 'works', // 命令行用它点名
output: 'src/data/works.json', // 写入目标(对应 content.config.ts 里的 file() loader)
enabled: true, // false = 只有点名时才跑
/** @param {ReturnType<import('../context.mjs').createContext>} ctx */
async fetch(ctx) {
return [/* 每条必须有唯一 id,字段符合集合 schema */];
},
};
文件名以 _ 开头的模块不会被加载,可用来放共享工具(如 _bilibili-wbi.mjs)。
四条安全规则(运行器已实现,数据源不用重复写)
- 拉取失败 / 返回空数组 / id 重复 → 不写文件,保留上一版,站点绝不会被构建成空页面
- 条目数比上一版骤降超过 50%(上一版 ≥ 10 条时)→ 视为异常不写入,需人工确认后
--force - 先写
.tmp再 rename:中断不会留下半截 JSON - 按 id 稳定排序输出:数据没变时 git diff 为空,有变化时 diff 可读
context 工具
| 方法 | 说明 |
|---|---|
ctx.request(url, { retries, timeoutMs, backoffMs, headers }) |
超时(默认 15s)+ 重试(默认 3 次,指数退避从 1s 起)。429 / 5xx / 网络错误重试;其余 4xx 直接失败 |
ctx.fetchJson(url, options) |
同上并解析 JSON |
ctx.cache(key).read(maxAgeMs) / .write(value) |
JSON 文件缓存,减少重复请求(签名密钥、详情接口) |
ctx.env('SYNC_XXX') |
读必填环境变量,缺失时报明确错误(会自动读项目根 .env) |
ctx.sleep(ms) / ctx.log(...) |
限速、带数据源前缀的日志 |
标准做法
1. 写一个数据源
- 复制
scripts/sync/sources/example.mjs改名,改name/output。 fetch()里:请求 → 检查业务状态码 → 分页取全 → 映射字段。pnpm sync <name> --dry-run看条数与差异,再正式运行,git diff src/data/审查后提交。
2. 字段映射规范
映射目标是 src/content.config.ts 的 schema,逐条对齐:
| 目标字段 | 规则 |
|---|---|
id |
平台稳定 id(BV 号、repo 名),只含 [A-Za-z0-9_-];不要用标题做 id(改标题就断链) |
title |
原样,去首尾空白 |
summary |
取简介前 80~120 字(按字符截,中文不乱码);简介为空时写真实的兜底句,不编造 |
description |
完整简介,保留换行(详情页按空行分段) |
date |
ISO 字符串;平台给秒级时间戳时 new Date(sec * 1000).toISOString() |
cover |
http:// 统一改 https://;域名加进 site.imageDomains 让构建期转存(见 image-optimization) |
category |
映射到 categories.json 里已存在的分类 id;未映射的归到兜底分类并 ctx.log 提示 |
metrics |
只放平台真实返回的数字。保留键:views = 播放 / 浏览次数(非负整数),duration = 时长秒(非负整数);其他键自定(见 content-collections 3.1) |
分类名和分类导语是 SEO 文案,应由人写在
src/data/categories.json;同步脚本只负责把条目映射到分类,
不自动生成分类描述。
3. 业务状态码必须检查
很多平台(包括 B 站)HTTP 状态是 200,真正的结果在 JSON 的 code 里。不检查就会把错误响应当成空列表:
const json = await ctx.fetchJson(url, { headers });
if (json.code !== 0) {
throw new Error(`接口返回 code=${json.code} message=${json.message}`);
}
抛错后运行器会保留上一版数据。
4. 分页与限速
- 按平台返回的总数(
count/total)判断何时停,不要「拿到空页才停」(空页也可能是被限流)。 - 页与页之间
await ctx.sleep(1000~2000),一次同步只跑一个平台的全量。 - 详情类接口(每条一个请求)务必用
ctx.cache(id)缓存,数据没变的条目不重复请求。
5. 什么时候跑、在哪跑
| 方式 | 适合 | 注意 |
|---|---|---|
本机手动 pnpm sync |
数据更新不频繁(每周几次) | 最稳:本机是家用网络,平台风控最宽松(经验判断,见下方 B 站节) |
| 本机计划任务 | 每天定时 | 同步后仍需构建 + 部署,建议串成一个脚本 |
| 国内服务器 cron | 无人值守 | 服务器 IP 属于数据中心,部分平台更容易触发风控 |
| CI(GitHub Actions 等) | 开源数据、无风控接口 | 海外 IP,访问国内平台接口不稳定 |
同步 → 构建 → 部署三步不要合并进 Dockerfile(镜像构建要可复现),见 deployment-guide。
B 站 UP 主投稿同步(示例,接真实项目时使用)
🔴 以下接口来自社区整理的非官方文档 bilibili-API-collect,B 站随时可能调整。
原仓库 SocialSisterYi/bilibili-API-collect 在编写本技能时(2026-09)已归档(archived),默认分支改为deprecated、
只剩 README 与一张说明图,文档文件与 issues 链接均为 404;
以下内容核对自其镜像 pskdje/bilibili-API-collect(docs/user/space.md、docs/video/collection.md、docs/misc/sign/wbi.md)。
实现前先用--dry-run实测,与文档不符以实测为准并更新本节。
接口清单
| 用途 | 接口 | 鉴权 | 来源 |
|---|---|---|---|
| 获取 WBI 密钥 | GET https://api.bilibili.com/x/web-interface/nav → data.wbi_img.img_url / sub_url |
无需登录(未登录也返回两个 key,code 为 -101) | wbi.md |
| UP 主投稿列表(主力) | GET https://api.bilibili.com/x/space/wbi/arc/search |
WBI 签名 | space.md「查询用户投稿视频明细」 |
| 投稿列表(备选) | GET https://api.bilibili.com/x/series/recArchivesByKeywords |
无签名;space.md 在投稿列表一节注明它「功能基本相同, 暂未发现风控校验」,但实测同样会被 HTTP 412 拦截(见下方「实测记录」) | 参数与返回见 collection.md「根据关键词查找视频」 |
| 合集内视频 | `GET https://api.bilibili.com/x/polymer/web-space/seasons_archives_ |
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the data-sync skill do?
构建前从外部接口拉数据写成集合 JSON:scripts/sync 运行器与数据源协议、重试退避与缓存、安全落盘,含 B 站投稿同步与软件版本数据。触发词:数据同步、下载页、版本数据、安装包、更新日志、versions.json、拉取接口、抓数据、外部API、B站、bilibili、GitHub API、RSS、定时更新数据、scripts/sync、数据源、防风控
How do I install the data-sync skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the data-sync skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
skill-creator
AnthropicCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
discernment-nudge
AnthropicAfter you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
data-consolidate
AnthropicJoins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
inspection-to-capa
AnthropicRuns the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…