From 7a816efc04fd30e8b0999e7ae2dc118edb18e71d Mon Sep 17 00:00:00 2001 From: HadiCherkaoui Date: Fri, 28 Feb 2025 09:13:11 +0100 Subject: [PATCH] fix google pse i tested everything, also tested if invalid api key and such and regression tested searxng which still works --- src/chains/imageSearchAgent.ts | 22 +++++++--------------- src/chains/videoSearchAgent.ts | 24 ++++++------------------ src/lib/searchEngines/google_pse.ts | 15 +++++++++++++-- src/routes/discover.ts | 7 +++---- 4 files changed, 29 insertions(+), 39 deletions(-) diff --git a/src/chains/imageSearchAgent.ts b/src/chains/imageSearchAgent.ts index 4c73876..920fdf6 100644 --- a/src/chains/imageSearchAgent.ts +++ b/src/chains/imageSearchAgent.ts @@ -45,22 +45,14 @@ async function performImageSearch(query: string) { switch (searchEngine) { case 'google': { const googleResult = await searchGooglePSE(query); - images = googleResult.originalres - .map((result) => { - // Extract image URL from multiple possible locations in Google's response - const imageSrc = result.pagemap?.cse_image?.[0]?.src || - result.pagemap?.cse_thumbnail?.[0]?.src || - result.image?.thumbnailLink; - - if (imageSrc && result.link && result.title) { - images.push({ - img_src: imageSrc, - url: result.link, + images = googleResult.results.map((result) => { + if (result.img_src && result.url && result.title) { + return { + img_src: result.img_src, + url: result.url, title: result.title, - // Add additional metadata if needed - source: result.displayLink, - fileFormat: result.fileFormat, - }); + source: result.displayLink + }; } }) .filter(Boolean); diff --git a/src/chains/videoSearchAgent.ts b/src/chains/videoSearchAgent.ts index df352fd..b16fb44 100644 --- a/src/chains/videoSearchAgent.ts +++ b/src/chains/videoSearchAgent.ts @@ -40,12 +40,6 @@ type VideoSearchChainInput = { const strParser = new StringOutputParser(); -function extractYouTubeVideoId(url: string): string | null { - const regex = /(?:v=|\/embed\/|\.be\/)([a-zA-Z0-9_-]{11})/; - const match = url.match(regex); - return match ? match[1] : null; -} - async function performVideoSearch(query: string) { const searchEngine = getSearchEngineBackend(); const youtubeQuery = `${query} site:youtube.com`; @@ -54,20 +48,14 @@ async function performVideoSearch(query: string) { switch (searchEngine) { case 'google': { const googleResult = await searchGooglePSE(youtubeQuery); - googleResult.originalres.results.forEach((result) => { - // Extract video metadata from Google PSE results - const thumbnail = result.pagemap?.cse_thumbnail?.[0]?.src - || result.pagemap?.videoobject?.[0]?.thumbnailurl; - - if (thumbnail && result.link && result.title) { + googleResult.results.forEach((result) => { // Use .results instead of .originalres + if (result.img_src && result.url && result.title) { + const videoId = new URL(result.url).searchParams.get('v'); videos.push({ - img_src: thumbnail, - url: result.link, + img_src: result.img_src, + url: result.url, title: result.title, - // Construct iframe URL from YouTube video ID - iframe_src: result.link.includes('youtube.com/watch?v=') - ? `https://www.youtube.com/embed/${result.link.split('v=')[1].split('&')[0]}` - : null, + iframe_src: videoId ? `https://www.youtube.com/embed/${videoId}` : null }); } }); diff --git a/src/lib/searchEngines/google_pse.ts b/src/lib/searchEngines/google_pse.ts index 5232b0c..f09531d 100644 --- a/src/lib/searchEngines/google_pse.ts +++ b/src/lib/searchEngines/google_pse.ts @@ -63,11 +63,22 @@ export const searchGooglePSE = async (query: string) => { title: item.title, url: item.link, content: item.snippet, - img_src: item.pagemap?.cse_image?.[0]?.src, + img_src: item.pagemap?.cse_image?.[0]?.src + || item.pagemap?.cse_thumbnail?.[0]?.src + || item.image?.thumbnailLink, + ...(item.pagemap?.videoobject?.[0] && { + videoData: { + duration: item.pagemap.videoobject[0].duration, + embedUrl: item.pagemap.videoobject[0].embedurl + } + }) })); return { results, originalres }; } catch (error) { - throw new Error('Google PSE Error:', error.response?.data || error.message); + const errorMessage = error.response?.data + ? JSON.stringify(error.response.data, null, 2) + : error.message || 'Unknown error'; + throw new Error(`Google PSE Error: ${errorMessage}`); } }; diff --git a/src/routes/discover.ts b/src/routes/discover.ts index 80022fc..0f47b97 100644 --- a/src/routes/discover.ts +++ b/src/routes/discover.ts @@ -6,9 +6,8 @@ import logger from '../utils/logger'; const router = express.Router(); -const searchEngine = getSearchEngineBackend(); - -async function performSearch(query: string, site: string, searchEngine: string) { +async function performSearch(query: string, site: string) { + const searchEngine = getSearchEngineBackend(); switch (searchEngine) { case 'google': { const googleResult = await searchGooglePSE(query); @@ -65,7 +64,7 @@ router.get('/', async (req, res) => { queries.map(async ({ site, topic }) => { try { const query = `site:${site} ${topic}`; - return await performSearch(query, site, searchEngine); + return await performSearch(query, site); } catch (error) { logger.error(`Error searching ${site}: ${error.message}`); return [];