diff --git a/functions/[[path]].js b/functions/[[path]].js index 1aec285..2358a2e 100644 --- a/functions/[[path]].js +++ b/functions/[[path]].js @@ -44,7 +44,6 @@ export async function onRequest(context) { "surreal-db": "rate_limit", }; - // First ensure namespace and database exist try { const initSql = `DEFINE NAMESPACE IF NOT EXISTS direct_img; USE NS direct_img; DEFINE DATABASE IF NOT EXISTS rate_limit;`; await fetch(`${env.SURREAL_URL}/sql`, { @@ -141,8 +140,14 @@ export async function onRequest(context) { return env.ASSETS.fetch(new Request(new URL("/bad.webp", url.origin))); }; - const imageUrls = await braveImageSearch(query, env.BRAVE_API_KEY); - if (!imageUrls || imageUrls.length === 0) return await fail("Search Failed", `Brave API returned no results for: ${query}`, "question", 3); + let imageUrls = await braveImageSearch(query, env.BRAVE_API_KEY); + + if (!imageUrls || imageUrls.length === 0) { + context.waitUntil(notify(env, { title: "Brave Search Empty", message: `No results for: ${query}. Trying Bing Fallback.`, tags: "warning,mag", priority: 3 })); + imageUrls = await bingImageSearchFallback(query); + } + + if (!imageUrls || imageUrls.length === 0) return await fail("Search Failed", `Both Brave and Bing returned no results for: ${query}`, "question", 3); const GLOBAL_DEADLINE = Date.now() + 20000; let imgResult = null; @@ -212,6 +217,36 @@ async function braveImageSearch(query, apiKey) { return data.results?.map(r => r.properties?.url || r.thumbnail?.src).filter(url => !!url) || null; } +async function bingImageSearchFallback(query) { + try { + const res = await fetch(`https://www.bing.com/images/search?q=${encodeURIComponent(query)}&safesearch=moderate`, { + headers: { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" + } + }); + if (!res.ok) return null; + const html = await res.text(); + const urls = []; + + // Bing encodes the image payload inside HTML entity attributes and script tags + const regex1 = /"murl":"(https:[^&"]+)"/g; + const regex2 = /"murl":"(https:[^"]+)"/g; + + let match; + while ((match = regex1.exec(html)) !== null) { + urls.push(match[1].replace(/\\\//g, '/')); + } + while ((match = regex2.exec(html)) !== null) { + urls.push(match[1].replace(/\\\//g, '/')); + } + + return urls.length > 0 ? [...new Set(urls)] : null; + } catch (err) { + return null; + } +} + async function fetchImage(imageUrl, timeoutMs = 5000) { try { const res = await fetch(imageUrl, {