This commit is contained in:
imnyang 2025-05-31 01:07:25 +09:00
commit 6c4f5f799e
No known key found for this signature in database
GPG key ID: 356406A02D4AFA55
5 changed files with 416 additions and 11 deletions

View file

@ -1,7 +1,7 @@
import path from "path";
import { join } from "path";
import { createCanvas, loadImage, GlobalFonts } from "@napi-rs/canvas";
import { getMealInfo, NameToEmoji } from "./meal"; // 이 함수의 내용은 제공되지 않았으므로 그대로 둡니다.
import { getMealInfo, getNutritionInfo, NameToEmoji, removeNutritionInfo } from "./meal"; // 이 함수의 내용은 제공되지 않았으므로 그대로 둡니다.
import { getAllSchedules } from "./schedule";
import { isVTS } from "./vts";
@ -9,7 +9,7 @@ GlobalFonts.registerFromPath('./template/Pretendard-Bold.ttf', 'Pretendard Bold'
GlobalFonts.registerFromPath('./template/NotoColorEmoji-Regular.ttf', 'NotoColorEmoji Regular')
export class CreateImage {
static async PostMeal(MLSV_YMD: string): Promise<void> {
static async PostMeal(MLSV_YMD: string): Promise<{ [key: string]: any } | undefined> {
const mealInfo = await getMealInfo(MLSV_YMD);
const img = await loadImage(path.join("./template/skeleton.png"));
const canvas = createCanvas(img.width, img.height);
@ -20,7 +20,7 @@ export class CreateImage {
ctx.fillStyle = "white";
ctx.textAlign = "left";
const lines = mealInfo.meal.split("\n").reverse();
const lines = removeNutritionInfo(mealInfo.meal).split("\n").reverse();
let emojis = (await NameToEmoji(lines.toString())).split(",");
if (lines.length !== emojis.length) {
@ -65,6 +65,16 @@ export class CreateImage {
console.log("🍲 | Meal Info Image Saved");
} catch (error) {
console.error("Error saving meal info image:", error);
} finally {
const meals = removeNutritionInfo(mealInfo.meal).split("\n");
const nutritionInfo = getNutritionInfo(mealInfo.meal);
const retrunValue: { [key: string]: any } = {};
for (let i = 0; i < meals.length; i++) {
const meal = (meals[i] ?? "").trim();
retrunValue[meal] = nutritionInfo[i] || [];
}
return retrunValue;
}
}

View file

@ -5,11 +5,31 @@ const KEY = process.env.NEIS_API_KEY;
export function removeNutritionInfo(value: string): string {
const lines = value.trim().split('\n');
const cleanedLines = lines.map(line => line.replace(/\(.*?\)/g, '').trim());
const cleanedLines = lines.map(line => line.replace(/\s*\([\d.,]+\)/g, '').trim());
const result = cleanedLines.join('\n');
return result;
}
const nutritionList = [
"난류", "우유", "메밀", "땅콩", "대두", "밀", "고등어", "게", "새우", "돼지고기",
"복숭아", "토마토", "아황산류", "호두", "닭고기", "쇠고기", "오징어", "조개류(굴, 전복, 홍합 포함)", "잣"
];
export function getNutritionInfo(value: string): string[][] {
const lines = value.trim().split('\n');
return lines.map(line => {
const indexes = line
.replace(/[()\s]/g, "")
.split(".")
.map(v => parseInt(v, 10) - 1)
.filter(i => i >= 0 && i < nutritionList.length);
return indexes
.map(i => nutritionList[i])
.filter((item): item is string => typeof item === "string");
});
}
export async function getMealInfo(MLSV_YMD: string): Promise<{ meal: string; date: string, kcal: string }> {
const url = `https://open.neis.go.kr/hub/mealServiceDietInfo?Type=json&ATPT_OFCDC_SC_CODE=E10&SD_SCHUL_CODE=7331071&MLSV_YMD=${MLSV_YMD}&KEY=${KEY}`;
const response = await fetch(url);
@ -17,7 +37,7 @@ export async function getMealInfo(MLSV_YMD: string): Promise<{ meal: string; dat
// @ts-ignore
const DDISH_NM = data.mealServiceDietInfo[1].row[0].DDISH_NM;
return {
meal: removeNutritionInfo(DDISH_NM.replace(/<br\s*\/?>/gi, '\n')),
meal: DDISH_NM.replace(/<br\s*\/?>/gi, '\n'),
date: MLSV_YMD,
// @ts-ignore
@ -32,7 +52,7 @@ export async function NameToEmoji(name: string): Promise<string> {
throw new Error("GITHUB_TOKEN environment variable is not set.");
}
const endpoint = "https://models.github.ai/inference";
const model = "openai/gpt-4.1";
const model = "openai/gpt-4.1-mini";
const client = ModelClient(
endpoint,