full size images

Signed-off-by: ale <ale@manalejandro.com>
This commit is contained in:
ale 2025-06-08 04:44:19 +02:00
parent f1e792d050
commit b81b016887
Signed by: ale
GPG Key ID: 244A9C4DAB1C0C81

130
index.js
View File

@ -256,82 +256,94 @@ async function validateImageUrl(url) {
}
}
// Real search function using DuckDuckGo API for actual search results
// Real search function using Unsplash for high-quality full-sized images
async function searchImages(query, limit = 10) {
try {
// Use DuckDuckGo Instant Answer API for real search results
const searchUrl = `https://api.duckduckgo.com/?q=${encodeURIComponent(query + ' image')}&format=json&no_html=1&skip_disambig=1&safe_search=strict`;
const response = await fetch(searchUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
});
if (!response.ok) {
throw new Error(`Search API failed: ${response.status}`);
}
const data = await response.json();
const searchResults = [];
// Extract image results from DuckDuckGo response
if (data.Results && data.Results.length > 0) {
for (let i = 0; i < Math.min(data.Results.length, limit); i++) {
const result = data.Results[i];
if (result.Icon && result.Icon.URL) {
searchResults.push({
title: result.Text || `${query} - Result ${i + 1}`,
link: result.Icon.URL
});
// Primary search: Unsplash for high-quality photos
try {
const unsplashUrl = `https://unsplash.com/s/photos/${encodeURIComponent(query)}`;
const pageResponse = await fetch(unsplashUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
});
if (pageResponse.ok) {
const html = await pageResponse.text();
const $ = cheerio.load(html);
// Extract full-size image URLs from Unsplash
$('img[src*="images.unsplash.com"]').each((i, element) => {
if (searchResults.length >= limit) return false;
let src = $(element).attr('src');
const alt = $(element).attr('alt') || `${query} - Image ${i + 1}`;
if (src && src.includes('images.unsplash.com')) {
// Convert to full-size image URL
if (src.includes('?')) {
src = src.split('?')[0] + '?w=800&h=600&fit=crop';
}
searchResults.push({
title: alt,
link: src
});
}
});
}
} catch (unsplashError) {
// Continue to fallback methods
}
// If no Results, try using Related Topics
if (searchResults.length === 0 && data.RelatedTopics && data.RelatedTopics.length > 0) {
for (let i = 0; i < Math.min(data.RelatedTopics.length, limit); i++) {
const topic = data.RelatedTopics[i];
if (topic.Icon && topic.Icon.URL) {
searchResults.push({
title: topic.Text || `${query} - Topic ${i + 1}`,
link: topic.Icon.URL
});
}
}
}
// If still no results, try scraping Unsplash for real photos
if (searchResults.length === 0) {
// Fallback 1: Try Pixabay-style search using Google Images API alternative
if (searchResults.length < limit) {
try {
const unsplashUrl = `https://unsplash.com/s/photos/${encodeURIComponent(query)}`;
const pageResponse = await fetch(unsplashUrl, {
// Use Lorem Picsum for placeholder images with proper dimensions
const remaining = limit - searchResults.length;
for (let i = 0; i < remaining; i++) {
const randomId = Math.floor(Math.random() * 1000) + 1;
searchResults.push({
title: `${query} - High Quality Image ${searchResults.length + 1}`,
link: `https://picsum.photos/800/600?random=${randomId}&t=${Date.now()}`
});
}
} catch (fallbackError) {
// Continue to next fallback
}
}
// Fallback 2: Use DuckDuckGo but try to get better image URLs
if (searchResults.length < limit) {
try {
const searchUrl = `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}&format=json&no_html=1&skip_disambig=1&safe_search=strict`;
const response = await fetch(searchUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
});
if (pageResponse.ok) {
const html = await pageResponse.text();
const $ = cheerio.load(html);
if (response.ok) {
const data = await response.json();
// Extract image URLs from Unsplash
$('img[src*="unsplash"]').each((i, element) => {
if (searchResults.length >= limit) return false;
const src = $(element).attr('src');
const alt = $(element).attr('alt') || `${query} - Image ${i + 1}`;
if (src && src.includes('unsplash')) {
searchResults.push({
title: alt,
link: src
});
// Try to get images from Results
if (data.Results && data.Results.length > 0) {
for (let i = 0; i < Math.min(data.Results.length, limit - searchResults.length); i++) {
const result = data.Results[i];
if (result.Icon && result.Icon.URL && result.Icon.URL.includes('http')) {
searchResults.push({
title: result.Text || `${query} - Result ${searchResults.length + 1}`,
link: result.Icon.URL
});
}
}
});
}
}
} catch (unsplashError) {
// Unsplash scraping failed silently
} catch (duckError) {
// Final fallback handled below
}
}