sofia-cloud / src /app /api /trends /route.ts
Gmagl
Fix: All API routes corrected - no fs/path, proper typing
30ad7ef
raw
history blame
2.54 kB
import { NextRequest, NextResponse } from "next/server";
import ZAI from "z-ai-web-dev-sdk";
import { db } from "@/lib/db";
const TRENDS = [
{ platform: "Instagram", name: "#aesthetic", growth: 15.5, category: "lifestyle" },
{ platform: "TikTok", name: "Storytime", growth: 32.1, category: "narrative" },
{ platform: "YouTube", name: "Shorts", growth: 25.6, category: "video" },
{ platform: "Instagram", name: "Pets content", growth: 55.8, category: "pets" }
];
const VIRAL_STRATEGIES = [
{ name: "Hook en 3 segundos", successRate: 85, platforms: ["tiktok", "instagram"] },
{ name: "Storytime con suspenso", successRate: 78, platforms: ["tiktok"] },
{ name: "Pet Reveal", successRate: 88, platforms: ["instagram", "tiktok"] }
];
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const platform = searchParams.get("platform");
const includePets = searchParams.get("includePets") === "true";
let trends = [...TRENDS];
if (platform) trends = trends.filter(t => t.platform.toLowerCase() === platform.toLowerCase());
if (includePets) trends = trends.filter(t => t.category === "pets");
trends.sort((a, b) => b.growth - a.growth);
return NextResponse.json({ success: true, trends, viralStrategies: VIRAL_STRATEGIES });
} catch {
return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { niche, platform } = body;
const zai = await ZAI.create();
const completion = await zai.chat.completions.create({
messages: [
{ role: "system", content: "Eres analista de tendencias. Responde JSON con {currentTrends:[], recommendations:[], predictedViralPotential:0-100}" },
{ role: "user", content: "Analiza tendencias para nicho: " + (niche || "general") + " plataforma: " + (platform || "todas") }
]
});
let analysis: Record<string, unknown> = {};
try {
const match = completion.choices[0]?.message?.content?.match(/\{[\s\S]*\}/);
if (match) analysis = JSON.parse(match[0]);
} catch {}
await db.trend.create({
data: { platform: platform || "all", type: "analysis", name: "Analisis " + (niche || "general"), contentIdeas: JSON.stringify(analysis) }
}).catch(() => {});
return NextResponse.json({ success: true, analysis });
} catch {
return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
}
}