Skip to content

Instantly share code, notes, and snippets.

@twosdai
Created February 13, 2025 17:49
Show Gist options
  • Save twosdai/3dbd353720e59229f35915b2b8e15d56 to your computer and use it in GitHub Desktop.
Save twosdai/3dbd353720e59229f35915b2b8e15d56 to your computer and use it in GitHub Desktop.
const fetch = require("cross-fetch");
async function fetchAllLeads(url) {
const leadsMap = new Map();
let offset = 0;
const limit = 100;
let totalLeads = Infinity; // initial dummy value
while (offset < totalLeads) {
try {
const response = await fetch(`${url}&offset=${offset}&limit=${limit}`);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const json = await response.json();
// Set totalLeads based on the response (if not already set)
totalLeads = parseInt(json.total_leads, 10);
// Iterate through the leads and store in the map
json.data.forEach((item) => {
if (item.lead && item.lead.email) {
// Using email as a unique key; store additional info if needed
leadsMap.set(item.lead.email, {
first_name: item.lead.first_name,
last_name: item.lead.last_name,
email: item.lead.email,
});
}
});
offset += limit;
} catch (error) {
console.error(
`Error fetching leads from ${url} at offset ${offset}:`,
error
);
break; // exit loop if error encountered
}
}
return leadsMap;
}
async function processIntersection() {
try {
const campaign1Url =
"https://server.smartlead.ai/api/v1/campaigns/:id1/leads?api_key=secret";
const campaign2Url =
"https://server.smartlead.ai/api/v1/campaigns/:id2/leads?api_key=secret";
// Fetch both campaigns concurrently
const [leadsCampaign1, leadsCampaign2] = await Promise.all([
fetchAllLeads(campaign1Url),
fetchAllLeads(campaign2Url),
]);
// Find intersection based on email and log the details
for (let [email, lead] of leadsCampaign1) {
if (leadsCampaign2.has(email)) {
console.log(
`Email: ${email}, First Name: ${lead.first_name}, Last Name: ${lead.last_name}`
);
}
}
} catch (error) {
console.error("Error processing intersection of leads:", error);
}
}
// Start the process
processIntersection();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment