From a9420500af6d7ab5de1d0492f1816163b47d5b66 Mon Sep 17 00:00:00 2001 From: ale Date: Sun, 8 Jun 2025 03:55:22 +0200 Subject: [PATCH] repair search images Signed-off-by: ale --- index.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index b6a23a5..fcab9e3 100644 --- a/index.js +++ b/index.js @@ -196,7 +196,13 @@ function isAudioFile(url) { async function validateMultimediaUrl(url) { try { - const response = await fetch(url, { method: 'HEAD', timeout: 5000 }); + const response = await fetch(url, { + method: 'HEAD', + timeout: 5000, + headers: { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' + } + }); const contentType = response.headers.get('content-type') || ''; const isMultimedia = contentType.startsWith('image/') || contentType.startsWith('video/') || @@ -212,6 +218,43 @@ async function validateMultimediaUrl(url) { } } +// More lenient validation for search results +async function validateImageUrl(url) { + try { + // First check if the URL looks like an image file + const urlLower = url.toLowerCase(); + const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp']; + const hasImageExtension = imageExtensions.some(ext => urlLower.includes(ext)); + + // If it has an image extension or is from known image hosting sites, consider it valid + const knownImageHosts = ['imgur.com', 'i.imgur.com', 'cdn.', 'images.', 'img.', 'static.', 'upload.wikimedia.org']; + const isFromImageHost = knownImageHosts.some(host => urlLower.includes(host)); + + if (hasImageExtension || isFromImageHost) { + return { valid: true, contentType: 'image/unknown' }; + } + + // For other URLs, try a quick HEAD request but don't be too strict + const response = await fetch(url, { + method: 'HEAD', + timeout: 3000, + headers: { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' + } + }); + + const contentType = response.headers.get('content-type') || ''; + + return { + valid: response.ok && (contentType.startsWith('image/') || contentType === ''), + contentType: contentType || 'image/unknown' + }; + } catch (error) { + // If validation fails, assume it might be valid anyway for search results + return { valid: true, contentType: 'image/unknown', error: error.message }; + } +} + // Bitcoin transaction monitoring functions async function initBitcoinMonitoring() { try { @@ -421,14 +464,14 @@ client.on(Events.InteractionCreate, async interaction => { if (mediaType === 'image') { // Enhanced image search with better filtering const searchTerm = `${query} filetype:jpg OR filetype:png OR filetype:gif OR filetype:webp`; - const results = await googleIt({ query: searchTerm, limit: count * 5 }); + const results = await googleIt({ query: searchTerm, limit: count * 3 }); const imageResults = []; for (const result of results) { const url = result.link; - // Validate if the URL is actually an image - const validation = await validateMultimediaUrl(url); - if (validation.valid && validation.contentType.startsWith('image/')) { + // Use more lenient validation for image search results + const validation = await validateImageUrl(url); + if (validation.valid) { imageResults.push({ title: result.title, url: url, @@ -439,7 +482,30 @@ client.on(Events.InteractionCreate, async interaction => { } if (imageResults.length === 0) { - await interaction.editReply(`No valid image results found for "${query}"`); + // If no results found, try a simpler search without file type restrictions + const simpleResults = await googleIt({ query: query, limit: count * 2 }); + + for (const result of simpleResults) { + const url = result.link; + const urlLower = url.toLowerCase(); + + // Check if URL contains image indicators + const imageIndicators = ['.jpg', '.jpeg', '.png', '.gif', '.webp', 'image', 'img', 'photo']; + const hasImageIndicator = imageIndicators.some(indicator => urlLower.includes(indicator)); + + if (hasImageIndicator) { + imageResults.push({ + title: result.title, + url: url, + contentType: 'image/unknown' + }); + if (imageResults.length >= count) break; + } + } + } + + if (imageResults.length === 0) { + await interaction.editReply(`No valid image results found for "${query}". Try a more specific search term.`); return; }