feat: real API integration + YouTube Data API v3 + progressive loading
- Replace mock useReport() with real Supabase API data pipeline - Add transformReport.ts to map API responses to MarketingReport type - Add useEnrichment() hook for background channel data enrichment - Replace Apify YouTube scraper with YouTube Data API v3 - Add mergeEnrichment() for progressive data loading - Add EmptyState component for graceful empty data handling - Add socialHandles to generate-report metadata - Graceful empty data in ClinicSnapshot, YouTube, Instagram, Facebook - Add Supabase Edge Functions and DB migrations - Add developer handoff documentation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
c6e18b6a67
commit
60cd055042
30 files changed
+2905
-142
No files matched your search
@@ -0,0 +1,3 @@
|
||||
# Configuration for private npm package dependencies
|
||||
# For more information on using private registries with Edge Functions, see:
|
||||
# https://supabase.com/docs/guides/functions/import-maps#importing-from-private-registries
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"imports": {
|
||||
"@supabase/functions-js": "jsr:@supabase/functions-js@^2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
import "@supabase/functions-js/edge-runtime.d.ts";
|
||||
|
||||
const corsHeaders = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Headers":
|
||||
"authorization, x-client-info, apikey, content-type",
|
||||
};
|
||||
|
||||
interface AnalyzeRequest {
|
||||
clinicName: string;
|
||||
services: string[];
|
||||
address: string;
|
||||
scrapeData?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
if (req.method === "OPTIONS") {
|
||||
return new Response("ok", { headers: corsHeaders });
|
||||
}
|
||||
|
||||
try {
|
||||
const { clinicName, services, address } =
|
||||
(await req.json()) as AnalyzeRequest;
|
||||
|
||||
const PERPLEXITY_API_KEY = Deno.env.get("PERPLEXITY_API_KEY");
|
||||
if (!PERPLEXITY_API_KEY) {
|
||||
throw new Error("PERPLEXITY_API_KEY not configured");
|
||||
}
|
||||
|
||||
// Run multiple Perplexity queries in parallel
|
||||
const queries = [
|
||||
{
|
||||
id: "competitors",
|
||||
prompt: `${address} 근처 ${services.slice(0, 3).join(", ")} 전문 성형외과/피부과 경쟁 병원 5곳을 분석해줘. 각 병원의 이름, 주요 시술, 온라인 평판, 마케팅 채널(블로그, 인스타그램, 유튜브)을 포함해줘. JSON 형식으로 응답해줘.`,
|
||||
},
|
||||
{
|
||||
id: "keywords",
|
||||
prompt: `한국 ${services.slice(0, 3).join(", ")} 관련 검색 키워드 트렌드를 분석해줘. 네이버와 구글에서 월간 검색량이 높은 키워드 20개, 경쟁 강도, 추천 롱테일 키워드를 JSON 형식으로 제공해줘.`,
|
||||
},
|
||||
{
|
||||
id: "market",
|
||||
prompt: `한국 ${services[0] || "성형외과"} 시장 트렌드 2025-2026을 분석해줘. 시장 규모, 성장률, 주요 트렌드(비수술 시술 증가, AI 마케팅, 외국인 환자 유치 등), 마케팅 채널별 효과를 JSON 형식으로 제공해줘.`,
|
||||
},
|
||||
{
|
||||
id: "targetAudience",
|
||||
prompt: `${clinicName || services[0] + " 병원"}의 잠재 고객을 분석해줘. 연령대별, 성별, 관심 시술, 정보 탐색 채널(강남언니, 바비톡, 네이버, 인스타), 의사결정 요인을 JSON 형식으로 제공해줘.`,
|
||||
},
|
||||
];
|
||||
|
||||
const perplexityResults = await Promise.allSettled(
|
||||
queries.map(async (q) => {
|
||||
const response = await fetch(
|
||||
"https://api.perplexity.ai/chat/completions",
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${PERPLEXITY_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "sonar",
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content:
|
||||
"You are a Korean medical marketing analyst. Always respond in Korean. Provide data in valid JSON format when requested.",
|
||||
},
|
||||
{ role: "user", content: q.prompt },
|
||||
],
|
||||
temperature: 0.3,
|
||||
}),
|
||||
}
|
||||
);
|
||||
const data = await response.json();
|
||||
return {
|
||||
id: q.id,
|
||||
content: data.choices?.[0]?.message?.content || "",
|
||||
citations: data.citations || [],
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
const analysis: Record<string, unknown> = {};
|
||||
for (const result of perplexityResults) {
|
||||
if (result.status === "fulfilled") {
|
||||
const { id, content, citations } = result.value;
|
||||
let parsed = content;
|
||||
const jsonMatch = content.match(/```json\n?([\s\S]*?)```/);
|
||||
if (jsonMatch) {
|
||||
try {
|
||||
parsed = JSON.parse(jsonMatch[1]);
|
||||
} catch {
|
||||
// Keep as string if JSON parse fails
|
||||
}
|
||||
}
|
||||
analysis[id] = { data: parsed, citations };
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
data: { clinicName, services, address, analysis, analyzedAt: new Date().toISOString() },
|
||||
}),
|
||||
{ headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
} catch (error) {
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, error: error.message }),
|
||||
{ status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
# Configuration for private npm package dependencies
|
||||
# For more information on using private registries with Edge Functions, see:
|
||||
# https://supabase.com/docs/guides/functions/import-maps#importing-from-private-registries
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"imports": {
|
||||
"@supabase/functions-js": "jsr:@supabase/functions-js@^2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
import "@supabase/functions-js/edge-runtime.d.ts";
|
||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
|
||||
|
||||
const corsHeaders = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Headers":
|
||||
"authorization, x-client-info, apikey, content-type",
|
||||
};
|
||||
|
||||
const APIFY_BASE = "https://api.apify.com/v2";
|
||||
|
||||
interface EnrichRequest {
|
||||
reportId: string;
|
||||
clinicName: string;
|
||||
instagramHandle?: string;
|
||||
youtubeChannelId?: string;
|
||||
address?: string;
|
||||
}
|
||||
|
||||
async function runApifyActor(
|
||||
actorId: string,
|
||||
input: Record<string, unknown>,
|
||||
token: string
|
||||
): Promise<unknown[]> {
|
||||
const res = await fetch(
|
||||
`${APIFY_BASE}/acts/${actorId}/runs?token=${token}&waitForFinish=120`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(input),
|
||||
}
|
||||
);
|
||||
const run = await res.json();
|
||||
const datasetId = run.data?.defaultDatasetId;
|
||||
if (!datasetId) return [];
|
||||
|
||||
const itemsRes = await fetch(
|
||||
`${APIFY_BASE}/datasets/${datasetId}/items?token=${token}&limit=20`
|
||||
);
|
||||
return itemsRes.json();
|
||||
}
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
if (req.method === "OPTIONS") {
|
||||
return new Response("ok", { headers: corsHeaders });
|
||||
}
|
||||
|
||||
try {
|
||||
const { reportId, clinicName, instagramHandle, youtubeChannelId, address } =
|
||||
(await req.json()) as EnrichRequest;
|
||||
|
||||
const APIFY_TOKEN = Deno.env.get("APIFY_API_TOKEN");
|
||||
if (!APIFY_TOKEN) throw new Error("APIFY_API_TOKEN not configured");
|
||||
|
||||
const enrichment: Record<string, unknown> = {};
|
||||
|
||||
// Run all enrichment tasks in parallel
|
||||
const tasks = [];
|
||||
|
||||
// 1. Instagram Profile
|
||||
if (instagramHandle) {
|
||||
tasks.push(
|
||||
(async () => {
|
||||
const items = await runApifyActor(
|
||||
"apify~instagram-profile-scraper",
|
||||
{ usernames: [instagramHandle], resultsLimit: 12 },
|
||||
APIFY_TOKEN
|
||||
);
|
||||
const profile = (items as Record<string, unknown>[])[0];
|
||||
if (profile && !profile.error) {
|
||||
enrichment.instagram = {
|
||||
username: profile.username,
|
||||
followers: profile.followersCount,
|
||||
following: profile.followsCount,
|
||||
posts: profile.postsCount,
|
||||
bio: profile.biography,
|
||||
isBusinessAccount: profile.isBusinessAccount,
|
||||
externalUrl: profile.externalUrl,
|
||||
latestPosts: ((profile.latestPosts as Record<string, unknown>[]) || [])
|
||||
.slice(0, 12)
|
||||
.map((p) => ({
|
||||
type: p.type,
|
||||
likes: p.likesCount,
|
||||
comments: p.commentsCount,
|
||||
caption: (p.caption as string || "").slice(0, 200),
|
||||
timestamp: p.timestamp,
|
||||
})),
|
||||
};
|
||||
}
|
||||
})()
|
||||
);
|
||||
}
|
||||
|
||||
// 2. Google Maps / Place Reviews
|
||||
if (clinicName || address) {
|
||||
tasks.push(
|
||||
(async () => {
|
||||
const searchQuery = `${clinicName} ${address || "강남"}`;
|
||||
const items = await runApifyActor(
|
||||
"compass~crawler-google-places",
|
||||
{
|
||||
searchStringsArray: [searchQuery],
|
||||
maxCrawledPlacesPerSearch: 1,
|
||||
language: "ko",
|
||||
maxReviews: 10,
|
||||
},
|
||||
APIFY_TOKEN
|
||||
);
|
||||
const place = (items as Record<string, unknown>[])[0];
|
||||
if (place) {
|
||||
enrichment.googleMaps = {
|
||||
name: place.title,
|
||||
rating: place.totalScore,
|
||||
reviewCount: place.reviewsCount,
|
||||
address: place.address,
|
||||
phone: place.phone,
|
||||
website: place.website,
|
||||
category: place.categoryName,
|
||||
openingHours: place.openingHours,
|
||||
topReviews: ((place.reviews as Record<string, unknown>[]) || [])
|
||||
.slice(0, 10)
|
||||
.map((r) => ({
|
||||
stars: r.stars,
|
||||
text: (r.text as string || "").slice(0, 200),
|
||||
publishedAtDate: r.publishedAtDate,
|
||||
})),
|
||||
};
|
||||
}
|
||||
})()
|
||||
);
|
||||
}
|
||||
|
||||
// 3. YouTube Channel (using YouTube Data API v3)
|
||||
if (youtubeChannelId) {
|
||||
const YOUTUBE_API_KEY = Deno.env.get("YOUTUBE_API_KEY");
|
||||
if (YOUTUBE_API_KEY) {
|
||||
tasks.push(
|
||||
(async () => {
|
||||
const YT_BASE = "https://www.googleapis.com/youtube/v3";
|
||||
|
||||
// Resolve handle/username to channel ID
|
||||
let channelId = youtubeChannelId;
|
||||
if (channelId.startsWith("@") || !channelId.startsWith("UC")) {
|
||||
// Use forHandle for @handles, forUsername for legacy usernames
|
||||
const param = channelId.startsWith("@") ? "forHandle" : "forUsername";
|
||||
const handle = channelId.startsWith("@") ? channelId.slice(1) : channelId;
|
||||
const lookupRes = await fetch(
|
||||
`${YT_BASE}/channels?part=id&${param}=${handle}&key=${YOUTUBE_API_KEY}`
|
||||
);
|
||||
const lookupData = await lookupRes.json();
|
||||
channelId = lookupData.items?.[0]?.id || "";
|
||||
}
|
||||
|
||||
if (!channelId) return;
|
||||
|
||||
// Step 1: Get channel statistics & snippet (1 quota unit)
|
||||
const channelRes = await fetch(
|
||||
`${YT_BASE}/channels?part=snippet,statistics,brandingSettings&id=${channelId}&key=${YOUTUBE_API_KEY}`
|
||||
);
|
||||
const channelData = await channelRes.json();
|
||||
const channel = channelData.items?.[0];
|
||||
|
||||
if (!channel) return;
|
||||
|
||||
const stats = channel.statistics || {};
|
||||
const snippet = channel.snippet || {};
|
||||
|
||||
// Step 2: Get recent/popular videos (100 quota units)
|
||||
const searchRes = await fetch(
|
||||
`${YT_BASE}/search?part=snippet&channelId=${channelId}&order=viewCount&type=video&maxResults=10&key=${YOUTUBE_API_KEY}`
|
||||
);
|
||||
const searchData = await searchRes.json();
|
||||
const videoIds = (searchData.items || [])
|
||||
.map((item: Record<string, unknown>) => (item.id as Record<string, string>)?.videoId)
|
||||
.filter(Boolean)
|
||||
.join(",");
|
||||
|
||||
// Step 3: Get video details — views, likes, duration (1 quota unit)
|
||||
let videos: Record<string, unknown>[] = [];
|
||||
if (videoIds) {
|
||||
const videosRes = await fetch(
|
||||
`${YT_BASE}/videos?part=snippet,statistics,contentDetails&id=${videoIds}&key=${YOUTUBE_API_KEY}`
|
||||
);
|
||||
const videosData = await videosRes.json();
|
||||
videos = videosData.items || [];
|
||||
}
|
||||
|
||||
enrichment.youtube = {
|
||||
channelId,
|
||||
channelName: snippet.title,
|
||||
handle: snippet.customUrl || youtubeChannelId,
|
||||
description: snippet.description?.slice(0, 500),
|
||||
publishedAt: snippet.publishedAt,
|
||||
thumbnailUrl: snippet.thumbnails?.default?.url,
|
||||
subscribers: parseInt(stats.subscriberCount || "0", 10),
|
||||
totalViews: parseInt(stats.viewCount || "0", 10),
|
||||
totalVideos: parseInt(stats.videoCount || "0", 10),
|
||||
videos: videos.slice(0, 10).map((v) => {
|
||||
const vs = v.statistics as Record<string, string> || {};
|
||||
const vSnippet = v.snippet as Record<string, unknown> || {};
|
||||
const vContent = v.contentDetails as Record<string, string> || {};
|
||||
return {
|
||||
title: vSnippet.title,
|
||||
views: parseInt(vs.viewCount || "0", 10),
|
||||
likes: parseInt(vs.likeCount || "0", 10),
|
||||
comments: parseInt(vs.commentCount || "0", 10),
|
||||
date: vSnippet.publishedAt,
|
||||
duration: vContent.duration,
|
||||
url: `https://www.youtube.com/watch?v=${(v.id as string)}`,
|
||||
thumbnail: (vSnippet.thumbnails as Record<string, Record<string, string>>)?.medium?.url,
|
||||
};
|
||||
}),
|
||||
};
|
||||
})()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.allSettled(tasks);
|
||||
|
||||
// Save enrichment data to Supabase
|
||||
const supabaseUrl = Deno.env.get("SUPABASE_URL")!;
|
||||
const supabaseKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
|
||||
const supabase = createClient(supabaseUrl, supabaseKey);
|
||||
|
||||
if (reportId) {
|
||||
// Get existing report
|
||||
const { data: existing } = await supabase
|
||||
.from("marketing_reports")
|
||||
.select("report")
|
||||
.eq("id", reportId)
|
||||
.single();
|
||||
|
||||
if (existing) {
|
||||
const updatedReport = {
|
||||
...existing.report,
|
||||
channelEnrichment: enrichment,
|
||||
enrichedAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
await supabase
|
||||
.from("marketing_reports")
|
||||
.update({ report: updatedReport, updated_at: new Date().toISOString() })
|
||||
.eq("id", reportId);
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
data: enrichment,
|
||||
enrichedAt: new Date().toISOString(),
|
||||
}),
|
||||
{ headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
} catch (error) {
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, error: error.message }),
|
||||
{ status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
# Configuration for private npm package dependencies
|
||||
# For more information on using private registries with Edge Functions, see:
|
||||
# https://supabase.com/docs/guides/functions/import-maps#importing-from-private-registries
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"imports": {
|
||||
"@supabase/functions-js": "jsr:@supabase/functions-js@^2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
import "@supabase/functions-js/edge-runtime.d.ts";
|
||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
|
||||
|
||||
const corsHeaders = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Headers":
|
||||
"authorization, x-client-info, apikey, content-type",
|
||||
};
|
||||
|
||||
interface ReportRequest {
|
||||
url: string;
|
||||
clinicName?: string;
|
||||
}
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
if (req.method === "OPTIONS") {
|
||||
return new Response("ok", { headers: corsHeaders });
|
||||
}
|
||||
|
||||
try {
|
||||
const { url, clinicName } = (await req.json()) as ReportRequest;
|
||||
|
||||
if (!url) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: "URL is required" }),
|
||||
{ status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
const PERPLEXITY_API_KEY = Deno.env.get("PERPLEXITY_API_KEY");
|
||||
if (!PERPLEXITY_API_KEY) {
|
||||
throw new Error("PERPLEXITY_API_KEY not configured");
|
||||
}
|
||||
|
||||
const supabaseUrl = Deno.env.get("SUPABASE_URL")!;
|
||||
const supabaseKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
|
||||
|
||||
// Step 1: Call scrape-website function
|
||||
const scrapeRes = await fetch(`${supabaseUrl}/functions/v1/scrape-website`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${supabaseKey}`,
|
||||
},
|
||||
body: JSON.stringify({ url, clinicName }),
|
||||
});
|
||||
const scrapeResult = await scrapeRes.json();
|
||||
|
||||
if (!scrapeResult.success) {
|
||||
throw new Error(`Scraping failed: ${scrapeResult.error}`);
|
||||
}
|
||||
|
||||
const clinic = scrapeResult.data.clinic;
|
||||
const resolvedName = clinicName || clinic.clinicName || url;
|
||||
const services = clinic.services || [];
|
||||
const address = clinic.address || "";
|
||||
|
||||
// Step 2: Call analyze-market function
|
||||
const analyzeRes = await fetch(`${supabaseUrl}/functions/v1/analyze-market`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${supabaseKey}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
clinicName: resolvedName,
|
||||
services,
|
||||
address,
|
||||
scrapeData: scrapeResult.data,
|
||||
}),
|
||||
});
|
||||
const analyzeResult = await analyzeRes.json();
|
||||
|
||||
// Step 3: Generate final report with Gemini
|
||||
const reportPrompt = `
|
||||
당신은 프리미엄 의료 마케팅 전문 분석가입니다. 아래 데이터를 기반으로 종합 마케팅 인텔리전스 리포트를 생성해주세요.
|
||||
|
||||
## 수집된 데이터
|
||||
|
||||
### 병원 정보
|
||||
${JSON.stringify(scrapeResult.data, null, 2)}
|
||||
|
||||
### 시장 분석
|
||||
${JSON.stringify(analyzeResult.data?.analysis || {}, null, 2)}
|
||||
|
||||
## 리포트 형식 (반드시 아래 JSON 구조로 응답)
|
||||
|
||||
{
|
||||
"clinicInfo": {
|
||||
"name": "병원명",
|
||||
"address": "주소",
|
||||
"phone": "전화번호",
|
||||
"services": ["시술1", "시술2"],
|
||||
"doctors": [{"name": "의사명", "specialty": "전문분야"}]
|
||||
},
|
||||
"executiveSummary": "경영진 요약 (3-5문장)",
|
||||
"overallScore": 0-100,
|
||||
"channelAnalysis": {
|
||||
"naverBlog": { "score": 0-100, "status": "active|inactive|weak", "posts": 0, "recommendation": "추천사항" },
|
||||
"instagram": { "score": 0-100, "status": "active|inactive|weak", "followers": 0, "recommendation": "추천사항" },
|
||||
"youtube": { "score": 0-100, "status": "active|inactive|weak", "subscribers": 0, "recommendation": "추천사항" },
|
||||
"naverPlace": { "score": 0-100, "rating": 0, "reviews": 0, "recommendation": "추천사항" },
|
||||
"gangnamUnni": { "score": 0-100, "rating": 0, "reviews": 0, "recommendation": "추천사항" },
|
||||
"website": { "score": 0-100, "issues": [], "recommendation": "추천사항" }
|
||||
},
|
||||
"competitors": [
|
||||
{ "name": "경쟁병원명", "strengths": ["강점1"], "weaknesses": ["약점1"], "marketingChannels": ["채널1"] }
|
||||
],
|
||||
"keywords": {
|
||||
"primary": [{"keyword": "키워드", "monthlySearches": 0, "competition": "high|medium|low"}],
|
||||
"longTail": [{"keyword": "롱테일 키워드", "monthlySearches": 0}]
|
||||
},
|
||||
"targetAudience": {
|
||||
"primary": { "ageRange": "25-35", "gender": "female", "interests": ["관심사1"], "channels": ["채널1"] },
|
||||
"secondary": { "ageRange": "35-45", "gender": "female", "interests": ["관심사1"], "channels": ["채널1"] }
|
||||
},
|
||||
"recommendations": [
|
||||
{ "priority": "high|medium|low", "category": "카테고리", "title": "제목", "description": "설명", "expectedImpact": "기대 효과" }
|
||||
],
|
||||
"marketTrends": ["트렌드1", "트렌드2"]
|
||||
}
|
||||
`;
|
||||
|
||||
const aiRes = await fetch("https://api.perplexity.ai/chat/completions", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${PERPLEXITY_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "sonar",
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: "You are a Korean medical marketing analyst. Respond ONLY with valid JSON, no markdown code blocks. Always respond in Korean for text fields.",
|
||||
},
|
||||
{ role: "user", content: reportPrompt },
|
||||
],
|
||||
temperature: 0.3,
|
||||
}),
|
||||
});
|
||||
|
||||
const aiData = await aiRes.json();
|
||||
let reportText = aiData.choices?.[0]?.message?.content || "";
|
||||
// Strip markdown code blocks if present
|
||||
const jsonMatch = reportText.match(/```(?:json)?\n?([\s\S]*?)```/);
|
||||
if (jsonMatch) reportText = jsonMatch[1];
|
||||
|
||||
let report;
|
||||
try {
|
||||
report = JSON.parse(reportText);
|
||||
} catch {
|
||||
report = { raw: reportText, parseError: true };
|
||||
}
|
||||
|
||||
// Save to Supabase
|
||||
const supabase = createClient(supabaseUrl, supabaseKey);
|
||||
const { data: saved, error: saveError } = await supabase
|
||||
.from("marketing_reports")
|
||||
.insert({
|
||||
url,
|
||||
clinic_name: resolvedName,
|
||||
report,
|
||||
scrape_data: scrapeResult.data,
|
||||
analysis_data: analyzeResult.data,
|
||||
})
|
||||
.select("id")
|
||||
.single();
|
||||
|
||||
// Extract social handles from scrape data for frontend enrichment
|
||||
const socialMedia = clinic.socialMedia || {};
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
reportId: saved?.id || null,
|
||||
report,
|
||||
metadata: {
|
||||
url,
|
||||
clinicName: resolvedName,
|
||||
generatedAt: new Date().toISOString(),
|
||||
dataSources: {
|
||||
scraping: scrapeResult.success,
|
||||
marketAnalysis: analyzeResult.success,
|
||||
aiGeneration: !report.parseError,
|
||||
},
|
||||
socialHandles: {
|
||||
instagram: socialMedia.instagram || null,
|
||||
youtube: socialMedia.youtube || null,
|
||||
facebook: socialMedia.facebook || null,
|
||||
blog: socialMedia.blog || null,
|
||||
},
|
||||
address,
|
||||
services,
|
||||
},
|
||||
}),
|
||||
{ headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
} catch (error) {
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, error: error.message }),
|
||||
{ status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
# Configuration for private npm package dependencies
|
||||
# For more information on using private registries with Edge Functions, see:
|
||||
# https://supabase.com/docs/guides/functions/import-maps#importing-from-private-registries
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"imports": {
|
||||
"@supabase/functions-js": "jsr:@supabase/functions-js@^2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
import "@supabase/functions-js/edge-runtime.d.ts";
|
||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
|
||||
|
||||
const corsHeaders = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Headers":
|
||||
"authorization, x-client-info, apikey, content-type",
|
||||
};
|
||||
|
||||
interface ScrapeRequest {
|
||||
url: string;
|
||||
clinicName?: string;
|
||||
}
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
// Handle CORS preflight
|
||||
if (req.method === "OPTIONS") {
|
||||
return new Response("ok", { headers: corsHeaders });
|
||||
}
|
||||
|
||||
try {
|
||||
const { url, clinicName } = (await req.json()) as ScrapeRequest;
|
||||
|
||||
if (!url) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: "URL is required" }),
|
||||
{ status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
const FIRECRAWL_API_KEY = Deno.env.get("FIRECRAWL_API_KEY");
|
||||
if (!FIRECRAWL_API_KEY) {
|
||||
throw new Error("FIRECRAWL_API_KEY not configured");
|
||||
}
|
||||
|
||||
// Step 1: Scrape the main website
|
||||
const scrapeResponse = await fetch("https://api.firecrawl.dev/v1/scrape", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${FIRECRAWL_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
url,
|
||||
formats: ["json", "links"],
|
||||
jsonOptions: {
|
||||
prompt:
|
||||
"Extract clinic information: clinic name, address, phone number, services offered, doctors with specialties, social media links (instagram, youtube, blog, facebook), business hours, and any marketing-related content like slogans or key messages",
|
||||
schema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
clinicName: { type: "string" },
|
||||
address: { type: "string" },
|
||||
phone: { type: "string" },
|
||||
businessHours: { type: "string" },
|
||||
slogan: { type: "string" },
|
||||
services: {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
},
|
||||
doctors: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
title: { type: "string" },
|
||||
specialty: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
socialMedia: {
|
||||
type: "object",
|
||||
properties: {
|
||||
instagram: { type: "string" },
|
||||
youtube: { type: "string" },
|
||||
blog: { type: "string" },
|
||||
facebook: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
waitFor: 5000,
|
||||
}),
|
||||
});
|
||||
|
||||
const scrapeData = await scrapeResponse.json();
|
||||
|
||||
if (!scrapeData.success) {
|
||||
throw new Error(scrapeData.error || "Scraping failed");
|
||||
}
|
||||
|
||||
// Step 2: Map the site to discover all pages
|
||||
const mapResponse = await fetch("https://api.firecrawl.dev/v1/map", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${FIRECRAWL_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
url,
|
||||
limit: 50,
|
||||
}),
|
||||
});
|
||||
|
||||
const mapData = await mapResponse.json();
|
||||
|
||||
// Step 3: Search for reviews and ratings
|
||||
const searchName = clinicName || scrapeData.data?.json?.clinicName || url;
|
||||
const searchResponse = await fetch("https://api.firecrawl.dev/v1/search", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${FIRECRAWL_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
query: `${searchName} 리뷰 평점 후기 강남언니 바비톡`,
|
||||
limit: 5,
|
||||
}),
|
||||
});
|
||||
|
||||
const searchData = await searchResponse.json();
|
||||
|
||||
// Combine all data
|
||||
const result = {
|
||||
clinic: scrapeData.data?.json || {},
|
||||
siteLinks: scrapeData.data?.links || [],
|
||||
siteMap: mapData.success ? mapData.links || [] : [],
|
||||
reviews: searchData.data || [],
|
||||
scrapedAt: new Date().toISOString(),
|
||||
sourceUrl: url,
|
||||
};
|
||||
|
||||
// Save to Supabase if configured
|
||||
const supabaseUrl = Deno.env.get("SUPABASE_URL");
|
||||
const supabaseKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY");
|
||||
|
||||
if (supabaseUrl && supabaseKey) {
|
||||
const supabase = createClient(supabaseUrl, supabaseKey);
|
||||
await supabase.from("scrape_results").insert({
|
||||
url,
|
||||
clinic_name: result.clinic.clinicName || searchName,
|
||||
data: result,
|
||||
});
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify({ success: true, data: result }), {
|
||||
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
||||
});
|
||||
} catch (error) {
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, error: error.message }),
|
||||
{
|
||||
status: 500,
|
||||
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user