1
2
3
4import { Actor } from 'apify';
5await Actor.init();
6const input = (await Actor.getInput()) ?? {};
7const { days = 7, new_only = true, min_revenue_usd = 0, sectors = [], exchanges = [], max_items = 500 } = input;
8const UA = 'apify-new-executive-leads/0.2 (data source: SEC EDGAR; served by POPS4 Filing Boards)';
9const H = { headers: { 'user-agent': UA, accept: 'application/json' } };
10const SITE = 'https://www.pops4.com';
11const UPDATE = 'Live feed read from SEC EDGAR every 2 minutes during EDGAR hours (Mon-Fri 10:00-23:59 UTC and 00:00-03:59 UTC Tue-Sat); enrichment board rebuilt from the SEC index at 13:15 UTC on the next business day and hourly after';
12async function getJson(url) { const r = await fetch(url, H); if (!r.ok) throw new Error(`HTTP ${r.status} ${url}`); return r.json(); }
13const NAME = /\b(?:Mr\.|Ms\.|Mrs\.|Dr\.)\s+([A-Z][A-Za-z'’\-]+(?:\s+[A-Z][A-Za-z'’\-]+){0,3})|\b([A-Z][a-z]+(?:\s+[A-Z]\.)?\s+[A-Z][A-Za-z'’\-]+)(?=,?\s+(?:as|to serve|has been|was|will|most recently|who|the Company))/g;
14function names(text) { const out = new Set(); let m; while ((m = NAME.exec(text || ''))) out.add((m[1] || m[2]).trim()); return [...out].slice(0, 4).join('; '); }
15
16const state = await Actor.openKeyValueStore('pops4-new-executive-leads-state');
17const prev = (await state.getValue('WATERMARK')) ?? null;
18const since = new_only && prev?.sec_accepted ? prev.sec_accepted : new Date(Date.now() - days * 86400000).toISOString();
19const seen = new Set(new_only && prev?.ids ? prev.ids : []);
20
21let events = []; let url = `${SITE}/boards/now/changes.json?since=${encodeURIComponent(since)}`;
22for (let page = 0; page < 20; page++) { const d = await getJson(url); events.push(...(d.events ?? [])); if (!d.has_more || !d.next_cursor) break; url = `${SITE}/boards/now/changes.json?cursor=${encodeURIComponent(d.next_cursor)}`; }
23const status = await getJson(`${SITE}/boards/now/status.json`).catch(() => ({}));
24const moves = events.filter((e) => e.form === '8-K' && (e.items ?? []).includes('5.02'));
25
26const board = await getJson(`${SITE}/boards/cmo-moves.json?limit=2600`).catch(() => ({ rows: [] }));
27const byAcc = new Map((board.rows ?? []).map((r) => [r.id, r]));
28const cardCache = new Map(); let cardCalls = 0;
29async function card(ticker) { if (!ticker) return null; if (cardCache.has(ticker)) return cardCache.get(ticker); if (cardCalls >= 150) return null; cardCalls += 1; const c = await getJson(`${SITE}/boards/company/${encodeURIComponent(ticker)}.json`).catch(() => null); cardCache.set(ticker, c); return c; }
30let pushed = 0; let newest = prev?.sec_accepted ?? ''; const ids = [];
31const uniq = new Map(); for (const e of moves) { const cur = uniq.get(e.accession); if (!cur || (e.item === '5.02' && cur.item !== '5.02')) uniq.set(e.accession, e); }
32for (const e of [...uniq.values()].sort((a, b) => String(b.sec_accepted).localeCompare(String(a.sec_accepted)))) {
33 if (pushed >= max_items) break;
34 if (seen.has(e.accession)) continue;
35 const b = byAcc.get(e.accession); const c = b ? null : await card(e.ticker);
36 const row = {
37 company: e.company, ticker: e.ticker ?? null, cik: e.cik, exchange: b?.exchange ?? null, sector: b?.sector ?? c?.peers?.sicDescription ?? null, filer_category: b?.filerCategory ?? null,
38 revenue_usd: b?.revenue ?? c?.metrics?.revenue ?? null, revenue_fiscal_year: b?.revenueFY ?? (c?.metrics?.fiscalYearEnd ? c.metrics.fiscalYearEnd.slice(0, 4) : null), city: b?.city ?? null, state: b?.hq ?? (c?.where?.headquarters ? c.where.headquarters.split(',').slice(-2, -1)[0]?.trim() : null) ?? null,
39 event_date: b?.event ?? null, filed: b?.filed ?? String(e.sec_accepted).slice(0, 10), items: (e.items ?? []).join('; '), what_the_filing_says: b?.quote ?? e.what ?? '', people_named: names(b?.quote),
40 sec_accepted: e.sec_accepted, seen_by_feed_at: e.posted ?? e.available_at ?? null, minutes_after_sec: e.minutes_after_sec ?? null,
41 filing_url: e.filing, company_card: e.card ?? null, company_filings_url: b?.profile ?? `https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=${e.cik}&type=8-K`,
42 data_as_of: new Date().toISOString(), feed_last_sweep: status?.last_sweep?.at ?? null, update_frequency: UPDATE, data_source: "SEC EDGAR, the company's own 8-K (Item 5.02)", license: 'Served by POPS4 Filing Boards, CC BY 4.0',
43 };
44 if (min_revenue_usd && (row.revenue_usd ?? 0) < min_revenue_usd) continue;
45 if (sectors.length && !sectors.some((s) => (row.sector ?? '').toLowerCase().includes(String(s).toLowerCase()))) continue;
46 if (exchanges.length && !exchanges.map((x) => String(x).toLowerCase()).includes(String(row.exchange ?? '').toLowerCase())) continue;
47 await Actor.pushData(row); pushed += 1; ids.push(e.accession); if (String(e.sec_accepted) > newest) newest = String(e.sec_accepted);
48 await Actor.charge({ eventName: 'appointment_or_departure' }).catch(() => {});
49}
50if (new_only) await state.setValue('WATERMARK', { sec_accepted: newest || since, ids: [...new Set([...ids, ...[...seen].slice(0, 2000)])].slice(0, 3000), updated: new Date().toISOString() });
51await Actor.setValue('RUN_SUMMARY', { rows: pushed, since, newest_sec_accepted: newest || null, events_scanned: moves.length, feed_last_sweep: status?.last_sweep?.at ?? null, new_only });
52console.log(`Done: ${pushed} rows since ${since}; feed last sweep ${status?.last_sweep?.at ?? '?'}; new_only=${new_only}.`);
53await Actor.exit();