feat: switch Whiskybase discovery to Google Custom Search JSON API

This commit is contained in:
2025-12-18 12:43:48 +01:00
parent fef1c4a275
commit ddf352dab6

View File

@@ -2,7 +2,7 @@
/** /**
* Service to discover a Whiskybase ID for a given bottle. * Service to discover a Whiskybase ID for a given bottle.
* Uses SerpApi to search Google and extracts the ID from the first result. * Uses Google Custom Search JSON API to search Google and extracts the ID from the first result.
*/ */
export async function discoverWhiskybaseId(bottle: { export async function discoverWhiskybaseId(bottle: {
name: string; name: string;
@@ -10,20 +10,19 @@ export async function discoverWhiskybaseId(bottle: {
abv?: number; abv?: number;
age?: number; age?: number;
}) { }) {
const apiKey = process.env.SERPAPI_API_KEY; const apiKey = process.env.GOOGLE_API_KEY;
const cx = '37e905eb03fd14e0f'; // Provided by user
if (!apiKey) { if (!apiKey) {
return { return {
success: false, success: false,
error: 'SERPAPI_API_KEY ist nicht konfiguriert.' error: 'GOOGLE_API_KEY ist nicht konfiguriert.'
}; };
} }
try { try {
// Construct targeted search query // Construct targeted search query
// site:whiskybase.com/whiskies/whisky ensures we only get product pages
const queryParts = [ const queryParts = [
'site:whiskybase.com/whiskies/whisky',
`"${bottle.distillery || ''}"`, `"${bottle.distillery || ''}"`,
`"${bottle.name}"`, `"${bottle.name}"`,
bottle.abv ? `${bottle.abv}%` : '', bottle.abv ? `${bottle.abv}%` : '',
@@ -31,12 +30,16 @@ export async function discoverWhiskybaseId(bottle: {
].filter(Boolean); ].filter(Boolean);
const q = queryParts.join(' '); const q = queryParts.join(' ');
const url = `https://serpapi.com/search.json?q=${encodeURIComponent(q)}&api_key=${apiKey}&engine=google&num=5`; const url = `https://www.googleapis.com/customsearch/v1?key=${apiKey}&cx=${cx}&q=${encodeURIComponent(q)}`;
const response = await fetch(url); const response = await fetch(url);
const data = await response.json(); const data = await response.json();
if (!data.organic_results || data.organic_results.length === 0) { if (data.error) {
throw new Error(data.error.message || 'Google API Error');
}
if (!data.items || data.items.length === 0) {
return { success: false, error: 'Keine Treffer auf Whiskybase gefunden.' }; return { success: false, error: 'Keine Treffer auf Whiskybase gefunden.' };
} }
@@ -44,8 +47,8 @@ export async function discoverWhiskybaseId(bottle: {
// Pattern matches: https://www.whiskybase.com/whiskies/whisky/12345/name // Pattern matches: https://www.whiskybase.com/whiskies/whisky/12345/name
const wbRegex = /\/whisky\/(\d+)\//; const wbRegex = /\/whisky\/(\d+)\//;
for (const result of data.organic_results) { for (const item of data.items) {
const link = result.link; const link = item.link;
const match = link.match(wbRegex); const match = link.match(wbRegex);
if (match && match[1]) { if (match && match[1]) {
@@ -53,7 +56,7 @@ export async function discoverWhiskybaseId(bottle: {
success: true, success: true,
id: match[1], id: match[1],
url: link, url: link,
title: result.title title: item.title
}; };
} }
} }
@@ -63,7 +66,7 @@ export async function discoverWhiskybaseId(bottle: {
console.error('Whiskybase Discovery Error:', error); console.error('Whiskybase Discovery Error:', error);
return { return {
success: false, success: false,
error: 'Fehler bei der Suche auf Whiskybase.' error: error instanceof Error ? error.message : 'Fehler bei der Suche auf Whiskybase.'
}; };
} }
} }