Initial commit - WhyRating Engine (Google Reviews Scraper)

This commit is contained in:
Alejandro Gutiérrez
2026-02-02 18:19:00 +00:00
parent 0543a08242
commit 2206ddeff2
136 changed files with 51138 additions and 855 deletions

View File

@@ -0,0 +1,63 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
const DB_URL = process.env.DATABASE_URL || 'postgresql://scraper:scraper123@localhost:5437/scraper';
// Direct database query for categories
async function fetchCategoriesFromDB() {
// For now, we'll fetch from the API server which has DB access
// In production, you might want to use a direct DB connection or cache
const response = await fetch(`${API_BASE_URL}/categories/tree`, {
cache: 'no-store',
});
if (!response.ok) {
throw new Error('Failed to fetch categories from API');
}
return response.json();
}
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams;
const search = searchParams.get('search');
const parentPath = searchParams.get('parent');
const level = searchParams.get('level');
// Build query params for backend
const params = new URLSearchParams();
if (search) params.set('search', search);
if (parentPath) params.set('parent', parentPath);
if (level) params.set('level', level);
const url = `${API_BASE_URL}/categories?${params.toString()}`;
const response = await fetch(url, {
cache: 'no-store',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
// Fallback: return mock data for development
console.error('API not available, returning mock data');
return NextResponse.json({
categories: [],
total: 0,
message: 'API not available'
});
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Error fetching categories:', error);
return NextResponse.json(
{ error: 'Failed to fetch categories', categories: [], total: 0 },
{ status: 500 }
);
}
}

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function POST(request: NextRequest) {
try {

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
// GET /api/jobs/[jobId]/compare?previous=<previousJobId>
// Returns reviews from current job with a flag indicating if they're new
@@ -16,8 +16,10 @@ export async function GET(
// Fetch current job reviews
const currentResponse = await fetch(`${API_BASE_URL}/jobs/${jobId}/reviews?limit=10000`);
if (!currentResponse.ok) {
const errorText = await currentResponse.text().catch(() => '');
console.error(`Failed to get current job reviews: ${currentResponse.status} - ${errorText}`);
return NextResponse.json(
{ error: 'Failed to get current job reviews' },
{ error: `Failed to get reviews for job ${jobId} (${currentResponse.status})` },
{ status: currentResponse.status }
);
}

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
/**
* GET /api/jobs/[jobId]/crash-report

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function GET(
request: NextRequest,

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
/**
* POST /api/jobs/[jobId]/retry

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function GET(
request: NextRequest,

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function GET(
request: NextRequest,

View File

@@ -1,6 +1,6 @@
import { NextRequest } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export const dynamic = 'force-dynamic';

View File

@@ -1,13 +1,19 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const limit = searchParams.get('limit') || '100';
const status = searchParams.get('status');
const response = await fetch(`${API_BASE_URL}/jobs?limit=${limit}`);
let url = `${API_BASE_URL}/jobs?limit=${limit}`;
if (status) {
url += `&status=${status}`;
}
const response = await fetch(url);
if (!response.ok) {
return NextResponse.json(

View File

@@ -1,6 +1,6 @@
import { NextRequest } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export const dynamic = 'force-dynamic';

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function GET(
request: NextRequest,

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function POST(
request: NextRequest,

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function GET(
request: NextRequest,

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function GET(
request: NextRequest,

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function GET(
request: NextRequest,

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function GET(
request: NextRequest,

View File

@@ -0,0 +1,42 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:8001';
/**
* Proxy route for ReviewIQ analytics endpoint.
* GET /api/pipelines/reviewiq/analytics
*/
export async function GET(request: NextRequest) {
try {
// Forward query parameters
const searchParams = request.nextUrl.searchParams;
const queryString = searchParams.toString();
const url = `${API_BASE_URL}/api/pipelines/reviewiq/analytics${queryString ? `?${queryString}` : ''}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
cache: 'no-store',
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
return NextResponse.json(
{ detail: errorData.detail || `Backend error: ${response.status}` },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('ReviewIQ analytics proxy error:', error);
return NextResponse.json(
{ detail: 'Failed to fetch analytics data' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,42 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:8001';
/**
* Proxy route for fetching spans related to an issue.
* GET /api/pipelines/reviewiq/issues/[issueId]/spans
*/
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ issueId: string }> }
) {
try {
const { issueId } = await params;
const url = `${API_BASE_URL}/api/pipelines/reviewiq/issues/${issueId}/spans`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
cache: 'no-store',
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
return NextResponse.json(
{ detail: errorData.detail || `Backend error: ${response.status}` },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Issue spans proxy error:', error);
return NextResponse.json(
{ detail: 'Failed to fetch issue spans' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,43 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE = process.env.API_URL || 'http://localhost:8001';
/**
* GET /api/pipelines/reviewiq/reviews/[reviewId]
* Proxy to backend for fetching a full review with all its spans.
*/
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ reviewId: string }> }
) {
const { reviewId } = await params;
const { searchParams } = new URL(request.url);
const source = searchParams.get('source') || 'google';
try {
const url = `${API_BASE}/api/pipelines/reviewiq/reviews/${encodeURIComponent(reviewId)}?source=${encodeURIComponent(source)}`;
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
const error = await response.text();
return NextResponse.json(
{ error: `Backend error: ${error}` },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Error fetching review:', error);
return NextResponse.json(
{ error: 'Failed to fetch review' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,42 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:8001';
/**
* Proxy route for ReviewIQ trends endpoint.
* GET /api/pipelines/reviewiq/trends
*/
export async function GET(request: NextRequest) {
try {
// Forward query parameters
const searchParams = request.nextUrl.searchParams;
const queryString = searchParams.toString();
const url = `${API_BASE_URL}/api/pipelines/reviewiq/trends${queryString ? `?${queryString}` : ''}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
cache: 'no-store',
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
return NextResponse.json(
{ detail: errorData.detail || `Backend error: ${response.status}` },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('ReviewIQ trends proxy error:', error);
return NextResponse.json(
{ detail: 'Failed to fetch trends data' },
{ status: 500 }
);
}
}

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function GET(request: NextRequest) {
try {

View File

@@ -1,16 +1,38 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { url, business_name, business_address, rating_snapshot, total_reviews_snapshot, scraper_version } = body;
const { url, business_name, business_address, rating_snapshot, total_reviews_snapshot, scraper_version, session_id, browser_fingerprint, geolocation } = body;
if (!url) {
return NextResponse.json({ error: 'URL is required' }, { status: 400 });
}
// Build metadata object
const metadata: Record<string, unknown> = {
business_name,
business_address,
rating_snapshot,
total_reviews_snapshot,
scraper_version, // Store in metadata for job tracking
};
// Include session_id for browser reuse (session handoff from validation)
if (session_id) {
metadata.session_id = session_id;
}
// Include browser fingerprint if provided
if (browser_fingerprint) {
metadata.browser_fingerprint = browser_fingerprint;
}
if (geolocation) {
metadata.geolocation = geolocation;
}
// Call the containerized scraper API with business metadata and version
const response = await fetch(`${API_BASE_URL}/scrape`, {
method: 'POST',
@@ -18,13 +40,8 @@ export async function POST(request: NextRequest) {
body: JSON.stringify({
url,
scraper_version, // Pass version to backend for routing
metadata: {
business_name,
business_address,
rating_snapshot,
total_reviews_snapshot,
scraper_version, // Also store in metadata for job tracking
},
session_id, // Pass session_id for browser reuse
metadata,
}),
});

View File

@@ -0,0 +1,37 @@
import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';
export async function POST(request: NextRequest) {
try {
const body = await request.json();
if (!body.url) {
return NextResponse.json({ error: 'URL is required' }, { status: 400 });
}
// Call the backend session validation endpoint
const response = await fetch(`${API_BASE_URL}/sessions/validate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
const data = await response.json();
if (!response.ok) {
return NextResponse.json(
{ error: data.detail || 'Failed to validate session' },
{ status: response.status }
);
}
return NextResponse.json(data);
} catch (error) {
console.error('Session validation API error:', error);
return NextResponse.json(
{ error: 'Failed to connect to scraper API' },
{ status: 500 }
);
}
}