/**
* CBETA API Client
* 用于与 CBETA Data API 交互的客户端
*/
const CBETA_API_BASE = "https://cbdata.dila.edu.tw/v1.2";
export interface SearchResult {
work_id: string;
title: string;
juan: string;
hits: number;
content_preview?: string;
}
export interface WorkInfo {
work_id: string;
title: string;
byline?: string;
juan_count: number;
category?: string;
}
export interface CatalogItem {
id: string;
label: string;
children?: CatalogItem[];
}
export interface JuanContent {
work_id: string;
juan: number;
title: string;
html: string;
}
/**
* 搜索佛典经文
*/
export async function searchSutra(
query: string,
options: {
scope?: string;
page?: number;
per_page?: number;
} = {}
): Promise<{ results: SearchResult[]; total: number }> {
const params = new URLSearchParams({
q: query,
page: String(options.page || 1),
per_page: String(options.per_page || 10),
});
if (options.scope) {
params.set("scope", options.scope);
}
const url = `${CBETA_API_BASE}/search?${params.toString()}`;
try {
const response = await fetch(url, {
headers: {
"Referer": "cbeta-mcp",
"Accept": "application/json",
},
});
if (!response.ok) {
throw new Error(`Search failed: ${response.status} ${response.statusText}`);
}
const data = await response.json() as { results?: SearchResult[]; num_found?: number };
return {
results: data.results || [],
total: data.num_found || 0,
};
} catch (error) {
// 如果 API 不可用,返回模拟数据用于测试
console.error("Search API error:", error);
return {
results: [],
total: 0,
};
}
}
/**
* 获取佛典作品信息
*/
export async function getWorkInfo(workId: string): Promise<WorkInfo | null> {
const url = `${CBETA_API_BASE}/works/${workId}`;
try {
const response = await fetch(url, {
headers: {
"Referer": "cbeta-mcp",
"Accept": "application/json",
},
});
if (!response.ok) {
return null;
}
return await response.json() as WorkInfo;
} catch (error) {
console.error("Get work info error:", error);
return null;
}
}
/**
* 获取指定卷的 HTML 内容
*/
export async function getJuanHtml(
workId: string,
juan: number = 1
): Promise<JuanContent | null> {
const url = `${CBETA_API_BASE}/juans/${workId}/${juan}?work_info=1`;
try {
const response = await fetch(url, {
headers: {
"Referer": "cbeta-mcp",
"Accept": "application/json",
},
});
if (!response.ok) {
return null;
}
const data = await response.json() as { title?: string; html?: string };
return {
work_id: workId,
juan: juan,
title: data.title || "",
html: data.html || "",
};
} catch (error) {
console.error("Get juan HTML error:", error);
return null;
}
}
/**
* 获取藏经目录
*/
export async function getCatalog(
catalogId: string = "CBETA"
): Promise<CatalogItem[]> {
const url = `${CBETA_API_BASE}/catalog/${catalogId}`;
try {
const response = await fetch(url, {
headers: {
"Referer": "cbeta-mcp",
"Accept": "application/json",
},
});
if (!response.ok) {
// 返回基本目录结构
return getDefaultCatalog();
}
const data = await response.json() as { children?: CatalogItem[] };
return data.children || [];
} catch (error) {
console.error("Get catalog error:", error);
return getDefaultCatalog();
}
}
/**
* 获取默认目录(当 API 不可用时)
*/
function getDefaultCatalog(): CatalogItem[] {
return [
{ id: "T", label: "大正新脩大藏經" },
{ id: "X", label: "卍新纂續藏經" },
{ id: "N", label: "漢譯南傳大藏經" },
{ id: "B", label: "大藏經補編" },
{ id: "D", label: "國家圖書館善本佛典" },
{ id: "Y", label: "印順法師佛學著作集" },
{ id: "LC", label: "呂澂佛學著作集" },
{ id: "TX", label: "太虛大師全書" },
{ id: "ZW", label: "藏外佛教文獻" },
{ id: "ZS", label: "正史佛教資料類編" },
];
}
/**
* 将 HTML 转换为纯文本
*/
export function htmlToText(html: string): string {
// 简单的 HTML 标签移除
return html
.replace(/<[^>]+>/g, "")
.replace(/ /g, " ")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/&/g, "&")
.replace(/\s+/g, " ")
.trim();
}