mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-04-30 16:22:29 +00:00
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:
@ -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);
|
||||
|
@ -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
|
||||
});
|
||||
}
|
||||
});
|
||||
|
@ -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}`);
|
||||
}
|
||||
};
|
||||
|
@ -6,9 +6,8 @@ import logger from '../utils/logger';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
async function performSearch(query: string, site: string) {
|
||||
const searchEngine = getSearchEngineBackend();
|
||||
|
||||
async function performSearch(query: string, site: string, searchEngine: string) {
|
||||
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 [];
|
||||
|
Reference in New Issue
Block a user