← back to Norma
agents/instagram-agent/monitor-reshares.js
36 lines
// Poll the 2 fabric-friday reshares for engagement; notify on first likes/comments.
const accounts=require('./accounts');
const { execFileSync }=require('child_process');
const a=accounts.resolve('fabric_fridays');
const {access_token:t,graph_host:h,graph_version:v}=a;
const POSTS=[{id:'18054497498547838',who:'Designtex'},{id:'17881914915683823',who:'Brunschwig & Fils'}];
const notify=(msg)=>{ try{ execFileSync('osascript',['-e',`display notification ${JSON.stringify(msg)} with title "@fabric_fridays 🧵" sound name "Glass"`]); }catch(_){} };
const base={};
const g=async(u)=>(await fetch(u)).json();
const CHECKS=16, EVERY=900000; // ~4h at 15-min intervals
(async()=>{
console.log('monitoring 2 reshares for engagement (15-min polls, ~4h)...');
for(let i=0;i<CHECKS;i++){
for(const p of POSTS){
const m=await g(`${h}/${v}/${p.id}?fields=like_count,comments_count&access_token=${encodeURIComponent(t)}`);
const key=p.id; const prev=base[key]||{like_count:0,comments_count:0};
const nl=m.like_count||0, nc=m.comments_count||0;
if(nl>prev.like_count || nc>prev.comments_count){
let newComments='';
if(nc>prev.comments_count){
const cj=await g(`${h}/${v}/${p.id}/comments?fields=text,username&access_token=${encodeURIComponent(t)}`);
newComments=(cj.data||[]).slice(-(nc-prev.comments_count)).map(c=>`@${c.username}: ${c.text}`).join(' | ');
}
const msg=`${p.who} reshare: ❤${nl} 💬${nc}${newComments?' — '+newComments:''}`;
console.log(new Date().toISOString(), msg);
notify(msg);
}
base[key]={like_count:nl,comments_count:nc};
}
if(i<CHECKS-1) await new Promise(r=>setTimeout(r,EVERY));
}
const summary=POSTS.map(p=>`${p.who}: ❤${base[p.id].like_count} 💬${base[p.id].comments_count}`).join(' · ');
console.log('MONITOR DONE —', summary);
notify('Reshare watch ended — '+summary);
})();