55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { SupabaseClient } from "@supabase/supabase-js";
|
|
import { create } from "apisauce";
|
|
import { ElevenLabs } from "elevenlabs";
|
|
|
|
const supabase = new SupabaseClient(process.env.SUPABASE_URL, process.env.SUPABASE_ANON_KEY);
|
|
const elevenLabs = new ElevenLabs(process.env.ELEVENLABS_API_KEY);
|
|
|
|
export default async function handler(req, res) {
|
|
if (req.method !== "POST") return res.status(405).end();
|
|
|
|
const { date } = req.body;
|
|
|
|
try {
|
|
// Fetch digest content
|
|
const { data: digest, error: fetchError } = await supabase
|
|
.from("digests")
|
|
.select("content")
|
|
.eq("date", date)
|
|
.single();
|
|
|
|
if (fetchError) throw fetchError;
|
|
if (!digest) return res.status(404).json({ error: "Digest not found" });
|
|
|
|
// Generate audio
|
|
const audioResponse = await elevenLabs.textToSpeech({
|
|
text: digest.content,
|
|
voice: "nova",
|
|
model: "eleven_multilingual_v2"
|
|
});
|
|
|
|
// Save to Supabase storage
|
|
const audioBuffer = await audioResponse.arrayBuffer();
|
|
const { data: { publicUrl }, error: storageError } = await supabase.storage
|
|
.from("podcasts")
|
|
.upload(`digest-${date}.mp3`, Buffer.from(audioBuffer), {
|
|
upsert: true
|
|
});
|
|
|
|
if (storageError) throw storageError;
|
|
|
|
// Update digest record
|
|
const { error: updateError } = await supabase
|
|
.from("digests")
|
|
.update({ audio_url: publicUrl })
|
|
.eq("date", date);
|
|
|
|
if (updateError) throw updateError;
|
|
|
|
res.status(200).json({ audioUrl: publicUrl });
|
|
} catch (error) {
|
|
console.error(error);
|
|
res.status(500).json({ error: "Failed to generate podcast" });
|
|
}
|
|
}
|