lacspace-leads 0.3.0 → 1.3.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 +50 -8
- package/dist/cli.js +383 -100
- package/dist/lib.cjs +323 -91
- package/dist/lib.d.cts +134 -8
- package/dist/lib.d.ts +134 -8
- package/dist/lib.js +307 -86
- package/package.json +6 -2
package/dist/lib.js
CHANGED
|
@@ -20,12 +20,15 @@ var ALL_FIELDS = [
|
|
|
20
20
|
"youtube",
|
|
21
21
|
"tiktok",
|
|
22
22
|
"telegram",
|
|
23
|
+
"emailStatus",
|
|
23
24
|
"plusCode",
|
|
24
25
|
"latitude",
|
|
25
26
|
"longitude",
|
|
27
|
+
"distanceKm",
|
|
26
28
|
"hours",
|
|
27
29
|
"mapsUrl"
|
|
28
30
|
];
|
|
31
|
+
var DERIVED_FIELDS = ["distanceKm"];
|
|
29
32
|
var ENRICHED_FIELDS = [
|
|
30
33
|
"email",
|
|
31
34
|
"facebook",
|
|
@@ -35,7 +38,8 @@ var ENRICHED_FIELDS = [
|
|
|
35
38
|
"twitter",
|
|
36
39
|
"youtube",
|
|
37
40
|
"tiktok",
|
|
38
|
-
"telegram"
|
|
41
|
+
"telegram",
|
|
42
|
+
"emailStatus"
|
|
39
43
|
];
|
|
40
44
|
var FIELD_PRESETS = {
|
|
41
45
|
/** Just enough to place a business: name, phone, website. */
|
|
@@ -47,11 +51,13 @@ var FIELD_PRESETS = {
|
|
|
47
51
|
/** Map/geo columns for plotting. */
|
|
48
52
|
geo: ["name", "address", "latitude", "longitude", "plusCode", "mapsUrl"],
|
|
49
53
|
/** Everything Maps shows, no website enrichment. */
|
|
50
|
-
full: ALL_FIELDS.filter((f) => !ENRICHED_FIELDS.includes(f)),
|
|
54
|
+
full: ALL_FIELDS.filter((f) => !ENRICHED_FIELDS.includes(f) && !DERIVED_FIELDS.includes(f)),
|
|
51
55
|
/** Every field, including enriched ones. */
|
|
52
56
|
everything: [...ALL_FIELDS]
|
|
53
57
|
};
|
|
54
|
-
var DEFAULT_FIELDS = ALL_FIELDS.filter(
|
|
58
|
+
var DEFAULT_FIELDS = ALL_FIELDS.filter(
|
|
59
|
+
(f) => !ENRICHED_FIELDS.includes(f) && !DERIVED_FIELDS.includes(f)
|
|
60
|
+
);
|
|
55
61
|
|
|
56
62
|
// src/query.ts
|
|
57
63
|
function composeQuery(opts) {
|
|
@@ -65,7 +71,9 @@ function mapsSearchUrl(query, opts = {}) {
|
|
|
65
71
|
const hl = (opts.hl ?? "en").split("-")[0] || "en";
|
|
66
72
|
const params = new URLSearchParams({ hl });
|
|
67
73
|
if (opts.gl) params.set("gl", opts.gl.toLowerCase());
|
|
68
|
-
|
|
74
|
+
const base = `https://www.google.com/maps/search/${encodeURIComponent(query)}`;
|
|
75
|
+
const at = opts.center ? `/@${opts.center.lat},${opts.center.lng},${Math.round(opts.center.zoom ?? 14)}z` : "";
|
|
76
|
+
return `${base}${at}?${params.toString()}`;
|
|
69
77
|
}
|
|
70
78
|
function expandQueries(opts) {
|
|
71
79
|
if (opts.query && opts.query.trim()) return [{ query: opts.query.trim() }];
|
|
@@ -223,6 +231,7 @@ function filterLeads(leads, filters = {}) {
|
|
|
223
231
|
if (filters.hasPhone && !l.phone) return false;
|
|
224
232
|
if (filters.hasWebsite && !l.website) return false;
|
|
225
233
|
if (filters.hasEmail && !l.email) return false;
|
|
234
|
+
if (filters.hasValidEmail && l.emailStatus !== "valid") return false;
|
|
226
235
|
return true;
|
|
227
236
|
});
|
|
228
237
|
}
|
|
@@ -230,7 +239,8 @@ function dedupeKey(lead, by) {
|
|
|
230
239
|
if (by === "none") return void 0;
|
|
231
240
|
if (by === "website") return normalizeHost(lead.website);
|
|
232
241
|
if (by === "phone") return lead.phone?.replace(/[^0-9]/g, "") || void 0;
|
|
233
|
-
return lead.name?.trim().toLowerCase() || void 0;
|
|
242
|
+
if (by === "name") return lead.name?.trim().toLowerCase() || void 0;
|
|
243
|
+
return normalizeHost(lead.website) ?? (lead.phone?.replace(/[^0-9]/g, "") || void 0) ?? (lead.name?.trim().toLowerCase() || void 0);
|
|
234
244
|
}
|
|
235
245
|
function normalizeHost(url) {
|
|
236
246
|
if (!url) return void 0;
|
|
@@ -363,12 +373,14 @@ function plausible(digits) {
|
|
|
363
373
|
return digits.length >= 8 && digits.length <= 15;
|
|
364
374
|
}
|
|
365
375
|
var priceRank = (p) => p ? p.replace(/[^$€£₹¥₩]/g, "").length : 0;
|
|
376
|
+
var ASC_BY_DEFAULT = /* @__PURE__ */ new Set(["name", "distance"]);
|
|
366
377
|
function sortLeads(leads, by, dir) {
|
|
367
378
|
if (!by) return [...leads];
|
|
368
|
-
const desc = dir ? dir === "desc" : by
|
|
379
|
+
const desc = dir ? dir === "desc" : !ASC_BY_DEFAULT.has(by);
|
|
369
380
|
const val = (l) => {
|
|
370
381
|
if (by === "name") return l.name?.toLowerCase();
|
|
371
382
|
if (by === "priceLevel") return l.priceLevel ? priceRank(l.priceLevel) : void 0;
|
|
383
|
+
if (by === "distance") return l.distanceKm;
|
|
372
384
|
return l[by];
|
|
373
385
|
};
|
|
374
386
|
return [...leads].map((lead, i) => ({ lead, i })).sort((a, b) => {
|
|
@@ -387,6 +399,196 @@ function sortLeads(leads, by, dir) {
|
|
|
387
399
|
}).map((x) => x.lead);
|
|
388
400
|
}
|
|
389
401
|
|
|
402
|
+
// src/verify.ts
|
|
403
|
+
import { resolveMx } from "dns/promises";
|
|
404
|
+
var EMAIL_SHAPE = /^[^\s@]+@([^\s@.]+\.)+[^\s@.]{2,}$/;
|
|
405
|
+
function emailFormatValid(email) {
|
|
406
|
+
return !!email && EMAIL_SHAPE.test(email.trim());
|
|
407
|
+
}
|
|
408
|
+
function emailDomain(email) {
|
|
409
|
+
if (!email) return void 0;
|
|
410
|
+
const at = email.lastIndexOf("@");
|
|
411
|
+
return at >= 0 ? email.slice(at + 1).trim().toLowerCase() || void 0 : void 0;
|
|
412
|
+
}
|
|
413
|
+
async function domainHasMail(domain, cache, timeoutMs) {
|
|
414
|
+
const cached = cache.get(domain);
|
|
415
|
+
if (cached !== void 0) return cached;
|
|
416
|
+
const timer = new Promise((res) => setTimeout(() => res(false), timeoutMs));
|
|
417
|
+
const lookup = resolveMx(domain).then((records) => Array.isArray(records) && records.length > 0).catch(() => false);
|
|
418
|
+
const ok = await Promise.race([lookup, timer]);
|
|
419
|
+
cache.set(domain, ok);
|
|
420
|
+
return ok;
|
|
421
|
+
}
|
|
422
|
+
async function verifyEmail(email, opts = {}) {
|
|
423
|
+
if (!email) return "unknown";
|
|
424
|
+
if (!emailFormatValid(email)) return "invalid-format";
|
|
425
|
+
const domain = emailDomain(email);
|
|
426
|
+
if (!domain) return "invalid-format";
|
|
427
|
+
const has = await domainHasMail(domain, opts.cache ?? /* @__PURE__ */ new Map(), opts.timeoutMs ?? 5e3);
|
|
428
|
+
return has ? "valid" : "no-mx";
|
|
429
|
+
}
|
|
430
|
+
async function verifyEmails(leads, opts = {}) {
|
|
431
|
+
const cache = /* @__PURE__ */ new Map();
|
|
432
|
+
const targets = leads.filter((l) => l.email);
|
|
433
|
+
const n = Math.max(1, Math.min(opts.concurrency ?? 5, targets.length || 1));
|
|
434
|
+
let next = 0;
|
|
435
|
+
let done = 0;
|
|
436
|
+
const verifyOpts = { cache };
|
|
437
|
+
if (opts.timeoutMs !== void 0) verifyOpts.timeoutMs = opts.timeoutMs;
|
|
438
|
+
const workers = Array.from({ length: n }, async () => {
|
|
439
|
+
for (; ; ) {
|
|
440
|
+
const i = next++;
|
|
441
|
+
if (i >= targets.length) return;
|
|
442
|
+
const lead = targets[i];
|
|
443
|
+
lead.emailStatus = await verifyEmail(lead.email, verifyOpts);
|
|
444
|
+
opts.onProgress?.(`verifying email ${++done}/${targets.length}: ${lead.email}\u2026`);
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
await Promise.all(workers);
|
|
448
|
+
return leads;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// src/geo.ts
|
|
452
|
+
var R_EARTH_M = 63710088e-1;
|
|
453
|
+
var toRad = (deg) => deg * Math.PI / 180;
|
|
454
|
+
function haversineMeters(a, b) {
|
|
455
|
+
const dLat = toRad(b.lat - a.lat);
|
|
456
|
+
const dLng = toRad(b.lng - a.lng);
|
|
457
|
+
const lat1 = toRad(a.lat);
|
|
458
|
+
const lat2 = toRad(b.lat);
|
|
459
|
+
const h = Math.sin(dLat / 2) ** 2 + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLng / 2) ** 2;
|
|
460
|
+
return 2 * R_EARTH_M * Math.asin(Math.min(1, Math.sqrt(h)));
|
|
461
|
+
}
|
|
462
|
+
function parseLatLngPair(input) {
|
|
463
|
+
if (!input) return void 0;
|
|
464
|
+
const m = input.replace(/^@/, "").match(/(-?\d+(?:\.\d+)?)\s*[, ]\s*(-?\d+(?:\.\d+)?)/);
|
|
465
|
+
if (!m) return void 0;
|
|
466
|
+
const lat = parseFloat(m[1]);
|
|
467
|
+
const lng = parseFloat(m[2]);
|
|
468
|
+
if (!Number.isFinite(lat) || !Number.isFinite(lng)) return void 0;
|
|
469
|
+
if (lat < -90 || lat > 90 || lng < -180 || lng > 180) return void 0;
|
|
470
|
+
return { lat, lng };
|
|
471
|
+
}
|
|
472
|
+
function parseDistance(input) {
|
|
473
|
+
if (!input) return void 0;
|
|
474
|
+
const m = input.trim().toLowerCase().match(/^(\d+(?:\.\d+)?)\s*(km|m|mi|mile|miles|meters?|metres?)?$/);
|
|
475
|
+
if (!m) return void 0;
|
|
476
|
+
const value = parseFloat(m[1]);
|
|
477
|
+
if (!Number.isFinite(value) || value <= 0) return void 0;
|
|
478
|
+
const unit = m[2] ?? "m";
|
|
479
|
+
const metres = unit === "km" ? value * 1e3 : unit.startsWith("mi") ? value * 1609.344 : value;
|
|
480
|
+
return metres;
|
|
481
|
+
}
|
|
482
|
+
function zoomForRadius(radiusM, lat = 0) {
|
|
483
|
+
const mpp = 2 * Math.max(50, radiusM) / 600;
|
|
484
|
+
const zoom = Math.log2(156543.03392 * Math.cos(toRad(lat)) / mpp);
|
|
485
|
+
return Math.max(3, Math.min(19, Math.round(zoom)));
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// src/export.ts
|
|
489
|
+
import { stringify as csvStringify } from "@lacspace/csv";
|
|
490
|
+
import { jsonToXlsx } from "@lacspace/xlsx";
|
|
491
|
+
var HEADERS = {
|
|
492
|
+
name: "Name",
|
|
493
|
+
category: "Category",
|
|
494
|
+
rating: "Rating",
|
|
495
|
+
reviews: "Reviews",
|
|
496
|
+
priceLevel: "Price",
|
|
497
|
+
address: "Address",
|
|
498
|
+
phone: "Phone",
|
|
499
|
+
website: "Website",
|
|
500
|
+
email: "Email",
|
|
501
|
+
facebook: "Facebook",
|
|
502
|
+
instagram: "Instagram",
|
|
503
|
+
whatsapp: "WhatsApp",
|
|
504
|
+
linkedin: "LinkedIn",
|
|
505
|
+
twitter: "Twitter/X",
|
|
506
|
+
youtube: "YouTube",
|
|
507
|
+
tiktok: "TikTok",
|
|
508
|
+
telegram: "Telegram",
|
|
509
|
+
emailStatus: "Email Status",
|
|
510
|
+
plusCode: "Plus Code",
|
|
511
|
+
latitude: "Latitude",
|
|
512
|
+
longitude: "Longitude",
|
|
513
|
+
distanceKm: "Distance (km)",
|
|
514
|
+
hours: "Hours",
|
|
515
|
+
mapsUrl: "Maps URL"
|
|
516
|
+
};
|
|
517
|
+
function toRows(leads, fields = ALL_FIELDS) {
|
|
518
|
+
return leads.map((lead) => {
|
|
519
|
+
const row = {};
|
|
520
|
+
for (const f of fields) {
|
|
521
|
+
const v = lead[f];
|
|
522
|
+
row[HEADERS[f]] = v === void 0 || v === null ? "" : v;
|
|
523
|
+
}
|
|
524
|
+
return row;
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
function serialize(leads, format, fields = ALL_FIELDS, opts = {}) {
|
|
528
|
+
if (format === "json" || format === "ndjson") {
|
|
529
|
+
const picked = leads.map((lead) => {
|
|
530
|
+
const o = {};
|
|
531
|
+
for (const f of fields) if (lead[f] !== void 0) o[f] = lead[f];
|
|
532
|
+
return o;
|
|
533
|
+
});
|
|
534
|
+
const data = format === "ndjson" ? picked.map((o) => JSON.stringify(o)).join("\n") + (picked.length ? "\n" : "") : JSON.stringify(picked, null, 2);
|
|
535
|
+
return { data, binary: false };
|
|
536
|
+
}
|
|
537
|
+
const rows = toRows(leads, fields);
|
|
538
|
+
if (format === "csv") {
|
|
539
|
+
const csvRows = rows;
|
|
540
|
+
return { data: csvStringify(csvRows, { escapeFormulas: true }), binary: false };
|
|
541
|
+
}
|
|
542
|
+
const columns = fields.map((f) => {
|
|
543
|
+
const header = HEADERS[f];
|
|
544
|
+
const widest = rows.reduce((m, r) => Math.max(m, String(r[header] ?? "").length), header.length);
|
|
545
|
+
return { header, width: Math.min(60, Math.max(8, widest + 2)) };
|
|
546
|
+
});
|
|
547
|
+
return {
|
|
548
|
+
data: jsonToXlsx(rows, { sheetName: opts.sheetName ?? "Leads", columns }),
|
|
549
|
+
binary: true
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
function computeStats(leads) {
|
|
553
|
+
const socialKeys = ["facebook", "instagram", "whatsapp", "linkedin", "twitter", "youtube", "tiktok", "telegram"];
|
|
554
|
+
const ratings = leads.map((l) => l.rating).filter((r) => typeof r === "number");
|
|
555
|
+
const stats = {
|
|
556
|
+
total: leads.length,
|
|
557
|
+
withPhone: leads.filter((l) => l.phone).length,
|
|
558
|
+
withWebsite: leads.filter((l) => l.website).length,
|
|
559
|
+
withEmail: leads.filter((l) => l.email).length,
|
|
560
|
+
withValidEmail: leads.filter((l) => l.emailStatus === "valid").length,
|
|
561
|
+
withSocial: leads.filter((l) => socialKeys.some((k) => l[k])).length
|
|
562
|
+
};
|
|
563
|
+
if (ratings.length) stats.avgRating = Math.round(ratings.reduce((a, b) => a + b, 0) / ratings.length * 10) / 10;
|
|
564
|
+
return stats;
|
|
565
|
+
}
|
|
566
|
+
var FIELD_BY_KEY = (() => {
|
|
567
|
+
const m = /* @__PURE__ */ new Map();
|
|
568
|
+
for (const f of ALL_FIELDS) {
|
|
569
|
+
m.set(f.toLowerCase(), f);
|
|
570
|
+
m.set(HEADERS[f].toLowerCase(), f);
|
|
571
|
+
}
|
|
572
|
+
return m;
|
|
573
|
+
})();
|
|
574
|
+
var NUMERIC_FIELDS = /* @__PURE__ */ new Set(["rating", "reviews", "latitude", "longitude", "distanceKm"]);
|
|
575
|
+
function rowsToLeads(rows) {
|
|
576
|
+
return rows.map((row) => {
|
|
577
|
+
const lead = {};
|
|
578
|
+
for (const [key, raw] of Object.entries(row)) {
|
|
579
|
+
const field = FIELD_BY_KEY.get(key.trim().toLowerCase());
|
|
580
|
+
if (!field || raw === null || raw === void 0 || raw === "") continue;
|
|
581
|
+
if (NUMERIC_FIELDS.has(field)) {
|
|
582
|
+
const n = typeof raw === "number" ? raw : parseFloat(String(raw));
|
|
583
|
+
if (Number.isFinite(n)) lead[field] = n;
|
|
584
|
+
} else {
|
|
585
|
+
lead[field] = String(raw);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
return lead;
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
|
|
390
592
|
// src/scrape.ts
|
|
391
593
|
async function pool(items, n, fn, signal) {
|
|
392
594
|
let next = 0;
|
|
@@ -436,17 +638,19 @@ function parseLatLng(url) {
|
|
|
436
638
|
if (at) return { latitude: parseFloat(at[1]), longitude: parseFloat(at[2]) };
|
|
437
639
|
return {};
|
|
438
640
|
}
|
|
439
|
-
async function launchBrowser(headless) {
|
|
641
|
+
async function launchBrowser(headless, proxy) {
|
|
440
642
|
let lastErr;
|
|
643
|
+
const base = { headless };
|
|
644
|
+
if (proxy) base.proxy = { server: proxy };
|
|
441
645
|
for (const channel of ["chrome", "msedge"]) {
|
|
442
646
|
try {
|
|
443
|
-
return await chromium.launch({
|
|
647
|
+
return await chromium.launch({ ...base, channel });
|
|
444
648
|
} catch (e) {
|
|
445
649
|
lastErr = e;
|
|
446
650
|
}
|
|
447
651
|
}
|
|
448
652
|
try {
|
|
449
|
-
return await chromium.launch(
|
|
653
|
+
return await chromium.launch(base);
|
|
450
654
|
} catch (e) {
|
|
451
655
|
lastErr = e;
|
|
452
656
|
}
|
|
@@ -569,17 +773,26 @@ async function scrapeLeads(opts) {
|
|
|
569
773
|
const fields = new Set(normalizeFields(opts.fields ?? DEFAULT_FIELDS));
|
|
570
774
|
const delayMs = Math.max(0, Math.trunc(opts.delayMs ?? 700));
|
|
571
775
|
const headless = opts.headless ?? false;
|
|
776
|
+
const retries = Math.max(0, Math.trunc(opts.retries ?? 1));
|
|
572
777
|
const onProgress = opts.onProgress;
|
|
778
|
+
const onLead = opts.onLead;
|
|
573
779
|
const signal = opts.signal;
|
|
574
780
|
const startedAt = Date.now();
|
|
575
781
|
const overBudget = () => opts.maxMs !== void 0 && Date.now() - startedAt > opts.maxMs;
|
|
576
|
-
const
|
|
782
|
+
const pauseMs = () => opts.jitter ? Math.round(delayMs * (0.6 + Math.random() * 0.8)) : delayMs;
|
|
783
|
+
const wantVerify = Boolean(opts.verifyEmails) || Boolean(opts.filters?.hasValidEmail) || fields.has("emailStatus");
|
|
784
|
+
const wantEnrich = Boolean(opts.enrich) || ENRICHED_FIELDS.some((f) => fields.has(f)) || Boolean(opts.filters?.hasEmail) || wantVerify;
|
|
785
|
+
const near = opts.near;
|
|
577
786
|
const collect = new Set(fields);
|
|
578
787
|
if (wantEnrich) collect.add("website");
|
|
788
|
+
if (near) {
|
|
789
|
+
collect.add("latitude");
|
|
790
|
+
collect.add("longitude");
|
|
791
|
+
}
|
|
579
792
|
const detailOnly = ["name", "mapsUrl"];
|
|
580
793
|
const wantDetails = (opts.details ?? true) && ([...collect].some((f) => !detailOnly.includes(f)) || wantEnrich);
|
|
581
794
|
onProgress?.(`searching Google Maps for "${query}"\u2026`);
|
|
582
|
-
const browser = await launchBrowser(headless);
|
|
795
|
+
const browser = await launchBrowser(headless, opts.proxy);
|
|
583
796
|
try {
|
|
584
797
|
const locale = opts.locale ?? "en-US";
|
|
585
798
|
const context = await browser.newContext({
|
|
@@ -589,6 +802,7 @@ async function scrapeLeads(opts) {
|
|
|
589
802
|
const page = await context.newPage();
|
|
590
803
|
const urlOpts = { hl: locale };
|
|
591
804
|
if (opts.region) urlOpts.gl = opts.region;
|
|
805
|
+
if (near) urlOpts.center = { lat: near.lat, lng: near.lng, zoom: zoomForRadius(opts.radiusM ?? 2e3, near.lat) };
|
|
592
806
|
await page.goto(mapsSearchUrl(query, urlOpts), { waitUntil: "domcontentloaded", timeout: 45e3 });
|
|
593
807
|
await dismissConsent(page);
|
|
594
808
|
await loadResults(page, limit, delayMs, onProgress, signal);
|
|
@@ -617,25 +831,47 @@ async function scrapeLeads(opts) {
|
|
|
617
831
|
}
|
|
618
832
|
return lead;
|
|
619
833
|
});
|
|
834
|
+
for (const lead of leads) {
|
|
835
|
+
try {
|
|
836
|
+
onLead?.(lead);
|
|
837
|
+
} catch {
|
|
838
|
+
}
|
|
839
|
+
}
|
|
620
840
|
} else {
|
|
621
841
|
for (let i = 0; i < cards.length; i++) {
|
|
622
842
|
if (signal?.aborted || overBudget()) break;
|
|
623
843
|
const card = cards[i];
|
|
624
844
|
onProgress?.(`reading ${i + 1}/${cards.length}: ${card.name ?? "listing"}\u2026`);
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
845
|
+
let lead;
|
|
846
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
847
|
+
try {
|
|
848
|
+
await page.goto(card.href, { waitUntil: "domcontentloaded", timeout: 3e4 });
|
|
849
|
+
await page.locator("h1.DUwDvf").first().waitFor({ timeout: 8e3 }).catch(() => {
|
|
850
|
+
});
|
|
851
|
+
lead = await extractDetail(page, collect, card.name);
|
|
852
|
+
break;
|
|
853
|
+
} catch {
|
|
854
|
+
if (attempt < retries && !signal?.aborted) {
|
|
855
|
+
onProgress?.(` retrying ${i + 1}/${cards.length} (attempt ${attempt + 2})\u2026`);
|
|
856
|
+
if (delayMs) await page.waitForTimeout(pauseMs());
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
if (!lead) {
|
|
631
861
|
if (card.name && (collect.has("name") || collect.has("mapsUrl"))) {
|
|
632
|
-
|
|
633
|
-
if (collect.has("name"))
|
|
634
|
-
if (collect.has("mapsUrl"))
|
|
635
|
-
|
|
862
|
+
lead = {};
|
|
863
|
+
if (collect.has("name")) lead.name = card.name;
|
|
864
|
+
if (collect.has("mapsUrl")) lead.mapsUrl = card.href;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
if (lead) {
|
|
868
|
+
leads.push(lead);
|
|
869
|
+
try {
|
|
870
|
+
onLead?.(lead);
|
|
871
|
+
} catch {
|
|
636
872
|
}
|
|
637
873
|
}
|
|
638
|
-
if (delayMs) await page.waitForTimeout(
|
|
874
|
+
if (delayMs) await page.waitForTimeout(pauseMs());
|
|
639
875
|
}
|
|
640
876
|
}
|
|
641
877
|
onProgress?.(`collected ${leads.length} listing${leads.length === 1 ? "" : "s"}.`);
|
|
@@ -644,6 +880,19 @@ async function scrapeLeads(opts) {
|
|
|
644
880
|
if (lead.website) lead.website = cleanWebsite(lead.website);
|
|
645
881
|
}
|
|
646
882
|
}
|
|
883
|
+
if (near) {
|
|
884
|
+
for (const lead of leads) {
|
|
885
|
+
if (typeof lead.latitude === "number" && typeof lead.longitude === "number") {
|
|
886
|
+
const m = haversineMeters(near, { lat: lead.latitude, lng: lead.longitude });
|
|
887
|
+
lead.distanceKm = Math.round(m / 1e3 * 100) / 100;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
if (opts.radiusM !== void 0) {
|
|
891
|
+
const r = opts.radiusM;
|
|
892
|
+
leads = leads.filter((l) => l.distanceKm !== void 0 && l.distanceKm * 1e3 <= r);
|
|
893
|
+
onProgress?.(`within ${Math.round(r)} m: ${leads.length} listing${leads.length === 1 ? "" : "s"}.`);
|
|
894
|
+
}
|
|
895
|
+
}
|
|
647
896
|
leads = dedupeLeads(leads, opts.dedupe ?? "website");
|
|
648
897
|
if (wantEnrich) {
|
|
649
898
|
const socialFields = ENRICHED_FIELDS.filter((f) => f !== "email");
|
|
@@ -668,6 +917,14 @@ async function scrapeLeads(opts) {
|
|
|
668
917
|
signal
|
|
669
918
|
);
|
|
670
919
|
}
|
|
920
|
+
if (wantVerify && !signal?.aborted) {
|
|
921
|
+
onProgress?.("verifying email domains\u2026");
|
|
922
|
+
const vOpts = {
|
|
923
|
+
concurrency: Math.max(1, Math.trunc(opts.concurrency ?? 5))
|
|
924
|
+
};
|
|
925
|
+
if (onProgress) vOpts.onProgress = (m) => onProgress(m);
|
|
926
|
+
await verifyEmails(leads, vOpts);
|
|
927
|
+
}
|
|
671
928
|
if (opts.country && fields.has("phone")) {
|
|
672
929
|
for (const lead of leads) {
|
|
673
930
|
if (lead.phone) lead.phone = normalizePhone(lead.phone, opts.country);
|
|
@@ -675,6 +932,17 @@ async function scrapeLeads(opts) {
|
|
|
675
932
|
}
|
|
676
933
|
if (opts.filters) leads = filterLeads(leads, opts.filters);
|
|
677
934
|
if (opts.sort) leads = sortLeads(leads, opts.sort, opts.sortDir);
|
|
935
|
+
if (wantVerify && !fields.has("emailStatus")) for (const l of leads) delete l.emailStatus;
|
|
936
|
+
if (near) {
|
|
937
|
+
const keepLat = fields.has("latitude");
|
|
938
|
+
const keepLng = fields.has("longitude");
|
|
939
|
+
const keepDist = fields.has("distanceKm");
|
|
940
|
+
for (const l of leads) {
|
|
941
|
+
if (!keepLat) delete l.latitude;
|
|
942
|
+
if (!keepLng) delete l.longitude;
|
|
943
|
+
if (!keepDist) delete l.distanceKm;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
678
946
|
onProgress?.(`collected ${leads.length} lead${leads.length === 1 ? "" : "s"}.`);
|
|
679
947
|
return leads;
|
|
680
948
|
} finally {
|
|
@@ -682,6 +950,11 @@ async function scrapeLeads(opts) {
|
|
|
682
950
|
});
|
|
683
951
|
}
|
|
684
952
|
}
|
|
953
|
+
async function searchLeadsDetailed(opts) {
|
|
954
|
+
const started = Date.now();
|
|
955
|
+
const leads = await scrapeLeads(opts);
|
|
956
|
+
return { leads, stats: computeStats(leads), elapsedMs: Date.now() - started };
|
|
957
|
+
}
|
|
685
958
|
|
|
686
959
|
// src/batch.ts
|
|
687
960
|
async function searchLeadsBatch(queries, opts = {}) {
|
|
@@ -746,69 +1019,6 @@ async function searchLeadsMulti(opts) {
|
|
|
746
1019
|
return searchLeadsBatch(queries, opts);
|
|
747
1020
|
}
|
|
748
1021
|
|
|
749
|
-
// src/export.ts
|
|
750
|
-
import { stringify as csvStringify } from "@lacspace/csv";
|
|
751
|
-
import { jsonToXlsx } from "@lacspace/xlsx";
|
|
752
|
-
var HEADERS = {
|
|
753
|
-
name: "Name",
|
|
754
|
-
category: "Category",
|
|
755
|
-
rating: "Rating",
|
|
756
|
-
reviews: "Reviews",
|
|
757
|
-
priceLevel: "Price",
|
|
758
|
-
address: "Address",
|
|
759
|
-
phone: "Phone",
|
|
760
|
-
website: "Website",
|
|
761
|
-
email: "Email",
|
|
762
|
-
facebook: "Facebook",
|
|
763
|
-
instagram: "Instagram",
|
|
764
|
-
whatsapp: "WhatsApp",
|
|
765
|
-
linkedin: "LinkedIn",
|
|
766
|
-
twitter: "Twitter/X",
|
|
767
|
-
youtube: "YouTube",
|
|
768
|
-
tiktok: "TikTok",
|
|
769
|
-
telegram: "Telegram",
|
|
770
|
-
plusCode: "Plus Code",
|
|
771
|
-
latitude: "Latitude",
|
|
772
|
-
longitude: "Longitude",
|
|
773
|
-
hours: "Hours",
|
|
774
|
-
mapsUrl: "Maps URL"
|
|
775
|
-
};
|
|
776
|
-
function toRows(leads, fields = ALL_FIELDS) {
|
|
777
|
-
return leads.map((lead) => {
|
|
778
|
-
const row = {};
|
|
779
|
-
for (const f of fields) {
|
|
780
|
-
const v = lead[f];
|
|
781
|
-
row[HEADERS[f]] = v === void 0 || v === null ? "" : v;
|
|
782
|
-
}
|
|
783
|
-
return row;
|
|
784
|
-
});
|
|
785
|
-
}
|
|
786
|
-
function serialize(leads, format, fields = ALL_FIELDS, opts = {}) {
|
|
787
|
-
if (format === "json" || format === "ndjson") {
|
|
788
|
-
const picked = leads.map((lead) => {
|
|
789
|
-
const o = {};
|
|
790
|
-
for (const f of fields) if (lead[f] !== void 0) o[f] = lead[f];
|
|
791
|
-
return o;
|
|
792
|
-
});
|
|
793
|
-
const data = format === "ndjson" ? picked.map((o) => JSON.stringify(o)).join("\n") + (picked.length ? "\n" : "") : JSON.stringify(picked, null, 2);
|
|
794
|
-
return { data, binary: false };
|
|
795
|
-
}
|
|
796
|
-
const rows = toRows(leads, fields);
|
|
797
|
-
if (format === "csv") {
|
|
798
|
-
const csvRows = rows;
|
|
799
|
-
return { data: csvStringify(csvRows, { escapeFormulas: true }), binary: false };
|
|
800
|
-
}
|
|
801
|
-
const columns = fields.map((f) => {
|
|
802
|
-
const header = HEADERS[f];
|
|
803
|
-
const widest = rows.reduce((m, r) => Math.max(m, String(r[header] ?? "").length), header.length);
|
|
804
|
-
return { header, width: Math.min(60, Math.max(8, widest + 2)) };
|
|
805
|
-
});
|
|
806
|
-
return {
|
|
807
|
-
data: jsonToXlsx(rows, { sheetName: opts.sheetName ?? "Leads", columns }),
|
|
808
|
-
binary: true
|
|
809
|
-
};
|
|
810
|
-
}
|
|
811
|
-
|
|
812
1022
|
// src/convert.ts
|
|
813
1023
|
import { readFile, writeFile } from "fs/promises";
|
|
814
1024
|
import { extname, resolve } from "path";
|
|
@@ -893,29 +1103,40 @@ export {
|
|
|
893
1103
|
cleanWebsite,
|
|
894
1104
|
columnsOf,
|
|
895
1105
|
composeQuery,
|
|
1106
|
+
computeStats,
|
|
896
1107
|
convertFile,
|
|
897
1108
|
dedupeLeads,
|
|
898
1109
|
defaultFilename,
|
|
899
1110
|
detectFormat,
|
|
1111
|
+
emailDomain,
|
|
1112
|
+
emailFormatValid,
|
|
900
1113
|
enrichContacts,
|
|
901
1114
|
expandQueries,
|
|
902
1115
|
extractEmails,
|
|
903
1116
|
extractSocials,
|
|
904
1117
|
filterLeads,
|
|
1118
|
+
haversineMeters,
|
|
905
1119
|
mapsSearchUrl,
|
|
906
1120
|
normalizeFields,
|
|
907
1121
|
normalizePhone,
|
|
1122
|
+
parseDistance,
|
|
908
1123
|
parseLatLng,
|
|
1124
|
+
parseLatLngPair,
|
|
909
1125
|
parseRating,
|
|
910
1126
|
parseReviewCount,
|
|
911
1127
|
readRows,
|
|
912
1128
|
resolvePreset,
|
|
1129
|
+
rowsToLeads,
|
|
913
1130
|
scrapeLeads,
|
|
914
1131
|
scrapeLeads as searchLeads,
|
|
915
1132
|
searchLeadsBatch,
|
|
1133
|
+
searchLeadsDetailed,
|
|
916
1134
|
searchLeadsMulti,
|
|
917
1135
|
serialize,
|
|
918
1136
|
serializeRows,
|
|
919
1137
|
sortLeads,
|
|
920
|
-
toRows
|
|
1138
|
+
toRows,
|
|
1139
|
+
verifyEmail,
|
|
1140
|
+
verifyEmails,
|
|
1141
|
+
zoomForRadius
|
|
921
1142
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lacspace-leads",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Free, open-source local-business lead finder — name a city,
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "Free, open-source local-business lead finder — name a city/area/business type, or search by coordinates + radius, and it drives a real browser over Google Maps to collect names, phones, websites, ratings, addresses, emails and social links, then exports to JSON, NDJSON, CSV or Excel. Sweep multiple areas at once, verify emails by MX, normalise phones to E.164, accumulate into a master file, and enrich in parallel through an optional proxy. No API keys.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"lacspace-leads": "dist/cli.js"
|
|
@@ -33,9 +33,13 @@
|
|
|
33
33
|
"google-maps",
|
|
34
34
|
"scraper",
|
|
35
35
|
"local-business",
|
|
36
|
+
"radius-search",
|
|
37
|
+
"geosearch",
|
|
36
38
|
"prospecting",
|
|
37
39
|
"b2b",
|
|
38
40
|
"email-finder",
|
|
41
|
+
"email-verification",
|
|
42
|
+
"mx-lookup",
|
|
39
43
|
"csv",
|
|
40
44
|
"excel",
|
|
41
45
|
"ndjson",
|