← back to Tk10640 Redirect Prune
hijack-probe.mjs
21 lines
// TK-10651 — hunt live-product HIJACKS: redirects whose SOURCE /products/<h> is a
// currently ACTIVE+published product (redirect steers users AWAY from a live product).
import { readFileSync, writeFileSync } from 'node:fs';
const txt=readFileSync(`${process.env.HOME}/Projects/secrets-manager/.env`,'utf8');
const env={}; for(const l of txt.split('\n')){const m=l.match(/^([A-Z0-9_]+)=(.*)$/); if(m) env[m[1]]=m[2].replace(/^["']|["']$/g,'');}
const STORE=env.SHOPIFY_STORE_DOMAIN, TOKEN=env.SHOPIFY_FULL_ACCESS_TOKEN, API='2024-10';
const sleep=ms=>new Promise(r=>setTimeout(r,ms));
async function gql(q,v={}){for(let a=0;a<10;a++){try{const res=await fetch(`https://${STORE}/admin/api/${API}/graphql.json`,{method:'POST',headers:{'Content-Type':'application/json','X-Shopify-Access-Token':TOKEN},body:JSON.stringify({query:q,variables:v})});const tx=await res.text();let j;try{j=JSON.parse(tx);}catch{await sleep(1500*(a+1));continue;}if(j.errors){if(/THROTTLED/i.test(JSON.stringify(j.errors))){await sleep(1500*(a+1));continue;}throw new Error(JSON.stringify(j.errors));}return j.data;}catch(e){if(a<9){await sleep(1500*(a+1));continue;}throw e;}}}
const rows=readFileSync('data/product-source-nonhome.jsonl','utf8').trim().split('\n').map(JSON.parse);
let A=0,AR=0,DR=0,DE=0; const hijacks=[];
for(let i=0;i<rows.length;i++){ const r=rows[i]; const h=r.path.replace(/^\/products\//,'').split('?')[0].split('#')[0];
const d=await gql(`query($h:String!){productByHandle(handle:$h){status onlineStoreUrl}}`,{h});
const p=d.productByHandle; const st=p?p.status:'DEAD';
if(st==='ACTIVE'&&p.onlineStoreUrl){ A++; hijacks.push({path:r.path,target:r.target,url:p.onlineStoreUrl}); }
else if(st==='ACTIVE')A++; else if(st==='ARCHIVED')AR++;else if(st==='DRAFT')DR++;else DE++;
if(i%250===249) process.stderr.write(` probed ${i+1}/${rows.length}: HIJACK(active+pub)=${hijacks.length}\n`);
}
writeFileSync('data/hijacks.json', JSON.stringify(hijacks,null,2));
console.log(JSON.stringify({probed:rows.length,active_total:A,ARCHIVED:AR,DRAFT:DR,DEAD:DE,
live_hijacks_active_published:hijacks.length, sample:hijacks.slice(0,10)},null,2));