← back to Dw Marketing Reels
data/fetch-innovations-recent.mjs
20 lines
import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os';
const env = fs.readFileSync(path.join(os.homedir(),'Projects/secrets-manager/.env'),'utf8');
const TOKEN=(env.match(/^SHOPIFY_ADMIN_TOKEN=(.+)$/m)||[])[1]?.replace(/"/g,'').trim();
const API='https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-10/graphql.json';
async function q(vendor){
const query=`{ products(first:50, sortKey:CREATED_AT, reverse:true, query:"vendor:'${vendor}' status:active"){ edges{ node{ title handle createdAt onlineStoreUrl images(first:10){edges{node{url width height}}} } } } }`;
const r=await fetch(API,{method:'POST',headers:{'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'},body:JSON.stringify({query})});
return (await r.json())?.data?.products?.edges||[];
}
let vendor='Innovations USA'; let edges=await q(vendor);
if(!edges.length){ vendor='Innovations'; edges=await q(vendor); }
const rows=edges.map(e=>{const n=e.node;const imgs=n.images.edges.map(i=>i.node);if(!imgs.length)return null;
const room=imgs.length>1?imgs.slice(1).reduce((b,im)=>(im.width*im.height>b.width*b.height?im:b)):null;
const title=n.title.replace(/ Wallcovering \| .*$/,'').replace(/ \| .*$/,'').replace(/ Wallcovering$/,'').trim();
return {title,handle:n.handle,created_at:n.createdAt,url:n.onlineStoreUrl||`https://www.designerwallcoverings.com/products/${n.handle}`,
image:imgs[0].url,room:(room||imgs[0]).url,has_room:!!room,chapter:'all'};}).filter(Boolean);
fs.writeFileSync('data/innovations-usa.json',JSON.stringify(rows,null,2));
console.log(`vendor="${vendor}" wrote ${rows.length} products (${rows.filter(r=>r.has_room).length} real rooms)`);
console.log('newest 3:',rows.slice(0,3).map(r=>r.title+' ['+r.created_at.slice(0,10)+']').join(' | '));