moodboard.mjs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/usr/bin/env node
  2. /**
  3. * moodboard.mjs — visual reference search → downloaded images → one contact sheet you can actually look at.
  4. *
  5. * Usage:
  6. * node scripts/moodboard.mjs "<query>" ["<query2>" ...] [options]
  7. *
  8. * Options:
  9. * --source bing,pinterest,arena which sources (default all three)
  10. * --limit N images kept in total (default 24, cap 60)
  11. * --out DIR output dir (default design/moodboard)
  12. * --cols N contact-sheet columns (default 5)
  13. * --min-kb N skip images smaller than this (default 15)
  14. *
  15. * Writes DIR/img/NN.<ext>, DIR/contact-sheet*.png (LOOK AT THESE), DIR/MOODBOARD.md.
  16. * Exit 0 if any image landed.
  17. *
  18. * Source notes (probed live, 2026-08):
  19. * · bing — most reliable. Indexes pinimg / dribbble / behance CDNs and serves
  20. * hotlinkable originals through a browser context. This is the workhorse.
  21. * · pinterest — logged-out search renders a partial grid behind a login wall: sometimes
  22. * ~15 pins, sometimes zero. Best-effort, never load-bearing. Thumbnails are
  23. * rewritten 236x → 736x (originals/ is 403 for hotlinks).
  24. * · arena — api.are.na public search, no auth. Curated designer taste, lower volume.
  25. */
  26. import { mkdir, writeFile } from 'fs/promises';
  27. import { resolve, join } from 'path';
  28. const args = process.argv.slice(2);
  29. if (!args.length || args.includes('--help')) {
  30. console.log(`moodboard.mjs — image search → contact sheet
  31. node scripts/moodboard.mjs "<query>" ["<query2>" ...] [--source bing,pinterest,arena]
  32. [--limit 24] [--out design/moodboard] [--cols 5] [--min-kb 15]`);
  33. process.exit(0);
  34. }
  35. const get = (f, d) => { const i = args.indexOf(f); return i !== -1 ? args[i + 1] : d; };
  36. const flagIdx = args.findIndex(a => a.startsWith('--'));
  37. const queries = (flagIdx === -1 ? args : args.slice(0, flagIdx)).filter(Boolean);
  38. const sources = get('--source', 'bing,pinterest,arena').split(',').map(s => s.trim());
  39. const limit = Math.min(parseInt(get('--limit', '24'), 10) || 24, 60);
  40. const outDir = resolve(get('--out', 'design/moodboard'));
  41. const cols = parseInt(get('--cols', '5'), 10) || 5;
  42. const minKb = parseInt(get('--min-kb', '15'), 10) || 15;
  43. if (!queries.length) { console.error('[moodboard] no query given.'); process.exit(1); }
  44. let chromium;
  45. try { ({ chromium } = await import('playwright')); }
  46. catch { console.error('playwright not found. Install: npm i -D playwright && npx playwright install chromium'); process.exit(2); }
  47. const UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
  48. const browser = await chromium.launch({ headless: true });
  49. const ctx = await browser.newContext({ userAgent: UA, viewport: { width: 1440, height: 1000 }, ignoreHTTPSErrors: true });
  50. async function open(page, url, settle = 4000) {
  51. await page.goto(url, { waitUntil: 'commit', timeout: 30_000 });
  52. const dcl = await page.waitForLoadState('domcontentloaded', { timeout: 15_000 }).then(() => true).catch(() => false);
  53. await page.waitForTimeout(settle);
  54. if (!dcl) await page.evaluate(() => window.stop()).catch(() => {}); // stream that never closes
  55. }
  56. // --- sources ------------------------------------------------------------------
  57. async function fromBing(q, want) {
  58. const page = await ctx.newPage();
  59. const out = [];
  60. try {
  61. await open(page, 'https://www.bing.com/images/search?q=' + encodeURIComponent(q) + '&form=HDRSC2', 3500);
  62. for (let pass = 0; pass < 3 && out.length < want; pass++) {
  63. const items = await page.evaluate(() =>
  64. [...document.querySelectorAll('a.iusc')].map(a => { try { return JSON.parse(a.getAttribute('m')); } catch { return null; } }).filter(Boolean));
  65. for (const it of items) {
  66. if (out.length >= want) break;
  67. if (it.murl && !out.some(o => o.img === it.murl)) out.push({ img: it.murl, thumb: it.turl, page: it.purl, source: 'bing' });
  68. }
  69. await page.evaluate(() => window.scrollBy(0, innerHeight * 2));
  70. await page.waitForTimeout(1800);
  71. }
  72. } catch (e) { console.error(`[moodboard] bing "${q}": ${e.message.split('\n')[0]}`); }
  73. finally { await page.close(); }
  74. return out;
  75. }
  76. async function fromPinterest(q, want) {
  77. const page = await ctx.newPage();
  78. const out = [];
  79. try {
  80. await open(page, 'https://www.pinterest.com/search/pins/?q=' + encodeURIComponent(q) + '&rs=typed', 6000);
  81. for (let pass = 0; pass < 4 && out.length < want; pass++) {
  82. const items = await page.evaluate(() =>
  83. [...document.querySelectorAll('img')]
  84. .filter(i => /i\.pinimg\.com\/\d+x\//.test(i.src))
  85. .map(i => ({ src: i.src, pin: i.closest('a[href^="/pin/"]')?.getAttribute('href') || null })));
  86. for (const it of items) {
  87. // 736x is the largest size that stays hotlinkable; /originals/ returns 403.
  88. const img = it.src.replace(/\/\d+x\//, '/736x/');
  89. if (out.length >= want) break;
  90. if (!out.some(o => o.img === img)) out.push({ img, thumb: it.src, page: it.pin ? 'https://www.pinterest.com' + it.pin : 'https://www.pinterest.com/search/pins/?q=' + encodeURIComponent(q), source: 'pinterest' });
  91. }
  92. await page.evaluate(() => window.scrollBy(0, innerHeight * 1.5));
  93. await page.waitForTimeout(2000);
  94. }
  95. if (!out.length) console.error('[moodboard] pinterest returned 0 (login wall) — bing covers it.');
  96. } catch (e) { console.error(`[moodboard] pinterest "${q}": ${e.message.split('\n')[0]}`); }
  97. finally { await page.close(); }
  98. return out;
  99. }
  100. async function fromArena(q, want) {
  101. const out = [];
  102. try {
  103. const res = await ctx.request.get(`https://api.are.na/v2/search?q=${encodeURIComponent(q)}&per=${Math.min(want * 2, 40)}`, { timeout: 20_000 });
  104. const json = await res.json();
  105. for (const b of json.blocks || []) {
  106. const img = b.image?.large?.url || b.image?.display?.url || b.image?.original?.url;
  107. if (!img || out.length >= want) continue;
  108. out.push({ img, thumb: b.image?.thumb?.url, page: `https://www.are.na/block/${b.id}`, source: 'arena', title: b.title });
  109. }
  110. } catch (e) { console.error(`[moodboard] are.na "${q}": ${e.message.split('\n')[0]}`); }
  111. return out;
  112. }
  113. // --- collect ------------------------------------------------------------------
  114. const perSourcePerQuery = Math.ceil(limit / (sources.length * queries.length)) + 4;
  115. let candidates = [];
  116. for (const q of queries) {
  117. for (const s of sources) {
  118. const fn = s === 'bing' ? fromBing : s === 'pinterest' ? fromPinterest : s === 'arena' ? fromArena : null;
  119. if (!fn) { console.error(`[moodboard] unknown source "${s}"`); continue; }
  120. const got = await fn(q, perSourcePerQuery);
  121. process.stderr.write(`[moodboard] ${s} · "${q}" → ${got.length}\n`);
  122. candidates.push(...got.map(g => ({ ...g, query: q })));
  123. }
  124. }
  125. // dedupe by url, then interleave sources so one source can't own the whole sheet
  126. const seen = new Set();
  127. candidates = candidates.filter(c => !seen.has(c.img) && seen.add(c.img));
  128. const bySource = sources.map(s => candidates.filter(c => c.source === s));
  129. const ordered = [];
  130. for (let i = 0; ordered.length < candidates.length; i++) {
  131. let added = false;
  132. for (const bucket of bySource) if (bucket[i]) { ordered.push(bucket[i]); added = true; }
  133. if (!added) break;
  134. }
  135. // --- download -----------------------------------------------------------------
  136. await mkdir(join(outDir, 'img'), { recursive: true });
  137. const kept = [];
  138. const sizes = new Set();
  139. for (const c of ordered) {
  140. if (kept.length >= limit) break;
  141. let buf = null, ct = '';
  142. for (const url of [c.img, c.thumb].filter(Boolean)) {
  143. try {
  144. const r = await ctx.request.get(url, { timeout: 20_000, headers: { Referer: c.page || '' } });
  145. if (!r.ok()) continue;
  146. ct = r.headers()['content-type'] || '';
  147. if (!/^image\//.test(ct)) continue;
  148. const b = await r.body();
  149. if (b.length < minKb * 1024) continue;
  150. buf = b; c.used = url; break;
  151. } catch {}
  152. }
  153. if (!buf) continue;
  154. if (sizes.has(buf.length)) continue; // cheap byte-identical dedupe across sources
  155. sizes.add(buf.length);
  156. const ext = /png/.test(ct) ? 'png' : /webp/.test(ct) ? 'webp' : /gif/.test(ct) ? 'gif' : 'jpg';
  157. const n = String(kept.length + 1).padStart(2, '0');
  158. const file = `img/${n}.${ext}`;
  159. await writeFile(join(outDir, file), buf);
  160. kept.push({ ...c, n, file, kb: Math.round(buf.length / 1024) });
  161. }
  162. // --- contact sheet ------------------------------------------------------------
  163. // Rendering an HTML grid and screenshotting it needs no extra dependency and copes
  164. // with mixed aspect ratios — one image the model reads instead of 24 separate ones.
  165. const PER_SHEET = 20;
  166. // A remainder of one or two tiles becoming its own 95%-empty page costs a second look for nothing,
  167. // and the doctrine promises reading a moodboard costs one look. Balance the sheets instead.
  168. const sheetCount = Math.max(1, Math.round(kept.length / PER_SHEET));
  169. const perSheet = Math.ceil(kept.length / sheetCount);
  170. const sheets = [];
  171. for (let s = 0; s * perSheet < kept.length; s++) {
  172. const slice = kept.slice(s * perSheet, (s + 1) * perSheet);
  173. const html = `<!doctype html><meta charset="utf-8"><style>
  174. body{margin:0;background:#111;font:13px/1.2 ui-monospace,monospace;color:#eee}
  175. .g{display:grid;grid-template-columns:repeat(${cols},1fr);gap:10px;padding:10px}
  176. figure{margin:0;background:#1c1c1c;border-radius:4px;overflow:hidden}
  177. img{display:block;width:100%;height:210px;object-fit:contain;background:#0a0a0a}
  178. figcaption{padding:4px 6px;color:#9a9a9a}
  179. b{color:#fff}
  180. </style><div class=g>${slice.map(k =>
  181. `<figure><img src="img/${k.file.split('/')[1]}"><figcaption><b>${k.n}</b> ${k.source}</figcaption></figure>`).join('')}</div>`;
  182. const f = join(outDir, `_sheet${s}.html`);
  183. await writeFile(f, html, 'utf8');
  184. const page = await ctx.newPage();
  185. await page.setViewportSize({ width: cols * 320, height: 1000 });
  186. await page.goto('file:///' + f.replace(/\\/g, '/'), { waitUntil: 'load', timeout: 20_000 }).catch(() => {});
  187. await page.waitForTimeout(1200);
  188. const name = sheetCount === 1 ? 'contact-sheet.png' : `contact-sheet-${s + 1}.png`;
  189. await page.screenshot({ path: join(outDir, name), fullPage: true });
  190. await page.close();
  191. sheets.push(name);
  192. }
  193. await browser.close();
  194. // --- index --------------------------------------------------------------------
  195. const md = [
  196. `# MOODBOARD — ${queries.map(q => `"${q}"`).join(' · ')}`,
  197. '',
  198. sheets.length
  199. ? `**Look at ${sheets.map(s => `\`${s}\``).join(', ')} first** — every tile is numbered; refer to tiles by number below.`
  200. : '_no contact sheet: nothing downloaded._',
  201. '',
  202. 'Reference images are for *direction* — palette, light, composition, texture, type energy.',
  203. 'They are not assets: never ship a downloaded image, and never reproduce one composition.',
  204. 'Name what you take from each tile you use, then throw the rest away.',
  205. '',
  206. '| # | source | kb | origin page |',
  207. '|---|---|---|---|',
  208. ...kept.map(k => `| ${k.n} | ${k.source} | ${k.kb} | ${k.page ? `[link](${k.page})` : '—'} |`),
  209. '',
  210. '## Read (fill in)',
  211. '- dominant palette across the tiles you like: ',
  212. '- light / contrast character: ',
  213. '- composition move worth stealing: ',
  214. '- what to explicitly avoid from these: ',
  215. '',
  216. ].join('\n');
  217. await writeFile(join(outDir, 'MOODBOARD.md'), md, 'utf8');
  218. console.log(`\n=== moodboard ===`);
  219. console.log(`queries : ${queries.join(' | ')}`);
  220. console.log(`kept : ${kept.length} images (${sources.map(s => `${s}:${kept.filter(k => k.source === s).length}`).join(' ')})`);
  221. console.log(`sheets : ${sheets.map(s => join(outDir, s)).join('\n ') || '—'}`);
  222. console.log(`index : ${join(outDir, 'MOODBOARD.md')}`);
  223. process.exit(kept.length ? 0 : 1);