fix google pse

i tested everything, also tested if invalid api key and such and regression tested searxng which still works
This commit is contained in:
HadiCherkaoui
2025-02-28 09:13:11 +01:00
parent 4d41243108
commit 7a816efc04
4 changed files with 29 additions and 39 deletions

View File

@ -45,22 +45,14 @@ async function performImageSearch(query: string) {
switch (searchEngine) { switch (searchEngine) {
case 'google': { case 'google': {
const googleResult = await searchGooglePSE(query); const googleResult = await searchGooglePSE(query);
images = googleResult.originalres images = googleResult.results.map((result) => {
.map((result) => { if (result.img_src && result.url && result.title) {
// Extract image URL from multiple possible locations in Google's response return {
const imageSrc = result.pagemap?.cse_image?.[0]?.src || img_src: result.img_src,
result.pagemap?.cse_thumbnail?.[0]?.src || url: result.url,
result.image?.thumbnailLink;
if (imageSrc && result.link && result.title) {
images.push({
img_src: imageSrc,
url: result.link,
title: result.title, title: result.title,
// Add additional metadata if needed source: result.displayLink
source: result.displayLink, };
fileFormat: result.fileFormat,
});
} }
}) })
.filter(Boolean); .filter(Boolean);

View File

@ -40,12 +40,6 @@ type VideoSearchChainInput = {
const strParser = new StringOutputParser(); 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) { async function performVideoSearch(query: string) {
const searchEngine = getSearchEngineBackend(); const searchEngine = getSearchEngineBackend();
const youtubeQuery = `${query} site:youtube.com`; const youtubeQuery = `${query} site:youtube.com`;
@ -54,20 +48,14 @@ async function performVideoSearch(query: string) {
switch (searchEngine) { switch (searchEngine) {
case 'google': { case 'google': {
const googleResult = await searchGooglePSE(youtubeQuery); const googleResult = await searchGooglePSE(youtubeQuery);
googleResult.originalres.results.forEach((result) => { googleResult.results.forEach((result) => { // Use .results instead of .originalres
// Extract video metadata from Google PSE results if (result.img_src && result.url && result.title) {
const thumbnail = result.pagemap?.cse_thumbnail?.[0]?.src const videoId = new URL(result.url).searchParams.get('v');
|| result.pagemap?.videoobject?.[0]?.thumbnailurl;
if (thumbnail && result.link && result.title) {
videos.push({ videos.push({
img_src: thumbnail, img_src: result.img_src,
url: result.link, url: result.url,
title: result.title, title: result.title,
// Construct iframe URL from YouTube video ID iframe_src: videoId ? `https://www.youtube.com/embed/${videoId}` : null
iframe_src: result.link.includes('youtube.com/watch?v=')
? `https://www.youtube.com/embed/${result.link.split('v=')[1].split('&')[0]}`
: null,
}); });
} }
}); });

View File

@ -63,11 +63,22 @@ export const searchGooglePSE = async (query: string) => {
title: item.title, title: item.title,
url: item.link, url: item.link,
content: item.snippet, 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 }; return { results, originalres };
} catch (error) { } 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}`);
} }
}; };

View File

@ -6,9 +6,8 @@ import logger from '../utils/logger';
const router = express.Router(); const router = express.Router();
async function performSearch(query: string, site: string) {
const searchEngine = getSearchEngineBackend(); const searchEngine = getSearchEngineBackend();
async function performSearch(query: string, site: string, searchEngine: string) {
switch (searchEngine) { switch (searchEngine) {
case 'google': { case 'google': {
const googleResult = await searchGooglePSE(query); const googleResult = await searchGooglePSE(query);
@ -65,7 +64,7 @@ router.get('/', async (req, res) => {
queries.map(async ({ site, topic }) => { queries.map(async ({ site, topic }) => {
try { try {
const query = `site:${site} ${topic}`; const query = `site:${site} ${topic}`;
return await performSearch(query, site, searchEngine); return await performSearch(query, site);
} catch (error) { } catch (error) {
logger.error(`Error searching ${site}: ${error.message}`); logger.error(`Error searching ${site}: ${error.message}`);
return []; return [];