Spaces:
Running
Running
File size: 6,327 Bytes
bd28470 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | /**
* Personal LinkedIn Finder
*
* Finds linkedin.com/in/person-name (personal profile)
* NOT linkedin.com/company/ (company page β already have that)
*
* Methods in priority order:
* 1. Google search: "name" "company" site:linkedin.com/in
* 2. Company's LinkedIn people page scrape
* 3. Hunter.io linkedin_url field (sometimes returned)
*
* MANDATORY β every qualified lead must have a LinkedIn attempt.
*/
import { searchCompanies, SerperResult } from "../providers/serper";
import { serperLimiter } from "../../shared/utils/rate-limiter";
import { logger } from "../../shared/utils/logger";
import axios from "axios";
import { getEnv } from "../../shared/config/env";
export interface PersonalLinkedIn {
url: string; // linkedin.com/in/john-smith-abc123
confidence: number; // how sure we are this is the right person
source: "google_search" | "company_people_page" | "hunter_field";
verified: boolean; // URL format is valid and accessible
}
/**
* Find personal LinkedIn profile for a decision maker.
* Tries multiple methods. Returns null if all fail (not an error β just LinkedIn-not-found).
*/
export async function findPersonalLinkedIn(
fullName: string,
companyName: string,
companyDomain: string,
companyLinkedInUrl: string | null
): Promise<PersonalLinkedIn | null> {
// Method 1: Google search (highest accuracy)
const googleResult = await searchViaGoogle(fullName, companyName);
if (googleResult) return googleResult;
// Method 2: From company LinkedIn people page (already scraped)
if (companyLinkedInUrl) {
const peopleResult = await searchViaPeoplePage(fullName, companyLinkedInUrl);
if (peopleResult) return peopleResult;
}
logger.info({ fullName, companyName }, "LinkedIn personal not found β all methods tried");
return null;
}
// βββ Method 1: Google Search βββββββββββββββββββββββββββββββββ
async function searchViaGoogle(
fullName: string,
companyName: string
): Promise<PersonalLinkedIn | null> {
try {
await serperLimiter.consume("serper");
const env = getEnv();
const query = `"${fullName}" "${companyName}" site:linkedin.com/in`;
const response = await axios.post(
"https://google.serper.dev/search",
{ q: query, num: 5 },
{
headers: {
"X-API-KEY": env.SERPER_API_KEY,
"Content-Type": "application/json",
},
timeout: 8_000,
}
);
const organic = response.data?.organic ?? [];
for (const result of organic) {
const url = result.link;
if (!isLinkedInPersonalUrl(url)) continue;
// Verify the result mentions both name and company
const snippet = (result.snippet ?? "").toLowerCase();
const title = (result.title ?? "").toLowerCase();
const combined = `${snippet} ${title}`;
const nameParts = fullName.toLowerCase().split(/\s+/);
const hasName = nameParts.some(part => part.length > 2 && combined.includes(part));
const hasCompany = companyName.toLowerCase().split(/\s+/).some(
part => part.length > 3 && combined.includes(part)
);
if (hasName) {
return {
url: cleanLinkedInUrl(url),
confidence: hasCompany ? 0.92 : 0.70,
source: "google_search",
verified: true,
};
}
}
return null;
} catch (err) {
logger.warn({ fullName, err }, "Google LinkedIn search failed");
return null;
}
}
// βββ Method 2: Company People Page ββββββββββββββββββββββββββ
async function searchViaPeoplePage(
fullName: string,
companyLinkedInUrl: string
): Promise<PersonalLinkedIn | null> {
try {
await serperLimiter.consume("serper");
const env = getEnv();
// Search Google for the person's name on the company's LinkedIn
const companySlug = companyLinkedInUrl.match(/company\/([^/?]+)/)?.[1];
if (!companySlug) return null;
const query = `"${fullName}" site:linkedin.com/in ${companySlug}`;
const response = await axios.post(
"https://google.serper.dev/search",
{ q: query, num: 3 },
{
headers: {
"X-API-KEY": env.SERPER_API_KEY,
"Content-Type": "application/json",
},
timeout: 8_000,
}
);
const organic = response.data?.organic ?? [];
for (const result of organic) {
if (isLinkedInPersonalUrl(result.link)) {
return {
url: cleanLinkedInUrl(result.link),
confidence: 0.75,
source: "company_people_page",
verified: true,
};
}
}
return null;
} catch {
return null;
}
}
// βββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββ
function isLinkedInPersonalUrl(url: string): boolean {
// Must be linkedin.com/in/ (personal) not /company/ or /jobs/
return /linkedin\.com\/in\/[a-zA-Z0-9\-]+/.test(url);
}
function cleanLinkedInUrl(url: string): string {
// Remove query params and fragments, normalize
const match = url.match(/(https?:\/\/(?:www\.)?linkedin\.com\/in\/[a-zA-Z0-9\-]+)/);
return match ? match[1] : url;
}
/**
* Batch find LinkedIn profiles for multiple decision makers.
* Stops after 5 to conserve API calls.
*/
export async function batchFindLinkedIn(
people: { fullName: string; title: string }[],
companyName: string,
companyDomain: string,
companyLinkedInUrl: string | null
): Promise<Map<string, PersonalLinkedIn>> {
const results = new Map<string, PersonalLinkedIn>();
const maxLookups = Math.min(people.length, 5);
for (let i = 0; i < maxLookups; i++) {
const person = people[i];
const result = await findPersonalLinkedIn(
person.fullName,
companyName,
companyDomain,
companyLinkedInUrl
);
if (result) {
results.set(person.fullName, result);
}
// Small delay between searches to be polite
await new Promise(r => setTimeout(r, 1500));
}
logger.info({ company: companyName, found: results.size, attempted: maxLookups },
"LinkedIn personal batch complete"
);
return results;
}
|