feat(data): 添加新食谱数据至 JSON 文件

This commit is contained in:
worry 2025-12-19 10:39:09 +08:00
parent ae70567ff5
commit 836e2f3161
4 changed files with 52894 additions and 19 deletions

52878
all_recipes.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "howtocook-mcp",
"version": "0.1.1",
"version": "0.2.0",
"type": "module",
"main": "build/index.js",
"bin": {
@ -8,6 +8,7 @@
},
"files": [
"build",
"all_recipes.json",
"README.md"
],
"scripts": {

View File

@ -1,24 +1,20 @@
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import { Recipe } from '../types/index.js'
// 远程菜谱JSON文件URL
const RECIPES_URL = 'https://weilei.site/all_recipes.json'
// 获取本地JSON文件路径
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const LOCAL_RECIPES_PATH = join(__dirname, '../../all_recipes.json')
// 从远程URL获取数据的异步函数
export async function fetchRecipes(): Promise<Recipe[]> {
// 从本地文件加载菜谱数据
export function fetchRecipes(): Recipe[] {
try {
// 使用fetch API获取远程数据
const response = await fetch(RECIPES_URL)
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`)
}
// 解析JSON数据
const data = await response.json()
return data as Recipe[]
const data = readFileSync(LOCAL_RECIPES_PATH, 'utf-8')
return JSON.parse(data) as Recipe[]
} catch (error) {
console.error('获取远程菜谱数据失败:', error)
// 直接返回空数组,不尝试使用本地备份
console.error('加载菜谱数据失败:', error)
return []
}
}

View File

@ -63,9 +63,9 @@ function createServerInstance(): McpServer {
}
// 加载菜谱数据
async function loadRecipeData() {
function loadRecipeData() {
try {
recipes = await fetchRecipes();
recipes = fetchRecipes();
categories = getAllCategories(recipes);
console.log(`📚 已加载 ${recipes.length} 个菜谱`);
} catch (error) {