mirror of
https://github.com/direct-img/direct-img.link.git
synced 2026-10-19 02:44:52 +00:00
Feat: Add free.direct-img.link and ?i= result picker (#1)
- free.direct-img.link serves only unrestricted (public domain/CC0) images from Openverse (license=cc0,pdm) and Wikimedia Commons (haslicense:unrestricted), with optional ?src=openverse|wikimedia - ?i=1..20 picks the i-th working image on both sites; broken links are skipped so every index is a different image - Openverse OAuth token is cached in KV and refreshed automatically - Skip Flickr "photo unavailable" placeholders (served with HTTP 200) - Free/indexed cache keys use uppercase prefixes; plain main-site keys are unchanged so existing cache entries stay valid - Update README, homepage, privacy policy and terms Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import { openverseImageSearch } from "./openverse.js";
|
||||
import { wikimediaImageSearch } from "./wikimedia.js";
|
||||
|
||||
// Wikimedia's User-Agent policy requires contact info; also used when downloading free images
|
||||
export const FREE_UA = "direct-img.link/1.0 (https://direct-img.link; contact@direct-img.link)";
|
||||
|
||||
// Tried in this order when no ?src= is given
|
||||
const SOURCES = { openverse: openverseImageSearch, wikimedia: wikimediaImageSearch };
|
||||
|
||||
// ?src=openverse|wikimedia, or "auto" (every source in order) when omitted. Returns null if invalid.
|
||||
export function parseFreeSource(searchParams) {
|
||||
const src = searchParams.get("src")?.toLowerCase() || "auto";
|
||||
return src === "auto" || Object.hasOwn(SOURCES, src) ? src : null;
|
||||
}
|
||||
|
||||
// Yields each source's image URLs in turn; a source is only searched once the previous one runs out
|
||||
export async function* freeImageUrls(query, src, env) {
|
||||
for (const name of src === "auto" ? Object.keys(SOURCES) : [src]) {
|
||||
yield* (await SOURCES[name](query, env, FREE_UA)) || [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user