fix: Instagram data collection pipeline — handle normalization + DB persistence
- Add normalizeInstagramHandle() utility (Edge + browser) to strip URLs, @ prefixes - generate-report: normalize handles before saving, persist socialHandles in report JSONB - enrich-channels: normalize Instagram handle before Apify call (defense in depth) - useReport: recover socialHandles + channelEnrichment from DB on direct URL access - ReportPage: skip redundant enrichment when data already exists in DB Fixes: Instagram enrichment failing due to URL-format handles passed to Apify Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2d6e95c414
commit
bd7bc45192
6 files changed
+156
-23
No files matched your search
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Normalize an Instagram handle from various input formats to a pure username.
|
||||
*
|
||||
* Handles these formats:
|
||||
* - "https://www.instagram.com/banobagi_ps/" → "banobagi_ps"
|
||||
* - "https://instagram.com/banobagi_ps?hl=en" → "banobagi_ps"
|
||||
* - "http://instagram.com/banobagi_ps" → "banobagi_ps"
|
||||
* - "instagram.com/banobagi_ps" → "banobagi_ps"
|
||||
* - "@banobagi_ps" → "banobagi_ps"
|
||||
* - "banobagi_ps" → "banobagi_ps"
|
||||
* - null / undefined / "" → null
|
||||
*/
|
||||
export function normalizeInstagramHandle(
|
||||
raw: string | null | undefined,
|
||||
): string | null {
|
||||
if (!raw || typeof raw !== "string") return null;
|
||||
|
||||
let handle = raw.trim();
|
||||
if (!handle) return null;
|
||||
|
||||
// If it contains "instagram.com", extract the first path segment
|
||||
if (handle.includes("instagram.com")) {
|
||||
try {
|
||||
// Add protocol if missing so URL constructor works
|
||||
const urlStr = handle.startsWith("http")
|
||||
? handle
|
||||
: `https://${handle}`;
|
||||
const url = new URL(urlStr);
|
||||
// pathname is like "/banobagi_ps/" or "/banobagi_ps"
|
||||
const segments = url.pathname.split("/").filter(Boolean);
|
||||
handle = segments[0] || "";
|
||||
} catch {
|
||||
// URL parsing failed — try regex fallback
|
||||
const match = handle.match(/instagram\.com\/([^/?#]+)/);
|
||||
handle = match?.[1] || "";
|
||||
}
|
||||
}
|
||||
|
||||
// Strip leading @
|
||||
if (handle.startsWith("@")) {
|
||||
handle = handle.slice(1);
|
||||
}
|
||||
|
||||
// Strip trailing slash
|
||||
handle = handle.replace(/\/+$/, "");
|
||||
|
||||
return handle || null;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import "@supabase/functions-js/edge-runtime.d.ts";
|
||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
|
||||
import { normalizeInstagramHandle } from "../_shared/normalizeHandles.ts";
|
||||
|
||||
const corsHeaders = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
@@ -58,12 +59,13 @@ Deno.serve(async (req) => {
|
||||
const tasks = [];
|
||||
|
||||
// 1. Instagram Profile
|
||||
if (instagramHandle) {
|
||||
const cleanIgHandle = normalizeInstagramHandle(instagramHandle);
|
||||
if (cleanIgHandle) {
|
||||
tasks.push(
|
||||
(async () => {
|
||||
const items = await runApifyActor(
|
||||
"apify~instagram-profile-scraper",
|
||||
{ usernames: [instagramHandle], resultsLimit: 12 },
|
||||
{ usernames: [cleanIgHandle], resultsLimit: 12 },
|
||||
APIFY_TOKEN
|
||||
);
|
||||
const profile = (items as Record<string, unknown>[])[0];
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import "@supabase/functions-js/edge-runtime.d.ts";
|
||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
|
||||
import { normalizeInstagramHandle } from "../_shared/normalizeHandles.ts";
|
||||
|
||||
const corsHeaders = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
@@ -153,6 +154,18 @@ ${JSON.stringify(analyzeResult.data?.analysis || {}, null, 2)}
|
||||
report = { raw: reportText, parseError: true };
|
||||
}
|
||||
|
||||
// Normalize social handles from scrape data
|
||||
const socialMedia = clinic.socialMedia || {};
|
||||
const normalizedHandles = {
|
||||
instagram: normalizeInstagramHandle(socialMedia.instagram),
|
||||
youtube: socialMedia.youtube || null,
|
||||
facebook: socialMedia.facebook || null,
|
||||
blog: socialMedia.blog || null,
|
||||
};
|
||||
|
||||
// Embed normalized handles in report for DB persistence
|
||||
report.socialHandles = normalizedHandles;
|
||||
|
||||
// Save to Supabase
|
||||
const supabase = createClient(supabaseUrl, supabaseKey);
|
||||
const { data: saved, error: saveError } = await supabase
|
||||
@@ -167,9 +180,6 @@ ${JSON.stringify(analyzeResult.data?.analysis || {}, null, 2)}
|
||||
.select("id")
|
||||
.single();
|
||||
|
||||
// Extract social handles from scrape data for frontend enrichment
|
||||
const socialMedia = clinic.socialMedia || {};
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
@@ -184,12 +194,7 @@ ${JSON.stringify(analyzeResult.data?.analysis || {}, null, 2)}
|
||||
marketAnalysis: analyzeResult.success,
|
||||
aiGeneration: !report.parseError,
|
||||
},
|
||||
socialHandles: {
|
||||
instagram: socialMedia.instagram || null,
|
||||
youtube: socialMedia.youtube || null,
|
||||
facebook: socialMedia.facebook || null,
|
||||
blog: socialMedia.blog || null,
|
||||
},
|
||||
socialHandles: normalizedHandles,
|
||||
address,
|
||||
services,
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user