#!/usr/bin/env node /** * 画个标 · 素材源发现器 * * 目标:把「找新素材源」从人工探测变成一次可重复运行的扫描,输出候选清单供准入筛选。 * * 数据来源(全部公开 API,无需鉴权): * 1. Iconify https://api.iconify.design/collections —— 图标集合全集与差值 * 2. Fontsource https://api.fontsource.org/v1/fonts —— Google Fonts 全字族 * 3. Poly Haven https://api.polyhaven.com/assets?t=textures —— CC0 纹理 * 4. GitHub https://api.github.com/search/repositories —— 主题检索(限流 10 次/分) * * 产物: * js/data/candidates.json 候选清单(按 score 降序) * js/data/sources/registry.json 源注册表(active 全量快照 + rejected 排除记录) * * 用法: * node scripts/discover-sources.js # 扫描并写入上述两个文件 * node scripts/discover-sources.js --dry # 只打印摘要,不写文件 * node scripts/discover-sources.js --only=iconify,font,texture */ 'use strict'; const fs = require('fs'); const path = require('path'); const ROOT = path.resolve(__dirname, '..'); const ICON_SRC = path.join(ROOT, 'js', 'data', 'icon-sources.js'); const POSTER_SRC = path.join(ROOT, 'js', 'data', 'poster-sources.js'); const OUT_CANDIDATES = path.join(ROOT, 'js', 'data', 'candidates.json'); const OUT_REGISTRY = path.join(ROOT, 'js', 'data', 'sources', 'registry.json'); /* ---------------- 准入规则(与项目规则一致) ---------------- */ const LICENSE_OK = /^(MIT|ISC|Apache-2\.0|Apache 2\.0|OFL-1\.1|CC0(-1\.0)?|Unlicense|BSD-3-Clause|BSD-2-Clause|MPL-2\.0)$/i; const LICENSE_BLOCK = /BY-SA|BY-NC|BY-ND|\bGPL\b|NonCommercial|非商业|禁止商用/i; /** 已知排除项(协议/可拉取性/同质重复),避免每轮重复踩坑 */ const REJECTED = [ { id: 'undraw', kind: 'poster', reason: '自定义协议禁止 AI/机器学习训练,与 AI 海报生成冲突' }, { id: 'open-doodles', kind: 'poster', reason: 'CC0 但托管控于 Dropbox/Webflow,无可用直链仓库' }, { id: 'hero-patterns', kind: 'poster', reason: '仓库无 LICENSE 文件,合规风险' }, { id: 'arcticons', kind: 'icon', reason: 'CC BY-SA 4.0 传染性协议,不可商用收录' }, { id: 'openmoji', kind: 'icon', reason: 'CC BY-SA 4.0 传染性协议(已通过 iconify 收录的源需复查)' }, { id: '3dicons', kind: 'icon', reason: '仓库是站点源码,无批量静态素材(仅 12 张示例图)' }, { id: 'svgwave', kind: 'poster', reason: '运行时生成器,无静态 SVG 产物' }, { id: 'inkjet', kind: 'poster', reason: '运行时生成器,无静态 SVG 产物' }, { id: 'mrmrs-patterns', kind: 'poster', reason: 'jsDelivr 未索引,无法直拉' }, { id: 'dracula', kind: 'poster', reason: '主题色存于扩展仓库,主仓库无配色 JSON' }, { id: 'montserrat', kind: 'poster', reason: '仓库体积超 jsDelivr 索引上限' }, { id: 'noto-emoji-repo', kind: 'icon', reason: '1.4GB 仓库,整树枚举不稳定;改用 GitHub Trees API 分目录(P3 已用子树方式接入)' } ]; function readSources(file) { if (!fs.existsSync(file)) return []; delete require.cache[require.resolve(file)]; const list = require(file); return Array.isArray(list) ? list : []; } async function getJSON(url, headers) { const r = await fetch(url, { headers: Object.assign({ 'User-Agent': 'drawlogo-discover' }, headers || {}) }); if (!r.ok) throw new Error(url + ' → HTTP ' + r.status); return r.json(); } const sleep = ms => new Promise(r => setTimeout(r, ms)); /* ---------------- 1. Iconify ---------------- */ async function scanIconify(iconSources) { const used = new Set(); for (const s of iconSources) { used.add(s.id); if (s.npm) { const m = String(s.npm).match(/@iconify-json\/([^@/\s]+)/); if (m) used.add(m[1]); } } const all = await getJSON('https://api.iconify.design/collections'); const out = []; for (const [prefix, v] of Object.entries(all)) { const aliases = v.aliases || []; if (used.has(prefix) || aliases.some(a => used.has(a))) continue; const license = (v.license && (v.license.spdx || v.license.title)) || '?'; out.push({ kind: 'icon', provider: 'iconify', id: prefix, name: v.name || prefix, volume: v.total || 0, license, licenseOk: LICENSE_OK.test(license) && !LICENSE_BLOCK.test(license), category: v.category || '', url: 'https://icon-sets.iconify.design/' + prefix + '/', note: 'Iconify 集合,可直接用 format:iconify 接入' }); } return out; } /* ---------------- 2. Fontsource(Google Fonts) ---------------- */ async function scanFontsource(posterSources) { const fonts = await getJSON('https://api.fontsource.org/v1/fonts'); const usedIds = new Set(posterSources.filter(s => s.type === 'font').map(s => s.id)); const out = []; for (const f of fonts) { if (!LICENSE_OK.test(f.license || '') || LICENSE_BLOCK.test(f.license || '')) continue; if (!Array.isArray(f.subsets) || !f.subsets.includes('latin')) continue; // 已收录的字体源(fontsource-xxx / noto-xxx)跳过 if (usedIds.has('fontsource-' + f.id)) continue; const weights = (f.weights || []).filter(w => w === 400 || w === 700); out.push({ kind: 'poster', provider: 'fontsource', id: f.id, name: f.family, volume: Math.max(1, weights.length), license: f.license, licenseOk: true, category: 'font', url: 'https://fontsource.org/fonts/' + f.id, note: weights.length ? 'latin ' + weights.join('/') + ' 可用' : '缺 400/700 标准字重' }); } return out; } /* ---------------- 3. Poly Haven 纹理 ---------------- */ async function scanPolyhaven(posterSources) { const used = new Set(posterSources.map(s => s.id)); if (used.has('polyhaven-textures')) return []; const all = await getJSON('https://api.polyhaven.com/assets?t=textures'); const cats = {}; for (const id of Object.keys(all)) { for (const c of (all[id].categories || [])) cats[c] = (cats[c] || 0) + 1; } const top = Object.entries(cats).sort((a, b) => b[1] - a[1]).slice(0, 8) .map(([c, n]) => c + '(' + n + ')').join('、'); return [{ kind: 'poster', provider: 'polyhaven', id: 'polyhaven-textures', name: 'Poly Haven Textures', volume: Object.keys(all).length, license: 'CC0-1.0', licenseOk: true, category: 'bg', url: 'https://polyhaven.com/textures', note: 'CC0 可商用免署名;分类:' + top }]; } /* ---------------- 4. GitHub 主题检索 ---------------- */ async function scanGithub() { const topics = [ 'topic:icon-set license:mit', 'topic:3d-icons license:mit', 'topic:emoji license:mit', 'topic:texture cc0-1.0' ]; const out = []; for (const q of topics) { try { const j = await getJSON('https://api.github.com/search/repositories?q=' + encodeURIComponent(q) + '&sort=stars&order=desc&per_page=10', { Accept: 'application/vnd.github+json' }); for (const r of (j.items || [])) { const license = (r.license && r.license.spdx_id) || '?'; out.push({ kind: q.includes('texture') ? 'poster' : 'icon', provider: 'github', id: r.full_name.replace('/', '-').toLowerCase(), name: r.full_name, volume: r.stargazers_count, license, licenseOk: LICENSE_OK.test(license), category: r.topics && r.topics.slice(0, 3).join(',') || '', url: r.html_url, note: '★' + r.stargazers_count + ' · 仓库 ' + Math.round(r.size / 1024) + 'MB(volume 为星数,需人工确认素材量)' }); } } catch (e) { console.warn(' ⚠️ GitHub 检索失败(' + q + '):' + e.message); } await sleep(6500); // 未鉴权搜索接口限流 10 次/分钟 } return out; } /* ---------------- 评分与输出 ---------------- */ function score(c) { if (!c.licenseOk) return 0; const vol = Math.min(c.volume, 50000) / 50000; const kindWeight = c.kind === 'icon' ? 1 : 1.1; return +(vol * 60 * kindWeight + 40).toFixed(1); } async function main() { const dry = process.argv.includes('--dry'); const onlyArg = (process.argv.find(a => a.startsWith('--only=')) || '').replace('--only=', ''); const only = onlyArg ? new Set(onlyArg.split(',')) : null; const iconSources = readSources(ICON_SRC); const posterSources = readSources(POSTER_SRC); console.log(`已收录:图标源 ${iconSources.length},海报源 ${posterSources.length}`); const candidates = []; const tasks = [ ['iconify', () => scanIconify(iconSources)], ['font', () => scanFontsource(posterSources)], ['texture', () => scanPolyhaven(posterSources)], ['github', () => scanGithub()] ]; for (const [key, fn] of tasks) { if (only && !only.has(key)) continue; try { const list = await fn(); console.log(`扫描 ${key}: ${list.length} 个候选`); candidates.push(...list); } catch (e) { console.warn(`扫描 ${key} 失败:${e.message}`); } } for (const c of candidates) c.score = score(c); candidates.sort((a, b) => b.score - a.score || b.volume - a.volume); const ok = candidates.filter(c => c.licenseOk); const blocked = candidates.filter(c => !c.licenseOk); // 注册表:active 全量快照 + rejected 记录 const registry = { updatedAt: new Date().toISOString().slice(0, 10), iconSources: {}, posterSources: {}, rejected: REJECTED }; for (const s of iconSources) { registry.iconSources[s.id] = { format: s.format || '', license: s.license || '', type: s.type || '', added: s.added || '' }; } for (const s of posterSources) { registry.posterSources[s.id] = { format: s.format || '', license: s.license || '', type: s.type || '', added: s.added || '' }; } console.log(`\n可用候选 ${ok.length} 个(占比 ${(ok.length / Math.max(1, candidates.length) * 100).toFixed(0)}%),` + `协议不合规 ${blocked.length} 个已排除`); console.log('Top 10:'); for (const c of ok.slice(0, 10)) { console.log(` ${String(c.score).padStart(5)} ${c.provider.padEnd(11)} ${c.id.padEnd(28)} ` + `${String(c.volume).padStart(6)} ${c.license}`); } const byProvider = {}; for (const c of ok) byProvider[c.provider] = (byProvider[c.provider] || 0) + 1; console.log('候选分布:' + JSON.stringify(byProvider)); if (dry) { console.log('\n--dry 模式,未写文件'); return; } fs.mkdirSync(path.dirname(OUT_REGISTRY), { recursive: true }); fs.writeFileSync(OUT_CANDIDATES, JSON.stringify({ generatedAt: new Date().toISOString(), total: candidates.length, candidates }, null, 2) + '\n', 'utf8'); fs.writeFileSync(OUT_REGISTRY, JSON.stringify(registry, null, 2) + '\n', 'utf8'); console.log('\n已写入:' + path.relative(ROOT, OUT_CANDIDATES) + '、' + path.relative(ROOT, OUT_REGISTRY)); } main().catch(e => { console.error('❌ 发现器失败:' + e.message); process.exit(1); });