mirror of
https://github.com/direct-img/direct-img.link.git
synced 2026-10-14 08:47:53 +00:00
Brave errors (e.g. its spending limit being hit) now fall back to Bing like empty results do, with the error in a Brave Unavailable alert. If Bing fails too (HTTP error or a page without results, which for Bing means it's blocking us), a new papercraft donate.webp is served instead of an AI image, and nothing is cached so the query works once either search is back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
14 lines
707 B
JavaScript
14 lines
707 B
JavaScript
// { urls } ([] if none found), or { error } if Brave refused or failed, e.g. once our rate or spending limit is hit
|
|
export async function braveImageSearch(query, apiKey) {
|
|
try {
|
|
const res = await fetch(`https://api.search.brave.com/res/v1/images/search?q=${encodeURIComponent(query)}&count=50&safesearch=off`, {
|
|
headers: { "Accept": "application/json", "X-Subscription-Token": apiKey },
|
|
});
|
|
if (!res.ok) return { error: `HTTP ${res.status}: ${(await res.text()).slice(0, 300)}` };
|
|
const data = await res.json();
|
|
return { urls: data.results?.map(r => r.properties?.url || r.thumbnail?.src).filter(url => !!url) || [] };
|
|
} catch (err) {
|
|
return { error: err.message };
|
|
}
|
|
}
|