← back to Designerwallcoverings

scripts/brands-page/fetch-bgs.cjs

20 lines

const fs=require('fs'),cp=require('child_process');
const PG='postgres://stevestudio2@/dw_unified?host=/tmp';
const room=JSON.parse(fs.readFileSync('/tmp/room-final.json','utf8'));
const need=process.argv.slice(2);
function q(sql){try{return cp.execSync(`psql "${PG}" -tAc ${JSON.stringify(sql)}`,{encoding:'utf8'}).trim();}catch(e){return '';}}
const got={},miss=[];
for(const title of need){
  if(room[title]){continue;}
  const esc=title.replace(/'/g,"''");
  let url=q(`SELECT image_url FROM shopify_products WHERE vendor='${esc}' AND status='ACTIVE' AND image_url<>'' AND image_url NOT ILIKE '%no-image%' ORDER BY length(image_url) DESC LIMIT 1`);
  if(!url){ // fuzzy: vendor starts-with
    url=q(`SELECT image_url FROM shopify_products WHERE vendor ILIKE '${esc}%' AND status='ACTIVE' AND image_url<>'' AND image_url NOT ILIKE '%no-image%' LIMIT 1`);
  }
  if(url){got[title]=url;}else{miss.push(title);}
}
console.log('RESOLVED',Object.keys(got).length);
for(const[k,v] of Object.entries(got))console.log('  +',k,'->',v.slice(0,70));
console.log('STILL MISSING BG:',miss.join(' | ')||'(none)');
fs.writeFileSync('/tmp/bg-fetched.json',JSON.stringify(got,null,1));