liurenchaxin/query-shushu-book.js

138 lines
3.6 KiB
JavaScript

// 查询术数书内容的脚本
// 通过 Hyperdrive API 查询 NeonDB 中的术数书数据
const API_BASE_URL = 'https://hyperdrive.seekkey.tech';
// 通用请求函数
async function apiRequest(endpoint, options = {}) {
const url = `${API_BASE_URL}${endpoint}`;
const headers = {
'Content-Type': 'application/json',
...options.headers
};
try {
const response = await fetch(url, {
...options,
headers
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return await response.json();
} else {
return await response.text();
}
} catch (error) {
console.error(`Request failed for ${endpoint}:`, error.message);
throw error;
}
}
// 查询数据库表结构
async function queryTables() {
console.log('\n📋 查询数据库表结构...');
try {
const result = await apiRequest('/query-tables');
console.log('✅ 数据库表:', result);
return result;
} catch (error) {
console.log('❌ 查询表结构失败:', error.message);
return null;
}
}
// 查询术数书内容
async function queryShushuBook(limit = 10) {
console.log('\n📚 查询术数书内容...');
try {
const result = await apiRequest(`/query-shushu?limit=${limit}`);
console.log('✅ 术数书内容:', JSON.stringify(result, null, 2));
return result;
} catch (error) {
console.log('❌ 查询术数书失败:', error.message);
return null;
}
}
// 搜索术数书内容
async function searchShushuBook(keyword, limit = 5) {
console.log(`\n🔍 搜索术数书内容: "${keyword}"...`);
try {
const result = await apiRequest(`/search-shushu?q=${encodeURIComponent(keyword)}&limit=${limit}`);
console.log('✅ 搜索结果:', JSON.stringify(result, null, 2));
return result;
} catch (error) {
console.log('❌ 搜索失败:', error.message);
return null;
}
}
// 获取术数书统计信息
async function getShushuStats() {
console.log('\n📊 获取术数书统计信息...');
try {
const result = await apiRequest('/shushu-stats');
console.log('✅ 统计信息:', JSON.stringify(result, null, 2));
return result;
} catch (error) {
console.log('❌ 获取统计信息失败:', error.message);
return null;
}
}
// 主函数
async function main() {
console.log('🚀 术数书查询脚本');
console.log('==================');
// 首先测试连接
console.log('\n🔗 测试 Hyperdrive 连接...');
try {
const connectionTest = await apiRequest('/test-connection');
console.log('✅ 连接成功:', connectionTest.message);
} catch (error) {
console.log('❌ 连接失败:', error.message);
return;
}
// 查询表结构
await queryTables();
// 获取统计信息
await getShushuStats();
// 查询术数书内容
await queryShushuBook(5);
// 搜索示例
await searchShushuBook('易经');
await searchShushuBook('八卦');
await searchShushuBook('太公');
}
// 如果是 Node.js 环境,导入 fetch
if (typeof window === 'undefined') {
// Node.js 环境
const { default: fetch } = require('node-fetch');
global.fetch = fetch;
main().catch(console.error);
} else {
// 浏览器环境
console.log('在浏览器控制台中运行: main()');
}
// 导出函数供其他模块使用
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
queryTables,
queryShushuBook,
searchShushuBook,
getShushuStats,
main
};
}