ilml-plugin-linkedin 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +369 -0
- package/dist/applyQueue.mjs +27 -0
- package/dist/daily.mjs +22 -0
- package/dist/funnelRunner.mjs +38 -0
- package/dist/messageBot.mjs +294 -0
- package/dist/run.mjs +50 -0
- package/dist/scout.mjs +235 -0
- package/dist/src/saveLoginState.mjs +3 -0
- package/dist/syncAll.mjs +279 -0
- package/dist/viewers.mjs +238 -0
- package/dist/visit.mjs +234 -0
- package/ilml-plugin.json +78 -0
- package/package.json +92 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
var Rt=Object.defineProperty;var Je=(e,t)=>()=>(e&&(t=e(e=0)),t);var Ve=(e,t)=>{for(var o in t)Rt(e,o,{get:t[o],enumerable:!0})};var dt={};Ve(dt,{addEncounter:()=>pe,canSendConnection:()=>Se,commitDataSnapshot:()=>ut,default:()=>to,getAllApplications:()=>lt,getPersonStatus:()=>me,getQuestionStats:()=>at,getQuotaStatus:()=>ee,getRecentConnections:()=>ye,linkPersonToJob:()=>ot,loadApplicationQuestions:()=>Me,loadConversations:()=>Ze,loadJobs:()=>de,loadPeople:()=>P,loadQuota:()=>he,loadSyncState:()=>st,logApplicationQuestions:()=>ct,migrateFromDailyFiles:()=>nt,recordConnectionSent:()=>we,resetCaches:()=>Kt,saveConversations:()=>et,saveJobs:()=>fe,savePeople:()=>J,saveQuota:()=>Oe,saveSyncState:()=>rt,updateConnectionStatus:()=>ge,upsertJob:()=>tt,upsertPerson:()=>Ae});import"dotenv/config";import*as C from"fs";import*as $ from"path";import{fileURLToPath as Gt}from"url";import{execSync as xe}from"child_process";function Kt(){H=null,z=null,F=null,U=null,G=null,N=null}function _(){C.existsSync(D)||C.mkdirSync(D,{recursive:!0})}function B(e,t){let o=JSON.stringify(t,null,2),r=e+".tmp";C.writeFileSync(r,o);for(let s=0;s<3;s++)try{C.renameSync(r,e);return}catch(i){if(s<2&&(i.code==="EPERM"||i.code==="EBUSY")){let l=Date.now();for(;Date.now()-l<200;);continue}throw i}}function Z(e,t){if(!C.existsSync(e))return null;try{let o=JSON.parse(C.readFileSync(e,"utf-8"));try{C.copyFileSync(e,e+".backup")}catch{}return o}catch(o){console.error(`[!] ${t} is corrupted: ${o.message}`),console.error(` File: ${$.resolve(e)}`);let r=e+".backup";if(C.existsSync(r))try{let s=JSON.parse(C.readFileSync(r,"utf-8"));console.error(` Restored from ${r}`);try{C.copyFileSync(r,e)}catch{}return s}catch{console.error(" Backup also corrupted!")}console.error(` Starting with empty data. Old file preserved as ${e}.corrupted`);try{C.copyFileSync(e,e+".corrupted")}catch{}return null}}function Q(){return new Date().toISOString().split("T")[0]}function P(){return H||(_(),H=Z(De,"people.json")||{},H)}function J(e){_(),H=e;try{B(De,e)}catch(t){console.error(`[!] Failed to save people.json: ${t.message}`),console.error(` Path: ${$.resolve(De)}`)}}function de(){return z||(_(),z=Z(_e,"jobs.json")||{},z)}function fe(e){_(),z=e;try{B(_e,e)}catch(t){console.error(`[!] Failed to save jobs.json: ${t.message}`),console.error(` Path: ${$.resolve(_e)}`)}}function Ze(){return U||(_(),U=Z(ue,"conversations.json")||{},Xt(U),U)}function Xt(e){let t=0;for(let o of Object.values(e)){if(!(o.draftStatus!==void 0||o.draftPreparedAt!==void 0))continue;let s=(o.messages||[]).find(i=>i.status==="draft");s&&o.draftStatus&&o.draftStatus!=="pending"&&(s.status=o.draftStatus),delete o.draftStatus,delete o.draftPreparedAt,t++}if(t>0){console.log(` [migration] Moved draft state from conv to msg.status for ${t} conversations`);try{B(ue,e)}catch{}}}function et(e){_(),U=e;try{B(ue,e)}catch(t){console.error(`[!] Failed to save conversations.json: ${t.message}`),console.error(` Path: ${$.resolve(ue)}`)}}function Ae(e,t={}){if(!e)return null;let o=P(),r=Q(),s=new Date().toISOString(),i=o[e];if(i){i.seenCount=(i.seenCount||1)+1,i.lastSeen=r,i.lastUpdated=s,t.source&&!i.sources?.includes(t.source)&&(i.sources=i.sources||[],i.sources.push(t.source));for(let l of Ee)t[l]!=null&&(i[l]=t[l])}else o[e]={name:t.name||null,title:t.title||null,location:t.location||null,company:t.company||null,connectionDegree:t.connectionDegree||null,connectionsCount:t.connectionsCount||null,mutualConnections:t.mutualConnections||null,photoUrl:t.photoUrl||null,hasVerifiedBadge:t.hasVerifiedBadge||!1,firstSeen:r,lastSeen:r,seenCount:1,sources:t.source?[t.source]:[],connectionStatus:null,connectionDate:null,connectionMethod:null,messageSent:null,jobIds:[],lastUpdated:s};return J(o),o[e]}function tt(e,t={}){if(!e)return null;let o=de(),r=Q(),s=o[e];if(s){let i=["title","company","companyUrl","location","salary","url","applied","appliedDate","recruiterUrl","description","techStack","jobTags","seniority","employmentType","industries","jobFunction","postedTime","applicantCount","companySize","workplaceType","contractType","isEasyApply","applyType","scoutedAt","lastSeen"];for(let l of i)t[l]!=null&&(s[l]=t[l])}else o[e]={title:t.title||null,company:t.company||null,location:t.location||null,salary:t.salary||null,url:t.url||null,applied:t.applied||!1,appliedDate:t.appliedDate||r,recruiterUrl:t.recruiterUrl||null,firstSeen:r};return fe(o),o[e]}function ot(e,t){if(!e||!t)return;let o=P(),r=o[e];r&&(r.jobIds||(r.jobIds=[]),r.jobIds.includes(t)||(r.jobIds.push(t),r.lastUpdated=new Date().toISOString(),J(o)))}function pe(e,t){if(!e||!t)return;let o=P(),r=o[e];if(!r)return;r.encounters||(r.encounters=[]);let i={date:Q(),source:t.source||"unknown",action:t.action||"unknown"};t.jobId&&(i.jobId=String(t.jobId)),t.jobTitle&&(i.jobTitle=t.jobTitle),t.jobCompany&&(i.jobCompany=t.jobCompany),!r.encounters.some(u=>u.date===i.date&&u.source===i.source&&u.action===i.action&&(u.jobId||"")===(i.jobId||""))&&(r.encounters.push(i),r.lastUpdated=new Date().toISOString(),J(o))}function me(e){return e&&P()[e]?.connectionStatus||null}function ge(e,t,o={}){if(!e)return;let r=P(),s=r[e];if(s||(Ae(e,o.profileInfo||{}),s=r[e]),!!s){if(s.connectionStatus=t,s.connectionDate=Q(),s.connectionMethod=o.method||s.connectionMethod,s.messageSent=o.messageSent||s.messageSent,s.lastUpdated=new Date().toISOString(),o.profileInfo)for(let i of Ee)o.profileInfo[i]!=null&&(s[i]=o.profileInfo[i]);J(r)}}function he(){return F||(_(),F=Z(Ke,"quota.json"),(!F||!F.connections)&&(F={connections:[]}),F)}function Oe(e){_(),F=e;try{B(Ke,e)}catch(t){console.error(`[!] Failed to save quota.json: ${t.message}`)}}function we(e,t="unknown"){let o=he(),r=new Date;o.connections.push({date:r.toISOString().split("T")[0],time:r.toTimeString().split(" ")[0],profileUrl:e,source:t}),Oe(o)}function ye(){let e=he(),t=new Date;t.setDate(t.getDate()-7);let o=t.toISOString().split("T")[0];return e.connections.filter(r=>r.date>=o)}function Se(){let e=ye(),t=Q(),o=e.filter(n=>n.date===t).length,r=e.length,s=o>=K,i=r>=X,l=!s&&!i,u={allowed:l,daily:{sent:o,max:K},weekly:{sent:r,max:X}};return l||(u.reason=s?`Daily limit reached (${o}/${K})`:`Weekly limit reached (${r}/${X})`),u}function ee(){let e=ye(),t=Q(),o=e.filter(s=>s.date===t).length,r=e.length;return{daily:{sent:o,max:K,remaining:K-o},weekly:{sent:r,max:X,remaining:X-r}}}function Zt(e){if(!e)return null;let t=e.match(/jobs\/view\/(\d+)/);return t?t[1]:null}function nt(){_();let e=C.readdirSync(D).filter(i=>i.endsWith(".json")&&i!=="people.json"&&i!=="jobs.json"&&i!=="quota.json");if(e.length===0)return console.log("No daily files found to migrate."),{people:0,jobs:0};console.log(`
|
|
2
|
+
Migrating from ${e.length} daily files...
|
|
3
|
+
`);let t=P(),o=de(),r=0,s=0;e.sort();for(let i of e){let l=$.join(D,i),u;try{u=JSON.parse(C.readFileSync(l,"utf-8"))}catch(a){console.log(` Skipping ${i}: ${a.message}`);continue}let n=u.date||i.match(/\d{4}-\d{2}-\d{2}/)?.[0]||null,c=u.source||i.split("-").slice(0,-1).join("-").replace(/-\d{4}$/,"")||"unknown";if(!u.profiles||!Array.isArray(u.profiles)){console.log(` Skipping ${i}: no profiles array`);continue}console.log(` ${i}: ${u.profiles.length} profiles`);for(let a of u.profiles){let f=a.profileUrl;if(!f)continue;let d=t[f];if(d){d.seenCount=(d.seenCount||1)+1,n&&(!d.lastSeen||n>d.lastSeen)&&(d.lastSeen=n),n&&(!d.firstSeen||n<d.firstSeen)&&(d.firstSeen=n),c&&!d.sources?.includes(c)&&(d.sources=d.sources||[],d.sources.push(c));for(let p of Ee)a[p]!=null&&(d[p]=a[p]);a.status&&a.processed&&(d.connectionStatus=a.status,d.connectionDate=n,a.funnelResult&&(d.connectionMethod=a.funnelResult.method||d.connectionMethod,d.messageSent=a.funnelResult.message||d.messageSent)),d.lastUpdated=new Date().toISOString()}else t[f]={name:a.name||null,title:a.title||null,location:a.location||null,company:a.company||null,connectionDegree:a.connectionDegree||null,connectionsCount:a.connectionsCount||null,mutualConnections:a.mutualConnections||null,photoUrl:a.photoUrl||null,hasVerifiedBadge:a.hasVerifiedBadge||!1,firstSeen:n,lastSeen:n,seenCount:1,sources:c?[c]:[],connectionStatus:a.processed&&a.status||null,connectionDate:a.processed?n:null,connectionMethod:a.funnelResult?.method||null,messageSent:a.funnelResult?.message||null,jobIds:[],lastUpdated:new Date().toISOString()},r++;if(a.fromJob?.url){let p=Zt(a.fromJob.url);p&&(t[f].jobIds||(t[f].jobIds=[]),t[f].jobIds.includes(p)||t[f].jobIds.push(p),o[p]?o[p].recruiterUrl||(o[p].recruiterUrl=f):(o[p]={title:a.fromJob.title||null,company:a.fromJob.company||null,location:null,salary:null,url:a.fromJob.url,applied:!0,appliedDate:n,recruiterUrl:f,firstSeen:n},s++))}}}return J(t),fe(o),console.log(`
|
|
4
|
+
Migration complete!`),console.log(` People: ${Object.keys(t).length} total (${r} new)`),console.log(` Jobs: ${Object.keys(o).length} total (${s} new)`),{people:Object.keys(t).length,jobs:Object.keys(o).length}}function st(){return G||(_(),G=Z(Xe,"sync-state.json")||{},G)}function rt(e){_(),G=e;try{B(Xe,e)}catch(t){console.error(`[!] Failed to save sync-state.json: ${t.message}`)}}function Me(){if(N)return N;_();try{C.existsSync(je)&&(N=JSON.parse(C.readFileSync(je,"utf-8")))}catch{}return N||(N={}),N}function it(e){return Array.isArray(e.applications)}function lt(){let e=Me();if(it(e))return e.applications;let t=[];for(let o of Object.values(e))o.applications&&t.push(...o.applications);return t}function ct(e){if(!e||!e.questions||e.questions.length===0)return;let t=Me(),o=e.jobId||"unknown",s={applicationId:`${o}_${Date.now()}`,date:new Date().toISOString(),result:e.result||"submitted",formPages:e.formPages||null,duration:e.duration||null,retries:e.retries||0,failedField:e.failedField||null,questionCount:e.questions.length,questions:e.questions.map(i=>{let l={type:i.type||"input",question:i.labelText||i.question||"",answer:i.answer||""};return i.options&&(i.options.length<=eo?l.options=i.options:l.optionsOmitted=!0),i.page&&(l.page=i.page),i.wasPreFilled&&(l.wasPreFilled=!0),i.wasRetry&&(l.wasRetry=!0),l})};it(t)?(s.jobId=o,s.jobTitle=e.jobTitle||null,s.jobCompany=e.jobCompany||null,s.jobUrl=e.jobUrl||null,t.applications.push(s)):(t[o]||(t[o]={applications:[]}),t[o].applications.push(s));try{B(je,t)}catch(i){console.error(`[!] Failed to save application-questions.json: ${i.message}`)}}function at(){let e=lt(),t=new Map;for(let i of e)for(let l of i.questions||[]){let u=l.question.toLowerCase().trim();if(!u)continue;t.has(u)||t.set(u,{question:l.question,count:0,answers:new Map});let n=t.get(u);n.count++;let c=(l.answer||"").trim();n.answers.set(c,(n.answers.get(c)||0)+1)}let o=[...t.values()].sort((i,l)=>l.count-i.count),r=o.slice(0,15).map(i=>({question:i.question,count:i.count,topAnswer:[...i.answers.entries()].sort((l,u)=>u[1]-l[1])[0]?.[0]||""})),s=o.filter(i=>i.answers.size>1&&i.count>=3).map(i=>({question:i.question,count:i.count,answers:[...i.answers.entries()].sort((l,u)=>u[1]-l[1]).map(([l,u])=>`${l} (${u}x)`)}));return{totalApplications:e.length,uniqueQuestions:t.size,topQuestions:r,inconsistent:s}}function ut(e){if(C.existsSync($.join(D,".git")))try{let t={cwd:D,stdio:"pipe",timeout:1e4};xe("git add conversations.json people.json jobs.json quota.json sync-state.json run-log.json application-questions.json",t);try{xe("git diff --cached --quiet",t);return}catch{}let o=(e||`sync ${new Date().toISOString().slice(0,16)}`).replace(/"/g,'\\"');xe(`git commit -m "${o}"`,t)}catch(t){process.env.VERBOSE&&console.log(` [git] snapshot skipped: ${t.message?.slice(0,80)}`)}}var Qt,Yt,D,De,_e,Ke,ue,Xe,je,K,X,H,z,F,U,G,N,Ee,eo,to,Fe=Je(()=>{Qt=Gt(import.meta.url),Yt=$.dirname($.dirname(Qt)),D=process.env.DATA_DIR?$.resolve(process.env.DATA_DIR,"collected-profiles"):$.join(Yt,"collected-profiles"),De=$.join(D,"people.json"),_e=$.join(D,"jobs.json"),Ke=$.join(D,"quota.json"),ue=$.join(D,"conversations.json"),Xe=$.join(D,"sync-state.json"),je=$.join(D,"application-questions.json"),K=20,X=100,H=null,z=null,F=null,U=null,G=null,N=null;Ee=["name","title","location","company","connectionDegree","connectionsCount","mutualConnections","photoUrl","hasVerifiedBadge"];eo=50;to={loadPeople:P,savePeople:J,loadJobs:de,saveJobs:fe,loadConversations:Ze,saveConversations:et,upsertPerson:Ae,upsertJob:tt,linkPersonToJob:ot,addEncounter:pe,getPersonStatus:me,updateConnectionStatus:ge,migrateFromDailyFiles:nt,loadQuota:he,saveQuota:Oe,recordConnectionSent:we,canSendConnection:Se,getRecentConnections:ye,getQuotaStatus:ee,loadSyncState:st,saveSyncState:rt,commitDataSnapshot:ut,logApplicationQuestions:ct,getQuestionStats:at}});var xt={};Ve(xt,{getJobDecision:()=>yo,getLastVisit:()=>Co,getScoutStats:()=>$o,loadCompanies:()=>It,loadDiscoveredPeople:()=>qe,loadJobDecisions:()=>Re,loadScoutedJobs:()=>Pe,loadVisitLog:()=>be,recordVisit:()=>vo,resetCaches:()=>go,saveCompany:()=>bo,saveDiscoveredPerson:()=>So,saveScoutedJob:()=>ho,setJobDecision:()=>wo});import"dotenv/config";import*as O from"fs";import*as j from"path";import{fileURLToPath as fo}from"url";function le(){O.existsSync(W)||O.mkdirSync(W,{recursive:!0})}function ce(e,t){let o=JSON.stringify(t,null,2),r=e+".tmp";O.writeFileSync(r,o);for(let s=0;s<3;s++)try{O.renameSync(r,e);return}catch(i){if(s<2&&(i.code==="EPERM"||i.code==="EBUSY")){let l=Date.now();for(;Date.now()-l<200;);continue}throw i}}function ae(e){try{if(O.existsSync(e))return JSON.parse(O.readFileSync(e,"utf-8"))}catch{}return null}function go(){ne=null,se=null,re=null,ie=null,V=null}function Pe(){return ne||(ne=ae(bt)||{},ne)}function ho(e,t){if(!e)return;le();let o=Pe(),r=new Date().toISOString().slice(0,10);if(o[e]){for(let[s,i]of Object.entries(t))i!=null&&(o[e][s]=i);o[e].lastSeen=r}else o[e]={...t,scoutedAt:r,lastSeen:r};try{ce(bt,o)}catch(s){console.error(`[!] Failed to save scouted-jobs.json: ${s.message}`)}}function Re(){return se||(se=ae(kt)||{},se)}function wo(e,t,o={}){if(!e||!t)return;le();let r=Re(),s=new Date().toISOString().slice(0,10);r[e]?(r[e].status=t,r[e].updatedAt=s,o.reason&&(r[e].reason=o.reason),o.score!=null&&(r[e].score=o.score),o.source&&(r[e].source=o.source)):r[e]={status:t,decidedAt:s,updatedAt:s,reason:o.reason||null,score:o.score??null,source:o.source||null};try{ce(kt,r)}catch(i){console.error(`[!] Failed to save job-decisions.json: ${i.message}`)}}function yo(e){return Re()[e]||null}function qe(){return re||(re=ae(vt)||{},re)}function So(e,t){if(!e)return;le();let o=qe(),r=new Date().toISOString().slice(0,10);if(o[e]){let s=o[e];t.name&&(s.name=t.name),t.title&&(s.title=t.title),t.company&&(s.company=t.company),t.location&&(s.location=t.location),t.role&&(s.role=t.role),t.isConnected!=null&&(s.isConnected=t.isConnected),t.jobId&&(s.jobIds||(s.jobIds=[]),s.jobIds.includes(t.jobId)||s.jobIds.push(t.jobId)),s.lastSeen=r,s.seenCount=(s.seenCount||1)+1}else o[e]={name:t.name||null,title:t.title||null,company:t.company||null,location:t.location||null,role:t.role||null,source:t.source||"scout",isConnected:t.isConnected||!1,discoveredAt:r,lastSeen:r,seenCount:1,jobIds:t.jobId?[t.jobId]:[]};try{ce(vt,o)}catch(s){console.error(`[!] Failed to save discovered-people.json: ${s.message}`)}}function It(){return ie||(ie=ae($t)||{},ie)}function bo(e,t){if(!e)return;le();let o=It(),r=new Date().toISOString().slice(0,10);if(o[e]){let s=o[e];t.name&&(s.name=t.name),t.about&&(s.about=t.about),t.size&&(s.size=t.size),t.industries&&(s.industries=t.industries),t.jobId&&(s.jobIds||(s.jobIds=[]),s.jobIds.includes(t.jobId)||s.jobIds.push(t.jobId)),t.recruiterUrl&&(s.recruiterUrls||(s.recruiterUrls=[]),s.recruiterUrls.includes(t.recruiterUrl)||s.recruiterUrls.push(t.recruiterUrl)),s.lastSeen=r,s.jobCount=s.jobIds?.length||0}else o[e]={name:t.name||null,about:t.about||null,size:t.size||null,industries:t.industries||null,discoveredAt:r,lastSeen:r,jobIds:t.jobId?[t.jobId]:[],recruiterUrls:t.recruiterUrl?[t.recruiterUrl]:[],jobCount:t.jobId?1:0};try{ce($t,o)}catch(s){console.error(`[!] Failed to save companies.json: ${s.message}`)}}function be(){return V||(V=ae(Ct)||{visits:[]},Array.isArray(V.visits)||(V.visits=[]),V)}function vo(e,t={}){if(!e)return;le();let o=be();o.visits.push({date:new Date().toISOString(),profileUrl:e,name:t.name||null,source:t.source||"manual"});let r=Date.now()-90*24*60*60*1e3;o.visits=o.visits.filter(s=>new Date(s.date).getTime()>r);try{ce(Ct,o)}catch(s){console.error(`[!] Failed to save visit-log.json: ${s.message}`)}}function Co(e){let t=be();for(let o=t.visits.length-1;o>=0;o--)if(t.visits[o].profileUrl===e)return t.visits[o].date;return null}function $o(){let e=Pe(),t=qe(),o=Object.values(e),r=Object.values(t),s={};for(let d of o)d.company&&(s[d.company]=(s[d.company]||0)+1);let i=Object.entries(s).sort((d,p)=>p[1]-d[1]).slice(0,5).map(([d,p])=>`${d} (${p})`),l={};for(let d of o)for(let p of d.techStack||[])l[p]=(l[p]||0)+1;let u=Object.entries(l).sort((d,p)=>p[1]-d[1]).slice(0,8).map(([d,p])=>`${d} (${p})`),n=be(),c=new Date().toISOString().slice(0,10),a=n.visits.filter(d=>d.date.startsWith(c)).length,f=new Set(n.visits.map(d=>d.profileUrl)).size;return{totalJobs:o.length,totalRecruiters:r.length,topCompanies:i,topTech:u,totalVisited:f,todayVisited:a,lastScoutDate:o.length>0?o.sort((d,p)=>(p.scoutedAt||"").localeCompare(d.scoutedAt||""))[0]?.scoutedAt:null}}var po,mo,W,bt,vt,Ct,$t,kt,ne,se,re,ie,V,Dt=Je(()=>{po=fo(import.meta.url),mo=j.dirname(j.dirname(po)),W=process.env.DATA_DIR?j.resolve(process.env.DATA_DIR,"market-research"):j.join(mo,"market-research"),bt=j.join(W,"scouted-jobs.json"),vt=j.join(W,"discovered-people.json"),Ct=j.join(W,"visit-log.json"),$t=j.join(W,"companies.json"),kt=j.join(W,"job-decisions.json");ne=null;se=null;re=null;ie=null;V=null});import jo from"puppeteer";import*as Mt from"fs";import"dotenv/config";import*as A from"fs";import*as L from"path";import{fileURLToPath as Vt}from"url";import"dotenv/config";import{createGraphClient as qt,resolveToken as Nt}from"@ilivemylife/graph-sdk";import{config as Ut}from"dotenv";Ut();var We=Nt()||process.env.TOKEN;We||(console.error(`
|
|
5
|
+
[FATAL] No iLiveMyLife token found.`),console.error(" The bot uses Lifebot AI to decide which jobs to apply for"),console.error(` and how to fill application forms. It cannot work without it.
|
|
6
|
+
`),console.error(" Easiest way:"),console.error(" 1. npm install -g @ilivemylife/graph-sdk"),console.error(` 2. ilml login your@email.com yourpassword
|
|
7
|
+
`),console.error(` Or add ILML_TOKEN=... to .env
|
|
8
|
+
`),process.exit(1));var He=qt({token:We});function Bt(e){if(!e||e<1e3)return`${e||0}ms`;let t=Math.round(e/1e3);if(t<60)return`${t}s`;let o=Math.floor(t/60),r=t%60;return r?`${o}m ${r}s`:`${o}m`}function Jt({scriptName:e,mode:t,status:o,error:r,duration:s}){let i=o==="error"?"\u2717":o==="quota-reached"?"\u26A0":"\u2713",l=new Date().toISOString().slice(0,16).replace("T"," "),u=s?` ${Bt(s)}`:"",n=o==="error"?`error${r?`: ${String(r).slice(0,80)}`:""}`:o==="quota-reached"?"daily quota reached":"done",c=t&&t!=="default"?` (${t})`:"";return`${i} ${e}${c} \u2014 ${n} \u2014 ${l}${u}`}async function ze({scriptName:e,mode:t,status:o,summaryLines:r,error:s,duration:i}){let l=process.env.NODE_RUN_REPORTS;if(!l||!Array.isArray(r)||r.length===0)return;let n=`**${Jt({scriptName:e,mode:t,status:o,error:s,duration:i})}**
|
|
9
|
+
|
|
10
|
+
${r.join(`
|
|
11
|
+
`)}`;try{await He.addMessage(l,n)}catch(c){console.error(`[runReport] Failed to post end-of-session message: ${c.message}`)}}var Wt=Vt(import.meta.url),Ht=L.dirname(L.dirname(Wt)),$e=process.env.DATA_DIR?L.resolve(process.env.DATA_DIR,"collected-profiles"):L.join(Ht,"collected-profiles"),ke=L.join($e,"run-log.json");function zt(e,t){let o=JSON.stringify(t,null,2),r=e+".tmp";A.writeFileSync(r,o);for(let s=0;s<3;s++)try{A.renameSync(r,e);return}catch(i){if(s<2&&(i.code==="EPERM"||i.code==="EBUSY")){let l=Date.now();for(;Date.now()-l<200;);continue}throw i}}function Ge(){try{if(!A.existsSync(ke))return{runs:[]};let e=JSON.parse(A.readFileSync(ke,"utf-8"));return!e.runs||!Array.isArray(e.runs)?{runs:[]}:e}catch{return{runs:[]}}}function Qe(e){A.existsSync($e)||A.mkdirSync($e,{recursive:!0});try{zt(ke,e)}catch(t){console.error(`[!] Failed to save run-log.json: ${t.message}`)}}function Ye(e,t="default"){let o=Ge();for(let l of o.runs)l.status==="running"&&(l.status="interrupted",l.result="interrupted",l.timestamp&&(l.duration=Date.now()-new Date(l.timestamp).getTime()));let r=new Date,s={timestamp:r.toISOString(),script:e,mode:t,status:"running",result:null,duration:null,stats:null,error:null};o.runs.push(s);let i=Date.now()-30*24*60*60*1e3;return o.runs=o.runs.filter(l=>new Date(l.timestamp).getTime()>i),Qe(o),{script:e,mode:t,startTime:r.getTime(),runIndex:o.runs.length-1}}function Ie(e,t,o=null,r=null){if(!e)return;let s=null;try{let l=Ge(),u=l.runs[e.runIndex];if(u&&u.status==="running"&&u.script===e.script)u.status=t,u.result=t,u.duration=Date.now()-e.startTime,u.stats=o,u.error=r,s=u.duration;else{let n=l.runs.find(c=>c.script===e.script&&c.status==="running"&&c.timestamp===new Date(e.startTime).toISOString());n&&(n.status=t,n.result=t,n.duration=Date.now()-e.startTime,n.stats=o,n.error=r,s=n.duration)}Qe(l)}catch(l){console.error(`[runLog] Failed to end run: ${l.message}`)}let i=o&&Array.isArray(o.summaryLines)?o.summaryLines:null;if(i&&i.length>0&&process.env.NODE_RUN_REPORTS){let l=t==="error"?"error":o&&o.dailyLimitHit?"quota-reached":"done";ze({scriptName:e.script,mode:e.mode,status:l,summaryLines:i,error:r,duration:s??Date.now()-e.startTime}).catch(u=>console.error(`[runLog] postRunReport failed: ${u.message}`))}}async function g(e){let t=Math.floor(Math.random()*2001),o=e+t;return new Promise(r=>setTimeout(r,o))}async function q(e){return await e.evaluate(()=>{let t=document.querySelectorAll("div.artdeco-modal--is-open");for(let r of t){let s=r.innerText||"",i=s.includes("Premium")||s.includes("premium")||s.includes("Upgrade")||s.includes("upgrade"),l=s.includes("Try for")||s.includes("free trial")||s.includes("/month")||s.includes("per month")||s.includes("Start my")||s.includes("Subscribe"),u=s.includes("Get hired faster")||s.includes("Get hired 2x");if(i||l||u){let n=r.querySelector('button[aria-label="Dismiss"]')||r.querySelector("button.artdeco-modal__dismiss")||r.querySelector("button[data-test-modal-close-btn]");if(n)return n.click(),{dismissed:!0,matched:s.substring(0,80)};let c=r.querySelectorAll("button");for(let a of c){let f=a.innerText?.trim().toLowerCase()||"",d=a.getAttribute("aria-label")?.toLowerCase()||"";if(f==="not now"||f==="no thanks"||f==="dismiss"||d.includes("dismiss"))return a.click(),{dismissed:!0,matched:s.substring(0,80)}}}}let o=document.querySelector(".premium-upsell-link")||document.querySelector("[data-test-premium-upsell]")||document.querySelector(".premium-hub-upsell");if(o){let r=o.closest('[class*="modal"]')?.querySelector('button[aria-label="Dismiss"]')||o.closest('[class*="banner"]')?.querySelector('button[aria-label="Close"]');if(r)return r.click(),{dismissed:!0,matched:"premium-banner"}}return!1})}Fe();async function ft(e,t){let o=e.url();if(o.includes("/in/"))return!1;if(o.includes("/login")||o.includes("/authwall")||o.includes("/checkpoint"))throw new Error("Session expired \u2014 redirected to login. Run: npm run login");console.log(` Off-track: ${o.slice(0,80)}`),console.log(" Navigating back to profile..."),await e.goto(t,{waitUntil:"domcontentloaded",timeout:6e4}),await g(3e3);let r=e.url();return r.includes("/in/")||console.log(` Recovery failed \u2014 still on: ${r.slice(0,80)}`),!0}function oo(e,t=[]){if(!e||!t||t.length===0)return!1;let o=e.toLowerCase();return t.some(r=>o.includes(r.toLowerCase()))}async function no(e,t){try{await e.goto(t,{waitUntil:"domcontentloaded",timeout:6e4}),await g(2e3+Math.random()*2e3),await q(e)&&(console.log(" Dismissed Premium overlay"),await g(1500),await ft(e,t));let r=await e.evaluate(()=>{let s=document.querySelector("h1")?.textContent?.trim()||null,i=document.querySelector("div.text-body-medium")?.textContent?.trim()||null,l=document.querySelector(".text-body-small.inline.t-black--light.break-words")?.textContent?.trim()||null,u=document.querySelector("section#about div.display-flex")?.textContent?.trim()||null,c=document.querySelector('button[aria-label^="Current company"] span.hoverable-link-text')?.textContent?.trim()||null,f=document.querySelector(".dist-value")?.textContent?.trim()||null,p=document.querySelector('a[href*="connectionOf"] span.t-bold')?.textContent?.trim()||null,y=(document.querySelector('a[href*="facetConnectionOf"] span.t-normal')?.textContent?.trim()||null)?.match(/(\d+)\s+other\s+mutual/),w=y?parseInt(y[1],10):null,b=document.querySelector("img.pv-top-card-profile-picture__image--show")?.src||null,v=!!document.querySelector(".pv-text-details__verified-badge-icon");return{name:s,title:i,location:l,about:u,company:c,connectionDegree:f,connectionsCount:p,mutualConnections:w,photoUrl:b,hasVerifiedBadge:v}});return console.log(`Viewed: ${r.name} - ${r.title}`),{...r,profileUrl:t}}catch(o){console.error(`Error viewing profile ${t}:`,o.message);try{await e.goto("about:blank",{timeout:1e4}),await g(1e3)}catch{}return null}}async function te(e){let t=await e.evaluate(()=>{let o=document.querySelectorAll('button[aria-label*="Pending"]'),r=Array.from(o).map(n=>n.getAttribute("aria-label")),s=document.querySelector('button[aria-label*="Message"]'),i=document.querySelector('button[aria-label*="Invite"][aria-label*="connect"]')||document.querySelector('button[aria-label="Connect"]'),l={pendingCount:o.length,pendingLabels:r,hasMessageBtn:!!s,hasConnectBtn:!!i};return o.length>0?{status:"pending",debug:l}:s&&!i?{status:"connected",debug:l}:document.body.innerText.includes("1st degree connection")?{status:"connected",debug:l}:{status:"not_connected",debug:l}});return console.log(" DEBUG connection status:",JSON.stringify(t.debug)),t.status}async function Te(e){let t=null,o=await e.evaluate(()=>{let n=document.querySelector("h1")?.textContent?.trim();if(n&&!n.includes("notification"))return n;let c=document.querySelector('main h2, [class*="profile"] h2, [class*="top-card"] h2');if(c&&(n=c.textContent?.trim(),n&&!n.includes("notification")))return n;let a=document.querySelector('[class*="text-heading-xlarge"]')||document.querySelector('[class*="profile-card-title"]')||document.querySelector(".pv-text-details__left-panel h1");if(a)return a.textContent?.trim()||"";let f=document.title;return f&&f.includes("|")?f.split("|")[0].trim():""});if(console.log(` Profile name: "${o}"`),o){let n=await e.evaluate(()=>{let a=document.querySelectorAll('a[href*="custom-invite"]');return{count:a.length,labels:Array.from(a).map(f=>f.getAttribute("aria-label")?.slice(0,50))}});n.count>0&&console.log(` S0: ${n.count} custom-invite links: ${JSON.stringify(n.labels)}`),t=await e.evaluateHandle(a=>{let f=document.querySelectorAll('a[href*="custom-invite"]'),d=a.toLowerCase().split(" ")[0],p=null,h=null;for(let m of f){let y=m.getBoundingClientRect();if(y.width===0||y.height===0)continue;if((m.getAttribute("aria-label")||"").toLowerCase().includes(d)){p=m;break}h||(h=m)}return p||h||null},o);let c=t?.asElement?.()||null;if(c||await t?.dispose?.().catch(()=>{}),c)return console.log(" Found: Connect link (new LinkedIn 2025)"),{button:c,method:"connect_link"}}if(o){let n=o.split(" ")[0].toLowerCase();console.log(` Looking for button with name: "${n}"`);let c=await e.evaluate(f=>{let d=new RegExp("\\b"+f.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")+"\\b","i"),p=document.querySelectorAll("button[aria-label]");return Array.from(p).filter(m=>{let y=m.getAttribute("aria-label")||"";return(y.toLowerCase().includes("invite")||y.toLowerCase().includes("connect"))&&d.test(y)}).map(m=>m.getAttribute("aria-label")?.slice(0,50))},n);c.length>0&&console.log(` S1: matched buttons: ${JSON.stringify(c)}`),t=await e.evaluateHandle(f=>{let d=document.querySelectorAll("button[aria-label]"),p=new RegExp("\\b"+f.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")+"\\b","i");for(let h of d){let m=h.getAttribute("aria-label")||"";if((m.toLowerCase().includes("invite")||m.toLowerCase().includes("connect"))&&p.test(m))return h}return null},n);let a=t?.asElement?.()||null;if(a||await t?.dispose?.().catch(()=>{}),a)return console.log(" Found: button matching profile name"),{button:a,method:"name_matched_button"}}if(t=await e.$('button.artdeco-button--primary[aria-label*="Invite"]:not([class*="sticky-header"]):not([class*="muted"])'),t&&o){let n=await e.evaluate(a=>(a.getAttribute("aria-label")||"").toLowerCase(),t),c=o.split(" ")[0].toLowerCase();n.includes("invite")&&!n.includes(c)&&(console.log(` Skipping primary button: wrong person (${n})`),t=null)}if(t||(t=await e.$('button.artdeco-button--primary[aria-label="Connect"]:not([class*="sticky-header"]):not([class*="muted"])')),t)return console.log(" Found: primary button"),{button:t,method:"primary_button"};let r=await e.$$('button[aria-label*="Invite"][aria-label*="connect"]');r.length===0&&(r=await e.$$('button[aria-label="Connect"]'));let s=o?o.split(" ")[0].toLowerCase():"",i=null;for(let n of r){let c=await e.evaluate((a,f)=>{let d=a.className.includes("muted"),p=a.className.includes("sticky-header")||a.className.includes("sticky"),h=a.closest('[class*="pymk"], [class*="people-also-viewed"], [class*="browse-map"]')!==null,m=(a.getAttribute("aria-label")||"").toLowerCase(),y=m.includes("invite"),w=f?m.includes(f):!0;return{isMuted:d,isStickyHeader:p,inRecommendations:h,hasInvite:y,matchesName:w,ariaLabel:m}},n,s);if(!c.isMuted&&!c.isStickyHeader&&!c.inRecommendations){if(c.hasInvite&&!c.matchesName){console.log(` Skipping secondary button: wrong person (${c.ariaLabel})`);continue}i=n;break}}for(let n of r)n!==i&&await n.dispose().catch(()=>{});if(i)return console.log(" Found: secondary button"),{button:i,method:"secondary_button"};if(t=await e.$('section.pv-supplementary-action button[aria-label*="Invite"]'),t||(t=await e.$('section.pv-supplementary-action button[aria-label="Connect"]')),t)return console.log(" Found: supplementary section"),{button:t,method:"supplementary_section"};console.log(" Trying More actions dropdown...");let l=null,u=await e.$$('button[id*="profile-overflow-action"]');for(let n of u){let c=await e.evaluate(a=>{let f=a.getBoundingClientRect(),d=f.width>0&&f.height>0,p=a.closest('.sticky-header, [class*="sticky"]')!==null;return{isVisible:d,isInStickyHeader:p,top:f.top}},n);if(c.isVisible&&!c.isInStickyHeader){l=n,console.log(" Found More button by ID pattern (main)");break}}for(let n of u)n!==l&&await n.dispose().catch(()=>{});if(!l&&u.length>0&&(l=u[u.length-1],console.log(" Found More button by ID pattern (last)")),l||(l=await e.$('button[aria-label="More actions"]'),l&&console.log(" Found More button by aria-label")),!l){let n=await e.evaluateHandle(()=>{let a=Array.from(document.querySelectorAll("button, a")).find(d=>{let p=d.innerText?.trim(),h=d.getAttribute("aria-label")||"",m=d.getBoundingClientRect();return m.top<100||m.top>600||m.width===0?!1:p==="Follow"||p==="Message"||p==="Connect"||h.includes("Follow")||h.includes("Message")||h.includes("Invite")});if(!a)return null;let f=a.parentElement;for(let d=0;d<8&&f;d++){for(let p of f.querySelectorAll("button"))if(p.innerText?.trim()==="More"&&p!==a)return p;f=f.parentElement}return null});l=n?.asElement?.()||null,l||await n?.dispose?.().catch(()=>{}),l&&console.log(" Found More button by text (near profile actions)")}if(l)try{if(await e.evaluate(a=>a.scrollIntoView({behavior:"smooth",block:"center"}),l),await g(500),await e.evaluate(a=>a.click(),l),await g(2e3),!e.url().includes("/in/"))return console.log(" Redirect detected after More click, aborting"),null;let c=await e.$('.artdeco-dropdown__item[aria-label*="Invite"]');if(c||(c=await e.$('.artdeco-dropdown__item[aria-label*="connect"]')),c||(c=await e.$('.artdeco-dropdown__item[aria-label="Connect"]')),c||(c=await e.$('div[role="button"][aria-label*="Invite"]')),c||(c=await e.$('div[role="button"][aria-label*="connect"]')),!c){let a=await e.evaluateHandle(()=>{let f=document.querySelectorAll('[role="menu"], [popover], .artdeco-dropdown__content');for(let d of f){let p=d.querySelector('a[href*="custom-invite"]');if(p)return p;for(let h of d.querySelectorAll('a, div[role="menuitem"], div[role="button"]'))if(h.innerText?.trim()==="Connect")return h}return null});c=a?.asElement?.()||null,c||await a?.dispose?.().catch(()=>{})}if(!c){let a=await e.evaluate(()=>{let f=document.querySelectorAll('[role="menu"], [popover], .artdeco-dropdown__content'),d=[];for(let p of f)for(let h of p.querySelectorAll('[role="menuitem"], a, div')){let m=h.innerText?.trim();m&&m.length<40&&d.push(m)}return[...new Set(d)].slice(0,10)});console.log(` More dropdown items: ${JSON.stringify(a)}`)}if(c)return console.log(" Found: More dropdown"),{button:c,method:"more_dropdown"};await e.keyboard.press("Escape")}catch(n){console.log(" More dropdown click failed:",n.message),await e.keyboard.press("Escape")}if(t=await e.$('button[aria-label*="Invite"]'),t&&o){let n=await e.evaluate(a=>(a.getAttribute("aria-label")||"").toLowerCase(),t),c=o.split(" ")[0].toLowerCase();n.includes("invite")&&!n.includes(c)&&(console.log(` Skipping generic button: wrong person (${n})`),t=null)}return t||(t=await e.$('button[aria-label="Connect"]')),t?(console.log(" Found: generic button"),{button:t,method:"generic_button"}):null}async function so(e,t){await e.evaluate(c=>c.scrollIntoView({behavior:"smooth",block:"center"}),t),await g(500);let o=await e.evaluate(c=>({tag:c.tagName,ariaLabel:c.getAttribute("aria-label"),href:c.getAttribute("href")}),t),s=o.tag==="A"&&o.href&&o.href.includes("custom-invite"),i=o.tag==="DIV";if(s){console.log(" Clicking custom-invite link (will navigate)..."),await Promise.all([e.waitForNavigation({waitUntil:"domcontentloaded",timeout:3e4}).catch(()=>{}),e.evaluate(a=>a.click(),t)]),await g(2e3);let c=e.url();if(c.includes("premium")||c.includes("redeem"))return console.log(" \u26A0 LinkedIn redirected to Premium page - connection limit reached?"),await e.goBack(),await g(2e3),"premium_redirect"}else i?(await e.evaluate(c=>c.click(),t),await g(3e3)):(await e.evaluate(c=>c.click(),t),await g(3e3));let l=e.url();if(l.includes("/premium/")||l.includes("/redeem"))return console.log(" \u26A0 LinkedIn redirected to Premium upsell page"),"premium_redirect";let u=await e.evaluate(()=>!!document.querySelector('[role="dialog"], .artdeco-modal, .send-invite')),n=e.url();return console.log(u?` \u2713 Connect sent \u2014 ${n}`:` \u26A0 No modal after connect click \u2014 ${n}`),"success"}async function ro(e){console.log(" Upsell: Premium required for notes");let t=await e.$('button[aria-label="Dismiss"]')||await e.$("button.artdeco-modal__dismiss");t?await t.click():await e.keyboard.press("Escape"),await g(1500);let o=await Te(e);if(o){await e.evaluate(s=>s.click(),o),await g(2e3);let r=await e.$('button[aria-label="Send without a note"]');if(r){try{await r.evaluate(s=>s.click())}catch{console.log(" Click timeout, likely still sent")}return await g(3e3),console.log(" \u2713 Connection sent (without note)"),!0}}return!1}async function io(e,t,o={}){let{message:r=null,useCustomMessage:s=!0,skipIfPending:i=!0,skipIfConnected:l=!0,excludeLocations:u=[]}=o,n={success:!1,reason:null,connectionSent:!1,withCustomMessage:!1,messageSent:null,method:null,error:null,profileInfo:null};try{let c=t.split("/in/")[1]?.replace("/","")||"unknown";if(console.log(`
|
|
12
|
+
--- Connecting: ${c} ---`),await e.goto(t,{waitUntil:"domcontentloaded",timeout:6e4}),await g(5e3),await q(e)&&(console.log(" Dismissed Premium overlay"),await g(2e3),await ft(e,t),await g(2e3)),n.profileInfo=await e.evaluate(()=>{let w=document.querySelector("h1")?.textContent?.trim()||null,S=document.querySelector("div.text-body-medium")?.textContent?.trim()||null,b=document.querySelector(".text-body-small.inline.t-black--light.break-words")?.textContent?.trim()||null,k=document.querySelector('button[aria-label^="Current company"] span.hoverable-link-text')?.textContent?.trim()||null,R=document.querySelector(".dist-value")?.textContent?.trim()||null,Ft=document.querySelector('a[href*="connectionOf"] span.t-bold')?.textContent?.trim()||null,Be=(document.querySelector('a[href*="facetConnectionOf"] span.t-normal')?.textContent?.trim()||null)?.match(/(\d+)\s+other\s+mutual/),Tt=Be?parseInt(Be[1],10):null,Lt=document.querySelector("img.pv-top-card-profile-picture__image--show")?.src||null,Pt=!!document.querySelector(".pv-text-details__verified-badge-icon");return{name:w,title:S,location:b,company:k,connectionDegree:R,connectionsCount:Ft,mutualConnections:Tt,photoUrl:Lt,hasVerifiedBadge:Pt}}),u.length>0&&oo(n.profileInfo?.location,u))return console.log(` \u26A0 Excluded location: ${n.profileInfo.location}`),n.reason="excluded_location",n;let f=await te(e);if(f==="pending"&&i)return console.log(" \u26A0 Already pending - skipping"),n.reason="pending",n;if(f==="connected"&&l)return console.log(" \u26A0 Already connected - skipping"),n.reason="connected",n;await q(e);let d=await Te(e);if(!d&&(await q(e)&&(console.log(" Dismissed overlay, retrying connect button search..."),await g(2e3),d=await Te(e)),!d))return console.log(" \u2717 Connect not found"),n.reason="button_not_found",n;if(n.method=d.method,await so(e,d.button)==="premium_redirect"){console.log(" \u26A0 Premium upsell redirect \u2014 navigating back to profile..."),await e.goto(t,{waitUntil:"domcontentloaded",timeout:6e4}),await g(4e3);let w=await te(e);if(w==="pending")return console.log(" \u2713 Connection was sent (detected Pending after upsell redirect)"),n.success=!0,n.connectionSent=!0,n.reason="sent_without_note",n.withCustomMessage=!1,n;console.log(` Status after upsell: ${w} \u2014 trying More dropdown...`),await q(e),await g(1e3);let S=await e.$('button[id*="profile-overflow-action"]:not([class*="sticky"])');if(S)try{await e.evaluate(v=>v.scrollIntoView({behavior:"smooth",block:"center"}),S),await g(500),await S.click().catch(()=>e.evaluate(v=>v.click(),S)),await g(2e3);let b=await e.$('.artdeco-dropdown__item[aria-label*="Invite"]')||await e.$('.artdeco-dropdown__item[aria-label*="connect"]')||await e.$('.artdeco-dropdown__item[aria-label="Connect"]')||await e.$('div[role="button"][aria-label*="Invite"]')||await e.$('div[role="button"][aria-label*="connect"]');if(b){console.log(" Found Connect in More dropdown, clicking..."),await e.evaluate(M=>M.click(),b),await g(3e3);let v=e.url();if(v.includes("/premium/")||v.includes("/redeem"))return console.log(" \u2717 More dropdown also redirected to Premium"),await e.goto(t,{waitUntil:"domcontentloaded",timeout:6e4}),await g(3e3),n.reason="premium_required",n;if(await te(e)==="pending")return console.log(" \u2713 Connection sent via More dropdown"),n.success=!0,n.connectionSent=!0,n.reason="sent_without_note",n.method="more_dropdown",n.withCustomMessage=!1,n;console.log(" More dropdown clicked, checking for modal...")}else return console.log(" No Connect option in More dropdown"),await e.keyboard.press("Escape"),await g(500),n.reason="premium_required",n}catch(b){return console.log(" More dropdown failed:",b.message),await e.keyboard.press("Escape").catch(()=>{}),n.reason="premium_required",n}else return console.log(" \u2717 No More button found \u2014 Premium required"),n.reason="premium_required",n}if(await e.$('input[name="email"][type="email"]')){console.log(" \u26A0 Email verification required - skipping");let w=await e.$('button[aria-label="Dismiss"]');return w&&await w.click(),await g(500),n.reason="email_required",n}let m=await e.$('button[aria-label="Add a note"]'),y=await e.$('button[aria-label="Send without a note"]');if(!m&&!y){let w=await e.evaluate(()=>{let S=document.querySelector("#interop-outlet")?.shadowRoot;if(!S)return{addNote:!1,sendWithout:!1};let b=S.querySelector('button[aria-label="Add a note"]'),v=S.querySelector('button[aria-label="Send without a note"]');return{addNote:!!b,sendWithout:!!v}});if(w.addNote){let S=await e.evaluateHandle(()=>document.querySelector("#interop-outlet")?.shadowRoot?.querySelector('button[aria-label="Add a note"]'));m=S?.asElement?.()||null,m||await S.dispose().catch(()=>{})}if(w.sendWithout){let S=await e.evaluateHandle(()=>document.querySelector("#interop-outlet")?.shadowRoot?.querySelector('button[aria-label="Send without a note"]'));y=S?.asElement?.()||null,y||await S.dispose().catch(()=>{})}}if(s){if(m&&r){if(console.log(" Adding personalized note..."),await m.click(),await g(1500),await e.$("div.modal-upsell")){let b=await ro(e);return n.success=b,n.connectionSent=b,n.reason=b?"sent_without_note_upsell":"upsell_failed",n.withCustomMessage=!1,n}let S=await e.$('textarea[name="message"]')||await e.$("textarea#custom-message")||await e.$("textarea");if(S){let b=await e.evaluate(()=>document.querySelector("h1")?.textContent?.trim()?.split(" ")[0]||""),v=r.replace("{name}",b);await S.type(v,{delay:20}),await g(500);let k=await e.$('button[aria-label="Send invitation"]')||await e.$('button[aria-label="Send now"]');if(k){try{await k.evaluate(M=>M.click())}catch{console.log(" Click timeout, likely still sent")}return await g(3e3),console.log(" \u2713 Connection sent (with note)"),n.success=!0,n.connectionSent=!0,n.reason="sent_with_note",n.withCustomMessage=!0,n.messageSent=v,n}}}else if(y){console.log(" Sending without note...");try{await y.evaluate(w=>w.click())}catch{console.log(" Click timeout, likely still sent")}return await g(3e3),console.log(" \u2713 Connection sent (without note)"),n.success=!0,n.connectionSent=!0,n.reason="sent_without_note",n.withCustomMessage=!1,n}else if(m&&!r){console.log(" No message provided, sending without note...");let w=await e.$('button[aria-label="Send without a note"]');if(w){try{await w.evaluate(S=>S.click())}catch{console.log(" Click timeout, likely still sent")}return await g(3e3),console.log(" \u2713 Connection sent (without note)"),n.success=!0,n.connectionSent=!0,n.reason="sent_without_note",n.withCustomMessage=!1,n}}}else{console.log(" Sending without note (custom messages disabled)...");let w=y;if(w){try{await w.evaluate(S=>S.click())}catch{console.log(" Click timeout, likely still sent")}return await g(3e3),console.log(" \u2713 Connection sent (without note)"),n.success=!0,n.connectionSent=!0,n.reason="sent_without_note",n.withCustomMessage=!1,n}}if(d.method!=="more_dropdown"){console.log(" \u26A0 No modal after click \u2014 trying More dropdown fallback..."),e.url().includes("/in/")||(await e.goto(t,{waitUntil:"domcontentloaded",timeout:6e4}),await g(4e3)),await q(e),await g(500);let S=await e.$$('button[id*="profile-overflow-action"]'),b=null;for(let v of S){let k=await e.evaluate(M=>{let R=M.getBoundingClientRect();return{visible:R.width>0&&R.height>0,inSticky:!!M.closest('.sticky-header, [class*="sticky"]')}},v);if(k.visible&&!k.inSticky){b=v;break}}!b&&S.length>0&&(b=S[S.length-1]);for(let v of S)v!==b&&await v.dispose().catch(()=>{});if(b)try{await e.evaluate(k=>k.scrollIntoView({behavior:"smooth",block:"center"}),b),await g(500),await b.click().catch(()=>e.evaluate(k=>k.click(),b)),await g(2e3);let v=await e.$('.artdeco-dropdown__item[aria-label*="Invite"]')||await e.$('.artdeco-dropdown__item[aria-label*="connect"]')||await e.$('.artdeco-dropdown__item[aria-label="Connect"]')||await e.$('div[role="button"][aria-label*="Invite"]')||await e.$('div[role="button"][aria-label*="connect"]');if(v){console.log(" Found Connect in More dropdown, clicking..."),n.method="more_dropdown",await e.evaluate(Y=>Y.click(),v),await g(3e3);let k=e.url();if(k.includes("/premium/")||k.includes("/redeem"))return console.log(" \u2717 More dropdown also redirected to Premium"),await e.goto(t,{waitUntil:"domcontentloaded",timeout:6e4}),await g(3e3),await te(e)==="pending"?(console.log(" \u2713 Connection was sent (detected Pending)"),n.success=!0,n.connectionSent=!0,n.reason="sent_without_note",n.withCustomMessage=!1,n):(n.reason="premium_required",n);if(await te(e)==="pending")return console.log(" \u2713 Connection sent via More dropdown"),n.success=!0,n.connectionSent=!0,n.reason="sent_without_note",n.withCustomMessage=!1,n;let R=await e.$('button[aria-label="Send without a note"]');if(R){console.log(" Modal appeared via More dropdown, sending without note...");try{await R.evaluate(Y=>Y.click())}catch{}return await g(3e3),console.log(" \u2713 Connection sent (without note, via More dropdown)"),n.success=!0,n.connectionSent=!0,n.reason="sent_without_note",n.withCustomMessage=!1,n}console.log(" More dropdown: no modal either")}else console.log(" No Connect option in More dropdown"),await e.keyboard.press("Escape")}catch(v){console.log(" More dropdown fallback failed:",v.message),await e.keyboard.press("Escape").catch(()=>{})}}return console.log(" \u2717 Could not complete connection request"),n.reason="modal_failed",n}catch(c){console.error(` \u2717 Error: ${c.message}`),n.reason="error",n.error=c.message;let a=c.message.toLowerCase();if(a.includes("detached")||a.includes("session closed")||a.includes("target closed")||a.includes("browser has disconnected")||a.includes("protocol error"))return n.fatal=!0,n;try{await e.goto("about:blank",{timeout:1e4}),await g(1e3)}catch{n.fatal=!0}return n}}async function pt(e,t,o={}){let{message:r=null,useCustomMessage:s=!0,maxConnections:i=20,viewOnly:l=!1,delayBetweenProfiles:u=3e3,onProfileProcessed:n=null,excludeLocations:c=[],source:a="unknown"}=o,f={viewed:0,connectionsSent:0,skipped:{pending:0,connected:0,email:0,notFound:0,excludedLocation:0},errors:0};if(console.log(`
|
|
13
|
+
=== Running Funnel: ${t.length} profiles ===`),console.log(`Mode: ${l?"View Only":`Connect (max ${i})`}`),l||console.log(`Custom messages: ${s?"Enabled":"Disabled (saving quota)"}`),c.length>0&&console.log(`Excluding locations: ${c.join(", ")}`),!l){let p=ee();console.log(`Quota: today ${p.daily.sent}/${p.daily.max} | week ${p.weekly.sent}/${p.weekly.max}`)}console.log("");let d=0;for(let p=0;p<t.length;p++){let h=t[p];try{if(e.isClosed()){console.log(`
|
|
14
|
+
\u26A0 Browser page is closed \u2014 aborting funnel`);break}}catch{console.log(`
|
|
15
|
+
\u26A0 Browser disconnected \u2014 aborting funnel`);break}if(!h.profileUrl){console.log(`[${p+1}/${t.length}] Skipping - no URL`);continue}let m=me(h.profileUrl);if(m==="pending"||m==="connected"||m==="sent_with_note"||m==="sent_without_note"||m==="sent_without_note_upsell"){console.log(`[${p+1}/${t.length}] ${h.name||h.profileUrl} \u2014 skipped (db: ${m})`),m==="pending"||m==="sent_with_note"||m==="sent_without_note"||m==="sent_without_note_upsell"?f.skipped.pending++:f.skipped.connected++,n&&await n(h,f,{success:!1,reason:m,connectionSent:!1,withCustomMessage:!1,messageSent:null,method:null,error:null,profileInfo:null});continue}console.log(`[${p+1}/${t.length}] ${h.name||h.profileUrl}`);let y=null;if(l){let w=await no(e,h.profileUrl);w?(f.viewed++,y={success:!0,reason:"viewed",connectionSent:!1,profileInfo:w}):(f.errors++,y={success:!1,reason:"view_failed",connectionSent:!1,profileInfo:null})}else{if(f.connectionsSent>=i){console.log(` Max connections per session (${i}) reached`);break}let w=Se();if(!w.allowed){console.log(` \u26A0 Global quota exhausted: ${w.reason}`);break}if(y=await io(e,h.profileUrl,{message:r,useCustomMessage:s,excludeLocations:c}),y.success)f.connectionsSent++,y.connectionSent&&we(h.profileUrl,a);else switch(y.reason){case"pending":f.skipped.pending++;break;case"connected":f.skipped.connected++;break;case"email_required":f.skipped.email++;break;case"button_not_found":f.skipped.notFound++;break;case"excluded_location":f.skipped.excludedLocation++;break;default:f.errors++}}if(y?.fatal){console.log(`
|
|
16
|
+
\u26A0 Fatal error \u2014 page is dead, aborting funnel`),f.errors++;break}if(y?.reason==="error"){if(d++,d>=3){console.log(`
|
|
17
|
+
\u26A0 ${d} consecutive errors \u2014 aborting funnel`);break}}else d=0;if(y&&y.reason&&(ge(h.profileUrl,y.reason,{method:y.method,messageSent:y.messageSent,profileInfo:y.profileInfo}),pe(h.profileUrl,{source:a||"funnel",jobId:h.jobId||h.fromJob?.jobId||null,jobTitle:h.fromJob?.title||null,jobCompany:h.fromJob?.company||null,action:y.reason})),n&&await n(h,f,y),p<t.length-1){let w=u+Math.random()*2e3;await g(w)}}if(console.log(`
|
|
18
|
+
=== Funnel Complete ===`),console.log(` Viewed: ${f.viewed}`),console.log(` Connections sent: ${f.connectionsSent}`),console.log(` Skipped (pending): ${f.skipped.pending}`),console.log(` Skipped (connected): ${f.skipped.connected}`),console.log(` Skipped (email req): ${f.skipped.email}`),console.log(` Skipped (not found): ${f.skipped.notFound}`),f.skipped.excludedLocation>0&&console.log(` Skipped (excluded location): ${f.skipped.excludedLocation}`),console.log(` Errors: ${f.errors}`),!l){let p=ee();console.log(` Quota remaining: today ${p.daily.remaining}/${p.daily.max} | week ${p.weekly.remaining}/${p.weekly.max}`)}return f}import"dotenv/config";import*as x from"fs";import*as T from"path";import{fileURLToPath as lo}from"url";var co=lo(import.meta.url),ao=T.dirname(T.dirname(co)),oe=process.env.DATA_DIR?T.resolve(process.env.DATA_DIR,"collected-profiles"):T.join(ao,"collected-profiles");function Le(){x.existsSync(oe)||x.mkdirSync(oe,{recursive:!0})}function uo(){return new Date().toISOString().split("T")[0]}function mt(e,t=null){let o=t||uo();return T.join(oe,`${e}-${o}.json`)}function gt(e,t=null){Le();let o=mt(e,t);try{if(x.existsSync(o)){let r=x.readFileSync(o,"utf-8"),s=JSON.parse(r);return console.log(`\u{1F4C2} Loaded ${s.profiles.length} profiles from ${o}`),s.profiles}}catch(r){console.error(`Error loading profiles from ${o}:`,r.message)}return[]}function ht(e,t=null){return gt(e,t).filter(r=>!r.processed)}function wt(e,t,o,r=null){Le();let s=mt(e,r);try{if(!x.existsSync(s))return;let i=JSON.parse(x.readFileSync(s,"utf-8")),l=i.profiles.find(u=>u.profileUrl===t);if(l){if(l.processed=!0,l.processedAt=new Date().toISOString(),typeof o=="string")l.status=o;else if(typeof o=="object"){if(l.status=o.status,o.profileInfo){let u=o.profileInfo;u.name&&(l.name=u.name),u.title&&(l.title=u.title),u.location&&(l.location=u.location),u.company&&(l.company=u.company),u.connectionDegree&&(l.connectionDegree=u.connectionDegree),u.connectionsCount&&(l.connectionsCount=u.connectionsCount),u.mutualConnections&&(l.mutualConnections=u.mutualConnections),u.photoUrl&&(l.photoUrl=u.photoUrl),u.hasVerifiedBadge!==void 0&&(l.hasVerifiedBadge=u.hasVerifiedBadge)}l.funnelResult={connectionSent:o.connectionSent||!1,withCustomMessage:o.withCustomMessage||!1,message:o.message||null,method:o.method||null,reason:o.reason||null,error:o.error||null}}i.lastUpdated=new Date().toISOString(),x.writeFileSync(s,JSON.stringify(i,null,2))}}catch(i){console.error("Error marking profile processed:",i.message)}}function yt(e){let t=gt(e),o=t.filter(s=>s.processed),r=t.filter(s=>!s.processed);return{total:t.length,processed:o.length,unprocessed:r.length,statuses:o.reduce((s,i)=>(s[i.status]=(s[i.status]||0)+1,s),{})}}function St(){Le();try{return x.readdirSync(oe).filter(t=>t.endsWith(".json")).map(t=>{let[o,r]=t.replace(".json","").split(/-(?=\d{4}-\d{2}-\d{2}$)/);try{let s=JSON.parse(x.readFileSync(T.join(oe,t),"utf-8"));return{file:t,source:o,date:r,count:s.profiles?.length||0,unprocessed:s.profiles?.filter(i=>!i.processed).length||0}}catch{return{file:t,source:o,date:r,count:0,unprocessed:0}}})}catch(e){return console.error("Error listing files:",e.message),[]}}import"dotenv/config";import*as E from"fs";import*as I from"path";import{fileURLToPath as ko}from"url";var Io=ko(import.meta.url),xo=I.dirname(Io),Et=I.dirname(I.dirname(xo)),ve=process.env.DATA_DIR?I.resolve(process.env.DATA_DIR,"collected-profiles"):I.join(Et,"collected-profiles"),Ce=process.env.DATA_DIR?I.resolve(process.env.DATA_DIR,"market-research"):I.join(Et,"market-research"),Ne=1,Do=[{version:1,file:"./v001-normalize-jobs-and-questions.mjs"}];function At(e){return I.join(e,".schema-version")}function Ue(e){let t=At(e);if(!E.existsSync(t))return{version:0,migrations:[]};try{return JSON.parse(E.readFileSync(t,"utf-8"))}catch{return{version:0,migrations:[]}}}function _o(e,t){E.writeFileSync(At(e),JSON.stringify(t,null,2))}function _t(e,t){if(!E.existsSync(e))return;let o=`.pre-v${String(t).padStart(3,"0")}.backup`,r=E.readdirSync(e).filter(s=>s.endsWith(".json")&&!s.includes(".backup"));for(let s of r){let i=I.join(e,s),l=I.join(e,s+o);try{E.copyFileSync(i,l)}catch(u){console.error(` [!] Failed to backup ${s}: ${u.message}`)}}r.length>0&&console.log(` Backed up ${r.length} files in ${I.basename(e)}/ (${o})`)}var jt=!1;async function Ot(){if(jt)return;jt=!0;let e=Ue(ve),t=Ue(Ce),o=Math.min(e.version,t.version);if(o>=Ne)return;console.log(`
|
|
19
|
+
\u{1F4E6} Schema migration: v${o} \u2192 v${Ne}`);let r=Do.filter(s=>s.version>o);for(let s of r){console.log(`
|
|
20
|
+
Running migration v${String(s.version).padStart(3,"0")}...`),_t(ve,s.version),_t(Ce,s.version);let i=await import(s.file);try{await i.up(ve,Ce)}catch(n){console.error(`
|
|
21
|
+
[FATAL] Migration v${s.version} failed: ${n.message}`),console.error(` Backups are in collected-profiles/ and market-research/ (.pre-v${String(s.version).padStart(3,"0")}.backup)`),console.error(` Fix the issue and restart.
|
|
22
|
+
`),process.exit(1)}let l=new Date().toISOString(),u={version:s.version,name:i.NAME||`v${String(s.version).padStart(3,"0")}`,appliedAt:l};for(let n of[ve,Ce]){if(!E.existsSync(n))continue;let c=Ue(n);c.version=s.version,c.migratedAt=l,c.migrations.push(u),_o(n,c)}console.log(` \u2713 Migration v${String(s.version).padStart(3,"0")} complete`)}try{let{resetCaches:s}=await Promise.resolve().then(()=>(Fe(),dt));s()}catch{}try{let{resetCaches:s}=await Promise.resolve().then(()=>(Dt(),xt));s()}catch{}console.log(`
|
|
23
|
+
\u2713 Schema is now at v${Ne}
|
|
24
|
+
`)}await Ot();function Eo(){let e={source:"recruiters-from-jobs",date:null,viewOnly:!1,max:20,list:!1,useCustomMessage:!0,message:"Hi {name}, I just applied for a position at your company and would love to connect!",excludeLocations:[]};process.env.FUNNEL_EXCLUDE_LOCATIONS&&(e.excludeLocations=process.env.FUNNEL_EXCLUDE_LOCATIONS.split(",").map(t=>t.trim()).filter(Boolean));for(let t of process.argv.slice(2))t==="--list"?e.list=!0:t==="--view-only"?e.viewOnly=!0:t==="--no-note"?e.useCustomMessage=!1:t==="--with-note"?e.useCustomMessage=!0:t.startsWith("--source=")?e.source=t.split("=")[1]:t.startsWith("--date=")?e.date=t.split("=")[1]:t.startsWith("--max=")?e.max=parseInt(t.split("=")[1],10):t.startsWith("--message=")?e.message=t.split("=")[1]:t.startsWith("--exclude-locations=")&&(e.excludeLocations=t.split("=")[1].split(",").map(o=>o.trim()).filter(Boolean));return e}async function Ao(){let e=Eo();if(e.list){console.log(`
|
|
25
|
+
\u{1F4C2} Available profile files:
|
|
26
|
+
`);let n=St();if(n.length===0)console.log(" No files found in collected-profiles/ (DATA_DIR)");else for(let c of n)console.log(` ${c.file}`),console.log(` Total: ${c.count}, Unprocessed: ${c.unprocessed}`);console.log(""),process.exit(0)}console.log(`
|
|
27
|
+
========================================`),console.log("Profile Funnel Runner"),console.log(`========================================
|
|
28
|
+
`),console.log(`Source: ${e.source}`),console.log(`Date: ${e.date||"today"}`),console.log(`Mode: ${e.viewOnly?"View Only":"Connect"}`),console.log(`Max connections: ${e.max}`),console.log(`Custom messages: ${e.useCustomMessage?"Enabled":"Disabled (--no-note)"}`),e.excludeLocations.length>0&&console.log(`Excluding locations: ${e.excludeLocations.join(", ")}`);let t=ht(e.source,e.date);console.log(`
|
|
29
|
+
Profiles to process: ${t.length}`),t.length===0&&(console.log(`
|
|
30
|
+
\u2705 No unprocessed profiles. Nothing to do!`),process.exit(0)),console.log(`
|
|
31
|
+
Profiles:`),t.slice(0,10).forEach((n,c)=>{console.log(` ${c+1}. ${n.name||"Unknown"} - ${n.fromJob?.company||"N/A"}`)}),t.length>10&&console.log(` ... and ${t.length-10} more`),console.log(`
|
|
32
|
+
Press Enter to start, or Ctrl+C to cancel...`),await new Promise(n=>{process.stdin.once("data",()=>n())}),console.log(`
|
|
33
|
+
\u{1F680} Starting browser...
|
|
34
|
+
`);let o=await jo.launch({headless:!1,devtools:!1,defaultViewport:null,protocolTimeout:3e5}),r=await o.newPage();try{let n=Mt.readFileSync("cookies.json","utf-8"),c=JSON.parse(n);await r.setCookie(...c),console.log(`\u2713 Cookies loaded
|
|
35
|
+
`)}catch{console.error("Error loading cookies. Run login first."),await o.close(),process.exit(1)}let s=e.viewOnly?"view-only":"connect",i=Ye("funnel",s),l=await pt(r,t,{message:e.message,useCustomMessage:e.useCustomMessage,maxConnections:e.max,viewOnly:e.viewOnly,excludeLocations:e.excludeLocations,source:e.source||"funnel-runner",delayBetweenProfiles:3e3,onProfileProcessed:async(n,c,a)=>{wt(e.source,n.profileUrl,{status:a.reason,connectionSent:a.connectionSent,withCustomMessage:a.withCustomMessage,message:a.messageSent,method:a.method,reason:a.reason,error:a.error,profileInfo:a.profileInfo})}});console.log(`
|
|
36
|
+
========================================`),console.log("Funnel Complete!"),console.log(`========================================
|
|
37
|
+
`);let u=yt(e.source);if(console.log(`Processed: ${u.processed}/${u.total}`),console.log(`Remaining: ${u.unprocessed}`),Object.keys(u.statuses).length>0){console.log(`
|
|
38
|
+
Status breakdown:`);for(let[n,c]of Object.entries(u.statuses))console.log(` ${n}: ${c}`)}Ie(i,"success",{sent:l.connectionsSent,viewed:l.viewed,errors:l.errors,skippedPending:l.skipped?.pending||0,skippedConnected:l.skipped?.connected||0}),await o.close(),process.exit(0)}Ao().catch(e=>{console.error("Funnel failed:",e);try{Ie(null,"error",null,e.message)}catch{}process.exit(1)});
|