feat: switch Whiskybase discovery to Google Custom Search JSON API
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* 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: {
|
||||
name: string;
|
||||
@@ -10,20 +10,19 @@ export async function discoverWhiskybaseId(bottle: {
|
||||
abv?: 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) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'SERPAPI_API_KEY ist nicht konfiguriert.'
|
||||
error: 'GOOGLE_API_KEY ist nicht konfiguriert.'
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
// Construct targeted search query
|
||||
// site:whiskybase.com/whiskies/whisky ensures we only get product pages
|
||||
const queryParts = [
|
||||
'site:whiskybase.com/whiskies/whisky',
|
||||
`"${bottle.distillery || ''}"`,
|
||||
`"${bottle.name}"`,
|
||||
bottle.abv ? `${bottle.abv}%` : '',
|
||||
@@ -31,12 +30,16 @@ export async function discoverWhiskybaseId(bottle: {
|
||||
].filter(Boolean);
|
||||
|
||||
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 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.' };
|
||||
}
|
||||
|
||||
@@ -44,8 +47,8 @@ export async function discoverWhiskybaseId(bottle: {
|
||||
// Pattern matches: https://www.whiskybase.com/whiskies/whisky/12345/name
|
||||
const wbRegex = /\/whisky\/(\d+)\//;
|
||||
|
||||
for (const result of data.organic_results) {
|
||||
const link = result.link;
|
||||
for (const item of data.items) {
|
||||
const link = item.link;
|
||||
const match = link.match(wbRegex);
|
||||
|
||||
if (match && match[1]) {
|
||||
@@ -53,7 +56,7 @@ export async function discoverWhiskybaseId(bottle: {
|
||||
success: true,
|
||||
id: match[1],
|
||||
url: link,
|
||||
title: result.title
|
||||
title: item.title
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -63,7 +66,7 @@ export async function discoverWhiskybaseId(bottle: {
|
||||
console.error('Whiskybase Discovery Error:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: 'Fehler bei der Suche auf Whiskybase.'
|
||||
error: error instanceof Error ? error.message : 'Fehler bei der Suche auf Whiskybase.'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user