Bilibili to IMA Note
Overview
Fetch Bilibili video metadata via official API, generate structured markdown notes, and save them to IMA (ima知识库) notes via the IMA OpenAPI. When a note already exists for the same video, append the new content and video link.
Prerequisites
IMA Credentials
IMA credentials must be stored at one of:
~/.config/ima/client_id— Client ID~/.config/ima/api_key— API Key
Or set as environment variables:
IMA_OPENAPI_CLIENTIDIMA_OPENAPI_APIKEY
IMA API Script
The IMA OpenAPI wrapper script is located at the pre-installed ima-skill:
~/.workbuddy/skills/skill_2053082144792322048/ima_api.cjs
Always use the full absolute path to this script. Do NOT rely on PATH.
Node.js Runtime
Use the managed Node.js binary:
C:Users枉凝眉.workbuddybinariesnodeversions22.22.2node.exe
Workflow
Step 1: Extract BV ID and Fetch Video Info
Given a Bilibili URL (e.g. https://www.bilibili.com/video/BV1ctjm67EiY/), extract the BV ID (the BV followed by alphanumeric characters in the URL path).
Fetch video metadata from the official Bilibili API:
https://api.bilibili.com/x/web-interface/view?bvid={BV_ID}
Key fields to extract from the response data object:
title— video titleowner.name— UP主 nameowner.mid— UP主 midduration— video duration in secondsdesc— video descriptionstat— stats (view, like, favorite, coin, share, reply)tid/tname— categorypubdate— publish timestamp (Unix)
Step 2: Generate Structured Notes
Generate markdown notes covering the following structure:
# {Title}
> **视频信息**
> - UP 主:{name}
> - 时长:{duration}秒({minutes}分{seconds}秒)
> - 来源:Bilibili({bvid})
> - 标签:{tags}
---
## 内容概要
[Generate a comprehensive summary of the video based on the title and description.
If description is limited, infer likely content from the title and category.]
---
## 关键知识点
[Organize key points from the video into structured sections with bullet points.]
---
## 总结
[1-2 paragraph summary of the key takeaways.]
Step 3: Save to IMA
Use the IMA OpenAPI to create a note:
node "{IMA_API_SCRIPT}" "openapi/note/v1/import_doc" '{"content_format": 1, "content": "{MARKDOWN_CONTENT_ESCAPED}"}' '{"clientId":"{CLIENT_ID}","apiKey":"{API_KEY}"}'
IMPORTANT — Proper escaping: The content field contains markdown with special characters. Use one of these approaches:
Approach A — Node.js script (preferred): Write a small .cjs script that reads the content from a file, constructs the JSON body programmatically, then calls imaApi by requiring ima_api.cjs directly.
Example:
const path = require('path');
const SKILL_DIR = 'C:/Users/枉凝眉/.workbuddy/skills/skill_2053082144792322048';
const { imaApi } = require(path.join(SKILL_DIR, 'ima_api.cjs'));
const opts = {
clientId: 'XXXX',
apiKey: 'XXXX'
};
(async () => {
const r = await imaApi('openapi/note/v1/import_doc', {
content_format: 1,
content: '... markdown ...'
}, opts);
console.log('RESULT:', r);
})().catch(e => console.error('ERR:', e.message || e.msg || e));
Approach B — Search then append (if note may already exist): Before creating, search for existing notes by title using search_note. If found, use append_doc instead of import_doc to add content to the existing note.
Search:
openapi/note/v1/search_note
Body: {"search_type": 0, "query_info": {"title": "关键词"}, "start": 0, "end": 5}
Append (if note exists):
openapi/note/v1/append_doc
Body: {"note_id": "EXISTING_NOTE_ID", "content_format": 1, "content": "nn---nn[追加的新内容]"}
Step 4: Append Video Link
After creating the note, append the video link at the end:
openapi/note/v1/append_doc
Body: {"note_id": "NOTE_ID", "content_format": 1, "content": "nn---nn> **视频链接**:[点击观看原视频]({VIDEO_URL})"}
Credential Handling
Credentials are read by ima_api.cjs automatically from:
- Options JSON passed as 3rd argument (highest priority)
- Environment variables
IMA_CLIENT_ID/IMA_API_KEY/IMA_OPENAPI_CLIENTID/IMA_OPENAPI_APIKEY - Files
~/.config/ima/client_idand~/.config/ima/api_key
When calling directly, pass credentials via the options JSON in the 3rd argument. When credentials are stored in the config files, the third argument can be '{}' or omitted entirely.
Error Handling
- Bilibili API fails: Try fetching again. If the API returns
code !== 0, report the error message to the user. - IMA API fails (
-100programmatic error): Check credentials first, then retry. - IMA API fails (
-200update available): Follow the update instruction to update the ima-skill, then retry. - Content too large: Split into multiple
append_doccalls. - PowerShell encoding: If running in PowerShell 5.1, the request body will be silently converted to GBK. Either use Git Bash, PowerShell 7+, or the UTF-8 byte array workaround. See the ima-skill SKILL.md for details.
Platform Notes
- Git Bash (Bash tool): Use forward slashes for paths. The managed node binary works with full paths.
- PowerShell: PowerShell 5.1 silently encodes request body as GBK. Use UTF-8 byte array mode or switch to Git Bash. Prefer Git Bash for IMA API calls.
- Python managed runtime: Available at
C:Users枉凝眉.workbuddybinariespythonversions3.13.12python.exe - Node.js managed runtime: Available at
C:Users枉凝眉.workbuddybinariesnodeversions22.22.2node.exe



