← back to George Gmail

redfin-count.js

25 lines

const fs=require("fs");const {google}=require("googleapis");
const env={};fs.readFileSync("/root/Projects/Designer-Wallcoverings/DW-MCP/.env","utf8").split("\n").forEach(l=>{const m=l.match(/^([^#=]+)=(.+)/);if(m)env[m[1].trim()]=m[2].trim()});
const accts=[
  {k:"steve-office",l:"steve@designerwallcoverings.com",r:env.GMAIL_REFRESH_TOKEN},
  {k:"info",l:"info@designerwallcoverings.com",r:env.INFO_REFRESH_TOKEN},
  {k:"steve-personal",l:"steveabramsdesigns@gmail.com",r:env.PERSONAL_REFRESH_TOKEN},
];
(async()=>{
  for(const a of accts){
    const oc=new google.auth.OAuth2(env.GMAIL_CLIENT_ID,env.GMAIL_CLIENT_SECRET,"http://localhost:9850/oauth2callback");
    oc.setCredentials({refresh_token:a.r});
    const gm=google.gmail({version:"v1",auth:oc});
    let count=0,pages=0,pageToken=null;
    const start=Date.now();
    do{
      const r=await gm.users.messages.list({userId:"me",q:"redfin",maxResults:500,pageToken:pageToken||undefined});
      count+=(r.data.messages||[]).length;
      pageToken=r.data.nextPageToken||null;
      pages++;
      if(pages%20===0) process.stdout.write(`    ...${count} so far (${a.l})...\n`);
    }while(pageToken);
    console.log(`${a.l}: ${count} messages, ${pages} pages, ${((Date.now()-start)/1000).toFixed(1)}s`);
  }
})();