← back to Embroideredwallpaper

scripts/pull-from-shopify.js

116 lines

// Embroidered wallpaper pull from live DW Shopify catalog.
// STRICT scope: genuine embroidered WALLCOVERINGS only.
// The "embroidery" tag in DW catalog is heavily polluted by Fabric/Drapery/
// Multipurpose items mis-tagged as Wallcovering. We require BOTH:
//   (1) product_type === 'Wallcovering'  (kept from base template)
//   (2) vendor is on the genuine-embroidered-wallcovering allowlist
//   (3) niche keyword (embroidery / stitched / embroidered) on title or tags
// AND we hard-reject titles that contain "Drapery|Multipurpose|Upholstery Embroidery"
// regardless of product_type, since those are mis-typed Fabrics.
const https = require('https');
const fs = require('fs');
const path = require('path');
const { classifyAesthetic } = require('../../_shared/microsite-aesthetic');
const SITE_CFG_RAILS = (() => { try { return require('../site.config.json').rails || []; } catch { return []; } })();

const NICHE_TAG = /\b(embroider(?:y|ed)|stitched|crewel)\b/i;
const NICHE_TITLE = /\b(embroider(?:y|ed)|stitched|crewel)\b/i;

// Hard-reject mis-typed Fabric items even if product_type says Wallcovering.
const HARD_REJECT_TITLE = /(drapery[\s_-]?embroidery|multipurpose[\s_-]?embroidery|upholstery[\s_-]?embroidery)/i;

const REJECT_TITLE = /(visual.{0,3}merchandiser|bh.?90210|\bimage[ _-]?4\b|lamp|rug|pillow|throw|tripod|frame|mirror|vase|candle|sculpture|figurine)/i;

// Genuine embroidered-wallcovering vendors in DW catalog.
const VENDOR_ALLOWLIST = [
  'schumacher wallpaper',
  'scalamandre wallpaper',
  'mind the gap',
  'cole & son',
  'cole and son',
  'armani casa',
  'arte international',
  'phillipe romano',
  'china seas',
  'missoni wallpaper',
  'phillip jeffries',
];

function vendorMatches(vendor) {
  const v = (vendor || '').toLowerCase();
  return VENDOR_ALLOWLIST.some(name => v.includes(name));
}

function fetchPage(page) {
  return new Promise((resolve, reject) => {
    https.get(`https://designerwallcoverings.com/products.json?limit=250&page=${page}`, {
      headers: { 'User-Agent': 'Mozilla/5.0 embroideredwallpaper-builder' }
    }, (res) => {
      let data = '';
      res.on('data', c => data += c);
      res.on('end', () => { try { resolve(JSON.parse(data).products || []); } catch(e) { reject(e); } });
    }).on('error', reject);
  });
}

function aestheticOf(tags, title) {
  const blob = ((tags || []).join(' ') + ' ' + (title || '')).toLowerCase();
  if (/crewel/.test(blob)) return 'crewel';
  if (/stitched/.test(blob)) return 'stitched';
  return 'embroidered';
}

function classifyForSite(tags, title) {
  const railsMatch = classifyAesthetic(tags, SITE_CFG_RAILS);
  if (railsMatch && railsMatch !== 'all') return railsMatch;
  return aestheticOf(tags, title);
}

(async () => {
  const all = [];
  for (let page = 1; page <= 30; page++) {
    const products = await fetchPage(page);
    if (!products.length) break;
    all.push(...products);
    if (products.length < 250) break;
  }
  console.log(`fetched: ${all.length}`);

  const niche = all
    .filter(p => p.product_type && /wallcovering/i.test(p.product_type))
    .filter(p => !HARD_REJECT_TITLE.test(p.title || ''))
    .filter(p => !REJECT_TITLE.test(p.title || ''))
    .filter(p => p.images && p.images.length > 0 && p.images[0].src)
    .filter(p => vendorMatches(p.vendor))
    .filter(p => {
      const tagsBlob = (p.tags || []).join(' ');
      const title = p.title || '';
      return NICHE_TAG.test(tagsBlob) || NICHE_TITLE.test(title);
    })
    .map(p => ({
      sku: p.handle,
      handle: p.handle,
      title: p.title,
      vendor: (p.vendor || '').trim(),
      product_type: p.product_type,
      image_url: p.images[0].src,
      tags: p.tags || [],
      aesthetic: classifyForSite(p.tags, p.title),
      product_url: `https://designerwallcoverings.com/products/${p.handle}`,
    }));

  console.log(`niche-filtered: ${niche.length}`);
  const byA = {};
  for (const p of niche) byA[p.aesthetic] = (byA[p.aesthetic] || 0) + 1;
  console.log('aesthetics:', byA);
  const byV = {};
  for (const p of niche) byV[p.vendor] = (byV[p.vendor] || 0) + 1;
  console.log('vendors:', Object.entries(byV).sort((a,b)=>b[1]-a[1]));

  const tmp = path.join(__dirname, '..', 'data', 'products.json.tmp');
  const final = path.join(__dirname, '..', 'data', 'products.json');
  fs.writeFileSync(tmp, JSON.stringify(niche, null, 2));
  fs.renameSync(tmp, final);
  console.log('wrote data/products.json');
})();