← back to Designerwallcoverings
scripts/ai-preamble-leak-sweep/body-start-classifier.mjs
24 lines
import fs from 'fs';
const ENV=fs.readFileSync('/Users/stevestudio2/Projects/secrets-manager/.env','utf8');
const TOK=((ENV.match(/^SHOPIFY_ADMIN_TOKEN=(.+)$/m)||[])[1]||'').replace(/['"\r]/g,'').trim();
const GQL='https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-10/graphql.json';
const sleep=ms=>new Promise(r=>setTimeout(r,ms));
async function gql(q,v){for(let a=0;a<6;a++){const r=await fetch(GQL,{method:'POST',headers:{'X-Shopify-Access-Token':TOK,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})});if(r.status===429||r.status>=500){await sleep(1500*(a+1));continue;}return r.json();}return null;}
const PJ=/phillip[- ]?jeffries/i;
const strip=h=>(h||'').replace(/<[^>]+>/g,' ').replace(/\s+/g,' ').trim();
// genuine-preamble = "here is/here's" appears at the START of the body or right before a colon/description word
const GENUINE=/^(?:okay,?\s*|sure,?\s*|certainly,?\s*)?here(?:'s| is)\b.{0,40}(?:description|:|product|below|revised|seo|following)/i;
const HEREIS=/here(?:'s| is)\b/i;
const Q=`query($cursor:String){ products(first:60, after:$cursor, query:"status:active"){ pageInfo{hasNextPage endCursor} edges{node{ title vendor descriptionHtml }} } }`;
let cursor=null,scanned=0,genuineStart=0; const ex=[];
while(true){ const j=await gql(Q,{cursor}); if(!j?.data)break; const c=j.data.products;
for(const e of c.edges){ const n=e.node; if(PJ.test(n.vendor||''))continue; scanned++; const b=strip(n.descriptionHtml); if(!b||!HEREIS.test(b))continue;
const head=b.slice(0,80);
// classify: does the body START with a preamble, or does "here is" appear mid-copy?
const startsPreamble = GENUINE.test(b.slice(0,120)) || /^(okay|sure|certainly)[,!: ]/i.test(b) || /^here(?:'s| is)\b/i.test(b);
if(startsPreamble){ genuineStart++; if(ex.length<25) ex.push({t:(n.title||'').slice(0,38),vendor:n.vendor,head:head}); }
}
cursor=c.pageInfo.endCursor; if(!c.pageInfo.hasNextPage)break; const co=j.extensions?.cost?.throttleStatus; if(co&&co.currentlyAvailable<500)await sleep(1500); else await sleep(200); }
console.log(`scanned ${scanned}; GENUINE preamble (body STARTS with here-is/okay/sure/certainly or here-is+description): ${genuineStart}`);
ex.forEach(x=>console.log(` "${x.t}" [${x.vendor}] => "${x.head}"`));