cc0-library-mcp 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +67 -23
- package/catalog/sources.json +1388 -30
- package/dist/http.js +5 -4
- package/dist/providers/blenderkit.js +60 -0
- package/dist/providers/collections.js +107 -0
- package/dist/providers/gbif.js +67 -0
- package/dist/providers/iconify.js +81 -0
- package/dist/providers/inaturalist.js +69 -0
- package/dist/providers/phylopic.js +88 -0
- package/dist/providers/wpphotos.js +70 -0
- package/dist/providers/zenodo.js +62 -0
- package/dist/search.js +49 -1
- package/dist/server.js +141 -32
- package/dist/types.js +10 -3
- package/package.json +1 -1
package/dist/http.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Poly Haven's API terms require a User-Agent that names the calling app.
|
|
3
3
|
* Openverse also asks clients to identify themselves.
|
|
4
4
|
*/
|
|
5
|
-
export const USER_AGENT = "cc0-library-mcp/0.
|
|
5
|
+
export const USER_AGENT = "cc0-library-mcp/0.2.0 (+https://github.com/sanghyoLe/cc0-library)";
|
|
6
6
|
const TIMEOUT_MS = 15_000;
|
|
7
7
|
export class UpstreamError extends Error {
|
|
8
8
|
provider;
|
|
@@ -21,11 +21,12 @@ export const defaultFetch = (url, init) => fetch(url, {
|
|
|
21
21
|
const RETRYABLE = new Set([502, 503, 504]);
|
|
22
22
|
const RETRY_DELAY_MS = 500;
|
|
23
23
|
/** GETs JSON. Retries once on a gateway error; any other non-2xx status throws UpstreamError. */
|
|
24
|
-
export async function getJson(fetchImpl, provider, url) {
|
|
25
|
-
|
|
24
|
+
export async function getJson(fetchImpl, provider, url, headers) {
|
|
25
|
+
const init = headers ? { headers } : undefined;
|
|
26
|
+
let res = await fetchImpl(url, init);
|
|
26
27
|
if (RETRYABLE.has(res.status)) {
|
|
27
28
|
await new Promise((r) => setTimeout(r, RETRY_DELAY_MS));
|
|
28
|
-
res = await fetchImpl(url);
|
|
29
|
+
res = await fetchImpl(url, init);
|
|
29
30
|
}
|
|
30
31
|
if (!res.ok)
|
|
31
32
|
throw new UpstreamError(provider, res.status, url);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { getJson } from "../http.js";
|
|
2
|
+
import { CC0_LICENSE_URL } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* BlenderKit (https://www.blenderkit.com/api/v1, search needs no key).
|
|
5
|
+
* Assets are under "Royalty Free" or CC0; the query asks for `license:cc_zero`
|
|
6
|
+
* and each result must report license "cc_zero". Downloads go through the
|
|
7
|
+
* BlenderKit add-on or website with a free account, so results link the asset
|
|
8
|
+
* page. The API sends no CORS headers: it works from servers, not browsers.
|
|
9
|
+
*/
|
|
10
|
+
const BASE = "https://www.blenderkit.com/api/v1";
|
|
11
|
+
const SERVED_BY = "Via BlenderKit (https://www.blenderkit.com)";
|
|
12
|
+
/** BlenderKit asset_type per kind. */
|
|
13
|
+
const ASSET_TYPE = { model: "model", texture: "material", hdri: "hdr" };
|
|
14
|
+
const KIND_OF = { model: "model", material: "texture", hdr: "hdri", scene: "model" };
|
|
15
|
+
export const isCc0 = (a) => a.license === "cc_zero" && !a.isPrivate;
|
|
16
|
+
export function toAsset(a) {
|
|
17
|
+
return {
|
|
18
|
+
id: `blenderkit:${a.id}`,
|
|
19
|
+
kind: KIND_OF[a.assetType] ?? "model",
|
|
20
|
+
title: a.displayName || a.name,
|
|
21
|
+
creator: a.author?.fullName ?? null,
|
|
22
|
+
landingUrl: `https://www.blenderkit.com/asset-gallery-detail/${a.id}/`,
|
|
23
|
+
fileUrl: null,
|
|
24
|
+
thumbnailUrl: a.thumbnailMiddleUrl ?? null,
|
|
25
|
+
tags: (a.tags ?? []).slice(0, 20),
|
|
26
|
+
provider: "blenderkit",
|
|
27
|
+
origin: "blenderkit.com",
|
|
28
|
+
licenseEvidence: {
|
|
29
|
+
license: "CC0-1.0",
|
|
30
|
+
licenseUrl: CC0_LICENSE_URL,
|
|
31
|
+
method: "per-item-license-field",
|
|
32
|
+
source: `BlenderKit asset license "cc_zero" (${BASE}/assets/${a.id}/)`,
|
|
33
|
+
declaredBy: "uploader",
|
|
34
|
+
},
|
|
35
|
+
servedBy: SERVED_BY,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const plainWords = (q) => q.replace(/[^\p{L}\p{N}\s]/gu, " ").replace(/\s+/g, " ").trim();
|
|
39
|
+
export async function searchBlenderkit(fetchImpl, kind, params) {
|
|
40
|
+
const words = plainWords(params.query);
|
|
41
|
+
if (!words)
|
|
42
|
+
return { total: 0, assets: [] };
|
|
43
|
+
const filters = kind === "all" ? "" : ` asset_type:${ASSET_TYPE[kind]}`;
|
|
44
|
+
const qs = new URLSearchParams({ query: `${words} license:cc_zero${filters}`, page_size: String(params.limit), page: String(params.page) });
|
|
45
|
+
const data = await getJson(fetchImpl, "BlenderKit", `${BASE}/search/?${qs}`);
|
|
46
|
+
return { total: data.count, assets: data.results.filter(isCc0).map(toAsset) };
|
|
47
|
+
}
|
|
48
|
+
export async function getBlenderkit(fetchImpl, id) {
|
|
49
|
+
if (!/^[0-9a-f-]{36}$/.test(id))
|
|
50
|
+
return null;
|
|
51
|
+
const a = await getJson(fetchImpl, "BlenderKit", `${BASE}/assets/${id}/`);
|
|
52
|
+
if (a.id !== id || !isCc0(a))
|
|
53
|
+
return null;
|
|
54
|
+
const asset = toAsset(a);
|
|
55
|
+
return {
|
|
56
|
+
...asset,
|
|
57
|
+
description: a.description ?? null,
|
|
58
|
+
files: [{ label: "download with a free BlenderKit account or the Blender add-on", url: asset.landingUrl, sizeBytes: null }],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { getJson } from "../http.js";
|
|
2
|
+
import { CC0_LICENSE_URL } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Asset collections: whole repositories and datasets released under CC0.
|
|
5
|
+
*
|
|
6
|
+
* - GitHub (https://api.github.com, no key; 10 searches/minute unauthenticated):
|
|
7
|
+
* repositories whose detected license is CC0-1.0. A repository's README can
|
|
8
|
+
* still license some contents differently, so results carry declaredBy
|
|
9
|
+
* "uploader" and the description says to check the README.
|
|
10
|
+
* - Hugging Face datasets (https://huggingface.co/api, no key): datasets tagged
|
|
11
|
+
* license:cc0-1.0 by their uploaders.
|
|
12
|
+
*/
|
|
13
|
+
const GH = "https://api.github.com";
|
|
14
|
+
const HF = "https://huggingface.co/api/datasets";
|
|
15
|
+
/** owner/name; each part starts with a letter or digit, so ".." path segments are rejected. */
|
|
16
|
+
const OWNER_NAME = /^[A-Za-z0-9][A-Za-z0-9_.-]*\/[A-Za-z0-9][A-Za-z0-9_.-]*$/;
|
|
17
|
+
const repoIsCc0 = (r) => r.license?.spdx_id === "CC0-1.0";
|
|
18
|
+
const datasetIsCc0 = (d) => (d.tags ?? []).includes("license:cc0-1.0");
|
|
19
|
+
export function repoToAsset(r) {
|
|
20
|
+
return {
|
|
21
|
+
id: `github:${r.full_name}`,
|
|
22
|
+
kind: "collection",
|
|
23
|
+
title: r.full_name,
|
|
24
|
+
creator: r.owner?.login ?? null,
|
|
25
|
+
landingUrl: r.html_url,
|
|
26
|
+
fileUrl: `${r.html_url}/archive/refs/heads/${r.default_branch ?? "main"}.zip`,
|
|
27
|
+
thumbnailUrl: `https://opengraph.githubassets.com/1/${r.full_name}`,
|
|
28
|
+
tags: (r.topics ?? []).slice(0, 20),
|
|
29
|
+
provider: "github",
|
|
30
|
+
origin: "github.com",
|
|
31
|
+
licenseEvidence: {
|
|
32
|
+
license: "CC0-1.0",
|
|
33
|
+
licenseUrl: CC0_LICENSE_URL,
|
|
34
|
+
method: "per-item-license-field",
|
|
35
|
+
source: `GitHub detected license CC0-1.0 for ${r.full_name} (${GH}/repos/${r.full_name}); check the README for contents under other licenses`,
|
|
36
|
+
declaredBy: "uploader",
|
|
37
|
+
},
|
|
38
|
+
servedBy: "Via GitHub (https://github.com)",
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export function datasetToAsset(d) {
|
|
42
|
+
const modalities = (d.tags ?? []).filter((t) => t.startsWith("modality:")).map((t) => t.slice(9));
|
|
43
|
+
return {
|
|
44
|
+
id: `huggingface:${d.id}`,
|
|
45
|
+
kind: "collection",
|
|
46
|
+
title: d.cardData?.pretty_name ?? d.id,
|
|
47
|
+
creator: d.author ?? d.id.split("/")[0] ?? null,
|
|
48
|
+
landingUrl: `https://huggingface.co/datasets/${d.id}`,
|
|
49
|
+
fileUrl: null,
|
|
50
|
+
thumbnailUrl: null,
|
|
51
|
+
tags: modalities,
|
|
52
|
+
provider: "huggingface",
|
|
53
|
+
origin: "huggingface.co",
|
|
54
|
+
licenseEvidence: {
|
|
55
|
+
license: "CC0-1.0",
|
|
56
|
+
licenseUrl: CC0_LICENSE_URL,
|
|
57
|
+
method: "per-item-license-field",
|
|
58
|
+
source: `Hugging Face dataset tag license:cc0-1.0 (${HF}/${d.id})`,
|
|
59
|
+
declaredBy: "uploader",
|
|
60
|
+
},
|
|
61
|
+
servedBy: "Via Hugging Face (https://huggingface.co)",
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** Keeps letters, digits and spaces so user text cannot add GitHub search qualifiers. */
|
|
65
|
+
const plainWords = (q) => q.replace(/[^\p{L}\p{N}\s]/gu, " ").replace(/\s+/g, " ").trim();
|
|
66
|
+
export async function searchGithub(fetchImpl, params) {
|
|
67
|
+
const words = plainWords(params.query);
|
|
68
|
+
if (!words)
|
|
69
|
+
return { total: 0, assets: [] };
|
|
70
|
+
const qs = new URLSearchParams({ q: `${words} license:cc0-1.0`, sort: "stars", per_page: String(params.limit), page: String(params.page) });
|
|
71
|
+
const data = await getJson(fetchImpl, "GitHub", `${GH}/search/repositories?${qs}`, {
|
|
72
|
+
Accept: "application/vnd.github+json",
|
|
73
|
+
});
|
|
74
|
+
return { total: data.total_count, assets: data.items.filter(repoIsCc0).map(repoToAsset) };
|
|
75
|
+
}
|
|
76
|
+
export async function getGithub(fetchImpl, fullName) {
|
|
77
|
+
if (!OWNER_NAME.test(fullName))
|
|
78
|
+
return null;
|
|
79
|
+
const r = await getJson(fetchImpl, "GitHub", `${GH}/repos/${fullName}`, { Accept: "application/vnd.github+json" });
|
|
80
|
+
if (!repoIsCc0(r))
|
|
81
|
+
return null;
|
|
82
|
+
const asset = repoToAsset(r);
|
|
83
|
+
return { ...asset, description: r.description, files: [{ label: "repository ZIP", url: asset.fileUrl, sizeBytes: null }] };
|
|
84
|
+
}
|
|
85
|
+
export async function searchHuggingface(fetchImpl, params) {
|
|
86
|
+
const words = plainWords(params.query);
|
|
87
|
+
if (!words)
|
|
88
|
+
return { total: 0, assets: [] };
|
|
89
|
+
// The Hub API pages with cursors; fetch enough for the requested page and slice.
|
|
90
|
+
const want = params.limit * params.page;
|
|
91
|
+
const qs = new URLSearchParams({ search: words, filter: "license:cc0-1.0", sort: "downloads", limit: String(want), full: "true" });
|
|
92
|
+
const data = await getJson(fetchImpl, "Hugging Face", `${HF}?${qs}`);
|
|
93
|
+
const page = data.filter(datasetIsCc0).slice((params.page - 1) * params.limit, want);
|
|
94
|
+
return { total: data.length, assets: page.map(datasetToAsset) };
|
|
95
|
+
}
|
|
96
|
+
export async function getHuggingface(fetchImpl, id) {
|
|
97
|
+
if (!OWNER_NAME.test(id))
|
|
98
|
+
return null;
|
|
99
|
+
const d = await getJson(fetchImpl, "Hugging Face", `${HF}/${id}`);
|
|
100
|
+
if (!datasetIsCc0(d))
|
|
101
|
+
return null;
|
|
102
|
+
return {
|
|
103
|
+
...datasetToAsset(d),
|
|
104
|
+
description: d.description?.replace(/\s+/g, " ").trim().slice(0, 500) || null,
|
|
105
|
+
files: [{ label: "dataset files on Hugging Face", url: `https://huggingface.co/datasets/${id}/tree/main`, sizeBytes: null }],
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { getJson } from "../http.js";
|
|
2
|
+
import { CC0_LICENSE_URL } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* GBIF occurrence images (https://api.gbif.org/v1, no key): specimen and
|
|
5
|
+
* field photos published by museums, herbaria and other institutions.
|
|
6
|
+
* The query asks for `license=CC0_1_0` and still images. An image is used only
|
|
7
|
+
* when the record's license is CC0 and the image's own license (if given) is CC0.
|
|
8
|
+
*/
|
|
9
|
+
const BASE = "https://api.gbif.org/v1/occurrence";
|
|
10
|
+
const SERVED_BY = "Via GBIF (https://www.gbif.org)";
|
|
11
|
+
const CC0_RE = /creativecommons\.org\/publicdomain\/zero\/1\.0/;
|
|
12
|
+
const isCc0 = (u) => !!u && CC0_RE.test(u);
|
|
13
|
+
/** Images on a CC0 record whose own license is CC0 or unstated (then the record license applies). */
|
|
14
|
+
export function cc0Images(o) {
|
|
15
|
+
if (!isCc0(o.license))
|
|
16
|
+
return [];
|
|
17
|
+
return (o.media ?? []).filter((m) => m.type === "StillImage" && m.identifier && (m.license ? isCc0(m.license) : true));
|
|
18
|
+
}
|
|
19
|
+
export function toAsset(o) {
|
|
20
|
+
const img = cc0Images(o)[0];
|
|
21
|
+
if (!img)
|
|
22
|
+
return null;
|
|
23
|
+
return {
|
|
24
|
+
id: `gbif:${o.key}`,
|
|
25
|
+
kind: "image",
|
|
26
|
+
title: [o.vernacularName, o.scientificName].filter(Boolean).join(" — ") || `Occurrence ${o.key}`,
|
|
27
|
+
creator: img.rightsHolder ?? o.recordedBy ?? o.institutionCode ?? null,
|
|
28
|
+
landingUrl: `https://www.gbif.org/occurrence/${o.key}`,
|
|
29
|
+
fileUrl: img.identifier,
|
|
30
|
+
thumbnailUrl: img.identifier,
|
|
31
|
+
tags: [o.scientificName, o.datasetName, o.institutionCode].filter((t) => !!t),
|
|
32
|
+
provider: "gbif",
|
|
33
|
+
origin: o.institutionCode ?? "gbif.org",
|
|
34
|
+
licenseEvidence: {
|
|
35
|
+
license: "CC0-1.0",
|
|
36
|
+
licenseUrl: CC0_LICENSE_URL,
|
|
37
|
+
method: "per-item-license-field",
|
|
38
|
+
source: `GBIF occurrence license and media license are CC0 (${BASE}/${o.key})`,
|
|
39
|
+
declaredBy: "institution",
|
|
40
|
+
},
|
|
41
|
+
servedBy: SERVED_BY,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export async function searchGbif(fetchImpl, params) {
|
|
45
|
+
const qs = new URLSearchParams({
|
|
46
|
+
q: params.query,
|
|
47
|
+
mediaType: "StillImage",
|
|
48
|
+
license: "CC0_1_0",
|
|
49
|
+
limit: String(params.limit),
|
|
50
|
+
offset: String((params.page - 1) * params.limit),
|
|
51
|
+
});
|
|
52
|
+
const data = await getJson(fetchImpl, "GBIF", `${BASE}/search?${qs}`);
|
|
53
|
+
return { total: data.count, assets: data.results.map(toAsset).filter((a) => a !== null) };
|
|
54
|
+
}
|
|
55
|
+
export async function getGbif(fetchImpl, key) {
|
|
56
|
+
if (!/^\d+$/.test(key))
|
|
57
|
+
return null;
|
|
58
|
+
const o = await getJson(fetchImpl, "GBIF", `${BASE}/${key}`);
|
|
59
|
+
const asset = toAsset(o);
|
|
60
|
+
if (!asset)
|
|
61
|
+
return null;
|
|
62
|
+
return {
|
|
63
|
+
...asset,
|
|
64
|
+
description: o.datasetName ?? null,
|
|
65
|
+
files: cc0Images(o).map((m, i) => ({ label: m.title || `image ${i + 1}`, url: m.identifier, sizeBytes: null })),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { getJson } from "../http.js";
|
|
2
|
+
import { CC0_LICENSE_URL } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Iconify icon sets (https://api.iconify.design, no key). Iconify serves 280+
|
|
5
|
+
* open-source icon sets under many licenses. Search is limited to the sets
|
|
6
|
+
* whose own license (from /collections) is CC0-1.0. Logo sets (Simple Icons,
|
|
7
|
+
* cryptocurrency icons...) are CC0 as files, but the logos stay trademarks.
|
|
8
|
+
*/
|
|
9
|
+
const BASE = "https://api.iconify.design";
|
|
10
|
+
const SERVED_BY = "Via Iconify (https://iconify.design)";
|
|
11
|
+
const SETS_TTL_MS = 60 * 60 * 1000;
|
|
12
|
+
let setsCache = null;
|
|
13
|
+
/** Test hook. */
|
|
14
|
+
export function resetIconifyCache() {
|
|
15
|
+
setsCache = null;
|
|
16
|
+
}
|
|
17
|
+
/** Icon sets whose license is CC0-1.0, keyed by prefix. */
|
|
18
|
+
export async function cc0Sets(fetchImpl) {
|
|
19
|
+
if (setsCache && Date.now() - setsCache.at < SETS_TTL_MS)
|
|
20
|
+
return setsCache.sets;
|
|
21
|
+
const all = await getJson(fetchImpl, "Iconify", `${BASE}/collections`);
|
|
22
|
+
const sets = Object.fromEntries(Object.entries(all).filter(([, s]) => s.license?.spdx === "CC0-1.0"));
|
|
23
|
+
setsCache = { at: Date.now(), sets };
|
|
24
|
+
return sets;
|
|
25
|
+
}
|
|
26
|
+
export function toAsset(prefix, name, set) {
|
|
27
|
+
const svg = `${BASE}/${prefix}/${name}.svg`;
|
|
28
|
+
return {
|
|
29
|
+
id: `iconify:${prefix}:${name}`,
|
|
30
|
+
kind: "image",
|
|
31
|
+
title: `${name.replace(/-/g, " ")} — ${set.name}`,
|
|
32
|
+
creator: set.author?.name ?? null,
|
|
33
|
+
landingUrl: `https://icon-sets.iconify.design/${prefix}/${name}/`,
|
|
34
|
+
fileUrl: svg,
|
|
35
|
+
thumbnailUrl: svg,
|
|
36
|
+
tags: [set.name, set.category, "icon", "svg"].filter((t) => !!t),
|
|
37
|
+
provider: "iconify",
|
|
38
|
+
origin: set.author?.url ?? "iconify.design",
|
|
39
|
+
licenseEvidence: {
|
|
40
|
+
license: "CC0-1.0",
|
|
41
|
+
licenseUrl: set.license?.url ?? CC0_LICENSE_URL,
|
|
42
|
+
method: "whole-library-cc0",
|
|
43
|
+
source: `Iconify /collections lists icon set "${prefix}" with license CC0-1.0`,
|
|
44
|
+
declaredBy: "library",
|
|
45
|
+
},
|
|
46
|
+
servedBy: SERVED_BY,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export async function searchIconify(fetchImpl, params) {
|
|
50
|
+
const sets = await cc0Sets(fetchImpl);
|
|
51
|
+
const prefixes = Object.keys(sets);
|
|
52
|
+
if (!prefixes.length)
|
|
53
|
+
return { total: 0, assets: [] };
|
|
54
|
+
// The search API has no offset; ask for enough results to cover the requested page.
|
|
55
|
+
const want = params.limit * params.page;
|
|
56
|
+
const qs = new URLSearchParams({ query: params.query, prefixes: prefixes.join(","), limit: String(Math.max(32, want)) });
|
|
57
|
+
const data = await getJson(fetchImpl, "Iconify", `${BASE}/search?${qs}`);
|
|
58
|
+
const page = data.icons.slice((params.page - 1) * params.limit, want);
|
|
59
|
+
const assets = page.flatMap((full) => {
|
|
60
|
+
const [prefix, name] = full.split(":");
|
|
61
|
+
return sets[prefix] && name ? [toAsset(prefix, name, sets[prefix])] : [];
|
|
62
|
+
});
|
|
63
|
+
return { total: data.total, assets };
|
|
64
|
+
}
|
|
65
|
+
export async function getIconify(fetchImpl, prefix, name) {
|
|
66
|
+
if (!/^[a-z0-9-]+$/.test(prefix) || !/^[a-z0-9-]+$/.test(name))
|
|
67
|
+
return null;
|
|
68
|
+
const sets = await cc0Sets(fetchImpl);
|
|
69
|
+
const set = sets[prefix];
|
|
70
|
+
if (!set)
|
|
71
|
+
return null;
|
|
72
|
+
const asset = toAsset(prefix, name, set);
|
|
73
|
+
return {
|
|
74
|
+
...asset,
|
|
75
|
+
description: set.category === "Logos" ? "Logo icon: the SVG file is CC0, but the logo is a trademark of its owner." : null,
|
|
76
|
+
files: [
|
|
77
|
+
{ label: "SVG", url: asset.fileUrl, sizeBytes: null },
|
|
78
|
+
{ label: "SVG 256px (fixed size)", url: `${asset.fileUrl}?height=256`, sizeBytes: null },
|
|
79
|
+
],
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { getJson } from "../http.js";
|
|
2
|
+
import { CC0_LICENSE_URL } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* iNaturalist observations (https://api.inaturalist.org/v1, no key).
|
|
5
|
+
* Observers choose a license per photo. The query asks for `photo_license=cc0`,
|
|
6
|
+
* and only photos whose own `license_code` is "cc0" are used.
|
|
7
|
+
*/
|
|
8
|
+
const BASE = "https://api.inaturalist.org/v1/observations";
|
|
9
|
+
const SERVED_BY = "Via iNaturalist (https://www.inaturalist.org)";
|
|
10
|
+
/** Photo URLs come in sizes (square, small, medium, large, original); swap the size segment. */
|
|
11
|
+
export const sized = (url, size) => url.replace(/\/(square|small|medium|large|original)\./, `/${size}.`);
|
|
12
|
+
const cc0Photos = (o) => (o.photos ?? []).filter((p) => p.license_code === "cc0");
|
|
13
|
+
export function toAsset(o) {
|
|
14
|
+
const photo = cc0Photos(o)[0];
|
|
15
|
+
if (!photo)
|
|
16
|
+
return null;
|
|
17
|
+
const common = o.taxon?.preferred_common_name;
|
|
18
|
+
const name = o.taxon?.name;
|
|
19
|
+
return {
|
|
20
|
+
id: `inaturalist:${o.id}`,
|
|
21
|
+
kind: "image",
|
|
22
|
+
title: [common ?? o.species_guess, name && name !== common ? `(${name})` : null].filter(Boolean).join(" ") || `Observation ${o.id}`,
|
|
23
|
+
creator: o.user?.name || o.user?.login || null,
|
|
24
|
+
landingUrl: o.uri,
|
|
25
|
+
fileUrl: sized(photo.url, "original"),
|
|
26
|
+
thumbnailUrl: sized(photo.url, "medium"),
|
|
27
|
+
tags: [name, common].filter((t) => !!t),
|
|
28
|
+
provider: "inaturalist",
|
|
29
|
+
origin: "inaturalist.org",
|
|
30
|
+
licenseEvidence: {
|
|
31
|
+
license: "CC0-1.0",
|
|
32
|
+
licenseUrl: CC0_LICENSE_URL,
|
|
33
|
+
method: "per-item-license-field",
|
|
34
|
+
source: `iNaturalist photo license_code="cc0" (${BASE}/${o.id})`,
|
|
35
|
+
declaredBy: "uploader",
|
|
36
|
+
},
|
|
37
|
+
servedBy: SERVED_BY,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export async function searchINaturalist(fetchImpl, params) {
|
|
41
|
+
const qs = new URLSearchParams({
|
|
42
|
+
q: params.query,
|
|
43
|
+
photo_license: "cc0",
|
|
44
|
+
photos: "true",
|
|
45
|
+
quality_grade: "research",
|
|
46
|
+
per_page: String(params.limit),
|
|
47
|
+
page: String(params.page),
|
|
48
|
+
});
|
|
49
|
+
const data = await getJson(fetchImpl, "iNaturalist", `${BASE}?${qs}`);
|
|
50
|
+
return { total: data.total_results, assets: data.results.map(toAsset).filter((a) => a !== null) };
|
|
51
|
+
}
|
|
52
|
+
export async function getINaturalist(fetchImpl, id) {
|
|
53
|
+
if (!/^\d+$/.test(id))
|
|
54
|
+
return null;
|
|
55
|
+
const data = await getJson(fetchImpl, "iNaturalist", `${BASE}/${id}`);
|
|
56
|
+
const o = data.results[0];
|
|
57
|
+
const asset = o ? toAsset(o) : null;
|
|
58
|
+
if (!o || !asset)
|
|
59
|
+
return null;
|
|
60
|
+
return {
|
|
61
|
+
...asset,
|
|
62
|
+
description: null,
|
|
63
|
+
files: cc0Photos(o).map((p, i) => ({
|
|
64
|
+
label: `photo ${i + 1}${p.original_dimensions ? ` (${p.original_dimensions.width}×${p.original_dimensions.height})` : ""}`,
|
|
65
|
+
url: sized(p.url, "original"),
|
|
66
|
+
sizeBytes: null,
|
|
67
|
+
})),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { getJson } from "../http.js";
|
|
2
|
+
import { CC0_LICENSE_URL } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* PhyloPic silhouettes of organisms (https://api.phylopic.org, no key).
|
|
5
|
+
* Contributors choose a license per image; only images whose license link is
|
|
6
|
+
* the CC0 1.0 deed are used. Search matches taxon names (often scientific
|
|
7
|
+
* names, e.g. "vulpes"), not free text.
|
|
8
|
+
*/
|
|
9
|
+
const BASE = "https://api.phylopic.org";
|
|
10
|
+
const SERVED_BY = "Via PhyloPic (https://www.phylopic.org)";
|
|
11
|
+
const ACCEPT = { Accept: "application/vnd.phylopic.v2+json" };
|
|
12
|
+
const CC0_RE = /creativecommons\.org\/publicdomain\/zero\/1\.0/;
|
|
13
|
+
/** The API versions its data by "build"; requests must name the current one. */
|
|
14
|
+
const BUILD_TTL_MS = 60 * 60 * 1000;
|
|
15
|
+
let buildCache = null;
|
|
16
|
+
/** Test hook. */
|
|
17
|
+
export function resetPhylopicCache() {
|
|
18
|
+
buildCache = null;
|
|
19
|
+
}
|
|
20
|
+
async function currentBuild(fetchImpl) {
|
|
21
|
+
if (buildCache && Date.now() - buildCache.at < BUILD_TTL_MS)
|
|
22
|
+
return buildCache.build;
|
|
23
|
+
const root = await getJson(fetchImpl, "PhyloPic", `${BASE}/`, ACCEPT);
|
|
24
|
+
buildCache = { at: Date.now(), build: root.build };
|
|
25
|
+
return root.build;
|
|
26
|
+
}
|
|
27
|
+
export const isCc0 = (img) => CC0_RE.test(img._links.license?.href ?? "");
|
|
28
|
+
/** Largest raster file, then the vector, as the primary file. */
|
|
29
|
+
function bestFile(img) {
|
|
30
|
+
const raster = [...(img._links.rasterFiles ?? [])].sort((a, b) => parseInt(b.sizes ?? "0") - parseInt(a.sizes ?? "0"))[0];
|
|
31
|
+
return raster?.href ?? img._links.vectorFile?.href ?? null;
|
|
32
|
+
}
|
|
33
|
+
export function toAsset(img) {
|
|
34
|
+
const thumb = [...(img._links.thumbnailFiles ?? [])].sort((a, b) => parseInt(b.sizes ?? "0") - parseInt(a.sizes ?? "0"))[0];
|
|
35
|
+
const taxon = img._links.specificNode?.title ?? img._links.self.title ?? "Silhouette";
|
|
36
|
+
return {
|
|
37
|
+
id: `phylopic:${img.uuid}`,
|
|
38
|
+
kind: "image",
|
|
39
|
+
title: `${taxon} (silhouette)`,
|
|
40
|
+
creator: img.attribution ?? img._links.contributor?.title ?? null,
|
|
41
|
+
landingUrl: `https://www.phylopic.org/images/${img.uuid}`,
|
|
42
|
+
fileUrl: bestFile(img),
|
|
43
|
+
thumbnailUrl: thumb?.href ?? null,
|
|
44
|
+
tags: [taxon, "silhouette"],
|
|
45
|
+
provider: "phylopic",
|
|
46
|
+
origin: "phylopic.org",
|
|
47
|
+
licenseEvidence: {
|
|
48
|
+
license: "CC0-1.0",
|
|
49
|
+
licenseUrl: CC0_LICENSE_URL,
|
|
50
|
+
method: "per-item-license-field",
|
|
51
|
+
source: `PhyloPic image license link is the CC0 deed (${BASE}/images/${img.uuid})`,
|
|
52
|
+
declaredBy: "uploader",
|
|
53
|
+
},
|
|
54
|
+
servedBy: SERVED_BY,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export async function searchPhylopic(fetchImpl, params) {
|
|
58
|
+
const name = params.query.toLowerCase().replace(/[^a-z\s-]/g, " ").replace(/\s+/g, " ").trim();
|
|
59
|
+
if (!name)
|
|
60
|
+
return { total: 0, assets: [] };
|
|
61
|
+
const build = await currentBuild(fetchImpl);
|
|
62
|
+
const qs = new URLSearchParams({
|
|
63
|
+
build: String(build),
|
|
64
|
+
filter_name: name,
|
|
65
|
+
// Exclude licenses that add conditions; CC0 and Public Domain Mark remain, and PDM is filtered out below.
|
|
66
|
+
filter_license_by: "false",
|
|
67
|
+
filter_license_nc: "false",
|
|
68
|
+
filter_license_sa: "false",
|
|
69
|
+
page: String(params.page - 1),
|
|
70
|
+
embed_items: "true",
|
|
71
|
+
});
|
|
72
|
+
const data = await getJson(fetchImpl, "PhyloPic", `${BASE}/images?${qs}`, ACCEPT);
|
|
73
|
+
const items = (data._embedded?.items ?? []).filter(isCc0).slice(0, params.limit);
|
|
74
|
+
return { total: data.totalItems ?? items.length, assets: items.map(toAsset) };
|
|
75
|
+
}
|
|
76
|
+
export async function getPhylopic(fetchImpl, uuid) {
|
|
77
|
+
if (!/^[0-9a-f-]{36}$/.test(uuid))
|
|
78
|
+
return null;
|
|
79
|
+
const build = await currentBuild(fetchImpl);
|
|
80
|
+
const img = await getJson(fetchImpl, "PhyloPic", `${BASE}/images/${uuid}?build=${build}`, ACCEPT);
|
|
81
|
+
if (!isCc0(img))
|
|
82
|
+
return null;
|
|
83
|
+
const files = [
|
|
84
|
+
...(img._links.vectorFile ? [{ label: "vector (SVG)", url: img._links.vectorFile.href, sizeBytes: null }] : []),
|
|
85
|
+
...(img._links.rasterFiles ?? []).map((f) => ({ label: `raster ${f.sizes ?? ""} (PNG)`.trim(), url: f.href, sizeBytes: null })),
|
|
86
|
+
];
|
|
87
|
+
return { ...toAsset(img), description: null, files };
|
|
88
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { getJson } from "../http.js";
|
|
2
|
+
import { CC0_LICENSE_URL } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* WordPress Photo Directory (https://wordpress.org/photos/), WP REST API, no key.
|
|
5
|
+
* Every photo is CC0 ("All photos are CC0 licensed" on the directory page), so
|
|
6
|
+
* CC0 is confirmed at library level. Contributors upload their own photos and
|
|
7
|
+
* the directory moderates submissions; the CC0 claim is still the uploader's,
|
|
8
|
+
* so declaredBy is "uploader".
|
|
9
|
+
*/
|
|
10
|
+
const BASE = "https://wordpress.org/photos/wp-json/wp/v2/photos";
|
|
11
|
+
const SERVED_BY = "Via WordPress Photo Directory (https://wordpress.org/photos/)";
|
|
12
|
+
/** Photo posts have a generic title ("Photo detail"); the alt text in `content` describes the photo. */
|
|
13
|
+
const TITLE_MAX = 90;
|
|
14
|
+
const text = (html) => (html ?? "").replace(/<[^>]*>/g, " ").replace(/’/g, "'").replace(/&/g, "&").replace(/&#\d+;/g, " ").replace(/\s+/g, " ").trim();
|
|
15
|
+
export function titleOf(p) {
|
|
16
|
+
const alt = text(p.content?.rendered);
|
|
17
|
+
if (alt)
|
|
18
|
+
return alt.length > TITLE_MAX ? `${alt.slice(0, TITLE_MAX - 1).trimEnd()}…` : alt;
|
|
19
|
+
return text(p.title?.rendered) || `Photo ${p.id}`;
|
|
20
|
+
}
|
|
21
|
+
export function toAsset(p) {
|
|
22
|
+
const media = p._embedded?.["wp:featuredmedia"]?.[0];
|
|
23
|
+
return {
|
|
24
|
+
id: `wpphotos:${p.id}`,
|
|
25
|
+
kind: "image",
|
|
26
|
+
title: titleOf(p),
|
|
27
|
+
creator: p._embedded?.author?.[0]?.name ?? null,
|
|
28
|
+
landingUrl: p.link,
|
|
29
|
+
fileUrl: media?.source_url ?? null,
|
|
30
|
+
thumbnailUrl: p["photo-thumbnail-url"] ?? null,
|
|
31
|
+
tags: [],
|
|
32
|
+
provider: "wpphotos",
|
|
33
|
+
origin: "wordpress.org/photos",
|
|
34
|
+
licenseEvidence: {
|
|
35
|
+
license: "CC0-1.0",
|
|
36
|
+
licenseUrl: CC0_LICENSE_URL,
|
|
37
|
+
method: "whole-library-cc0",
|
|
38
|
+
source: "https://wordpress.org/photos/ ('All photos are CC0 licensed')",
|
|
39
|
+
declaredBy: "uploader",
|
|
40
|
+
},
|
|
41
|
+
servedBy: SERVED_BY,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export async function searchWpPhotos(fetchImpl, params) {
|
|
45
|
+
const qs = new URLSearchParams({
|
|
46
|
+
search: params.query,
|
|
47
|
+
per_page: String(params.limit),
|
|
48
|
+
page: String(params.page),
|
|
49
|
+
_embed: "author,wp:featuredmedia",
|
|
50
|
+
});
|
|
51
|
+
const photos = await getJson(fetchImpl, "WordPress Photo Directory", `${BASE}?${qs}`);
|
|
52
|
+
// The response total lives in a header the shared fetch helper does not expose; report this page's size.
|
|
53
|
+
return { total: photos.length, assets: photos.map(toAsset) };
|
|
54
|
+
}
|
|
55
|
+
export async function getWpPhoto(fetchImpl, id) {
|
|
56
|
+
if (!/^\d+$/.test(id))
|
|
57
|
+
return null;
|
|
58
|
+
const p = await getJson(fetchImpl, "WordPress Photo Directory", `${BASE}/${id}?_embed=author,wp:featuredmedia`);
|
|
59
|
+
const media = p._embedded?.["wp:featuredmedia"]?.[0];
|
|
60
|
+
const files = [];
|
|
61
|
+
if (media?.source_url) {
|
|
62
|
+
const d = media.media_details;
|
|
63
|
+
files.push({ label: `original${d?.width ? ` (${d.width}×${d.height})` : ""}`, url: media.source_url, sizeBytes: d?.filesize ?? null });
|
|
64
|
+
for (const [name, size] of Object.entries(d?.sizes ?? {})) {
|
|
65
|
+
if (name !== "full")
|
|
66
|
+
files.push({ label: `${name} (${size.width}×${size.height})`, url: size.source_url, sizeBytes: null });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return { ...toAsset(p), description: text(p.content?.rendered) || null, files };
|
|
70
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { getJson } from "../http.js";
|
|
2
|
+
import { sanitizeQuery } from "./archive.js";
|
|
3
|
+
import { CC0_LICENSE_URL } from "../types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Zenodo research records (https://zenodo.org/api, no key): figures, photos
|
|
6
|
+
* and videos deposited by researchers. The query requires
|
|
7
|
+
* `metadata.rights.id:cc0-1.0`, and each record must report license "cc-zero".
|
|
8
|
+
*/
|
|
9
|
+
const BASE = "https://zenodo.org/api/records";
|
|
10
|
+
const SERVED_BY = "Via Zenodo (https://zenodo.org)";
|
|
11
|
+
export const isCc0 = (r) => r.metadata.license?.id === "cc-zero";
|
|
12
|
+
const IMAGE_FILE = /\.(png|jpe?g|gif|webp|tiff?|svg)$/i;
|
|
13
|
+
export function toAsset(r) {
|
|
14
|
+
const kind = r.metadata.resource_type?.type === "video" ? "video" : "image";
|
|
15
|
+
const preview = (r.files ?? []).find((f) => IMAGE_FILE.test(f.key));
|
|
16
|
+
return {
|
|
17
|
+
id: `zenodo:${r.id}`,
|
|
18
|
+
kind,
|
|
19
|
+
title: r.metadata.title,
|
|
20
|
+
creator: (r.metadata.creators ?? []).map((c) => c.name).slice(0, 3).join("; ") || null,
|
|
21
|
+
landingUrl: r.links.self_html ?? `https://zenodo.org/records/${r.id}`,
|
|
22
|
+
fileUrl: (preview ?? r.files?.[0])?.links.self ?? null,
|
|
23
|
+
thumbnailUrl: preview?.links.self ?? null,
|
|
24
|
+
tags: (r.metadata.keywords ?? []).slice(0, 20),
|
|
25
|
+
provider: "zenodo",
|
|
26
|
+
origin: "zenodo.org",
|
|
27
|
+
licenseEvidence: {
|
|
28
|
+
license: "CC0-1.0",
|
|
29
|
+
licenseUrl: CC0_LICENSE_URL,
|
|
30
|
+
method: "per-item-license-field",
|
|
31
|
+
source: `Zenodo record license "cc-zero" (${BASE}/${r.id})`,
|
|
32
|
+
declaredBy: "uploader",
|
|
33
|
+
},
|
|
34
|
+
servedBy: SERVED_BY,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export async function searchZenodo(fetchImpl, kind, params) {
|
|
38
|
+
const words = sanitizeQuery(params.query);
|
|
39
|
+
if (!words)
|
|
40
|
+
return { total: 0, assets: [] };
|
|
41
|
+
const qs = new URLSearchParams({ q: `(${words}) AND metadata.rights.id:cc0-1.0`, size: String(params.limit), page: String(params.page) });
|
|
42
|
+
if (kind !== "all")
|
|
43
|
+
qs.set("type", kind);
|
|
44
|
+
else {
|
|
45
|
+
qs.append("type", "image");
|
|
46
|
+
qs.append("type", "video");
|
|
47
|
+
}
|
|
48
|
+
const data = await getJson(fetchImpl, "Zenodo", `${BASE}?${qs}`);
|
|
49
|
+
return { total: data.hits.total, assets: data.hits.hits.filter(isCc0).map(toAsset) };
|
|
50
|
+
}
|
|
51
|
+
export async function getZenodo(fetchImpl, id) {
|
|
52
|
+
if (!/^\d+$/.test(id))
|
|
53
|
+
return null;
|
|
54
|
+
const r = await getJson(fetchImpl, "Zenodo", `${BASE}/${id}`);
|
|
55
|
+
if (!isCc0(r))
|
|
56
|
+
return null;
|
|
57
|
+
return {
|
|
58
|
+
...toAsset(r),
|
|
59
|
+
description: r.metadata.description?.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim().slice(0, 500) || null,
|
|
60
|
+
files: (r.files ?? []).map((f) => ({ label: f.key, url: f.links.self, sizeBytes: f.size ?? null })),
|
|
61
|
+
};
|
|
62
|
+
}
|