← back to Interiordesignershowroom

scripts/ingest-samplize.js

41 lines

// Ingest the Samplize peel-and-stick paint samples = the Room Builder's wall-color
// layer. They're a CJ advertiser; pull across paint searches, filter to Samplize,
// upsert, and flag is_wall_paint so the color picker can find them.
try { require('dotenv').config(); } catch (_) {}
const db = require('../lib/db');
const cj = require('../lib/adapters/cj');
const { normalizeProduct, UPSERT_SQL, upsertParams } = require('../lib/normalize');
const sleep = ms => new Promise(r => setTimeout(r, ms));

const SEARCHES = [
  'peel and stick paint sample', 'benjamin moore paint sample', 'sherwin williams paint sample',
  'paint color sample', 'wall paint swatch', 'peel stick paint blue', 'peel stick paint green',
  'peel stick paint white', 'peel stick paint gray', 'peel stick paint neutral',
];

async function main() {
  await db.query('SELECT 1');
  if (!cj.enabled(process.env)) { console.error('CJ not configured'); process.exit(1); }
  const seen = new Set(); let ok = 0;
  for (const q of SEARCHES) {
    let raws = [];
    try { raws = await cj.fetch(process.env, { limit: 60, keywords: q }); } catch (e) { continue; }
    for (const raw of raws) {
      if (!/samplize/i.test(raw.advertiser || '')) continue;
      if (!raw.image_url || seen.has(raw.external_id)) continue;
      seen.add(raw.external_id);
      const norm = normalizeProduct(raw, 'cj');
      if (!norm) continue;
      norm.category = 'Wall Paint';
      try { await db.query(UPSERT_SQL, upsertParams(norm)); ok++; } catch (_) {}
    }
    await sleep(400);
  }
  // flag them as the wall-paint layer
  await db.query(`UPDATE products SET is_wall_paint=TRUE, room='paint', category='Wall Paint' WHERE advertiser ILIKE 'samplize'`);
  const { rows } = await db.query(`SELECT count(*) n FROM products WHERE is_wall_paint`);
  console.log(`Samplize wall-paint samples ingested: ${ok} new; total paint layer = ${rows[0].n}`);
  await db.pool.end();
}
main().catch(e => { console.error(e); process.exit(1); });