|
| 1 | +import { cli, Strategy } from '@jackwener/opencli/registry'; |
| 2 | +import { CommandExecutionError } from '@jackwener/opencli/errors'; |
| 3 | +import { |
| 4 | + LINKEDIN_DOMAIN, |
| 5 | + assertLinkedInAuthenticated, |
| 6 | + normalizeWhitespace, |
| 7 | + unwrapEvaluateResult, |
| 8 | +} from './shared.js'; |
| 9 | + |
| 10 | +const SLUG_RE = /^[A-Za-z0-9%._-]+$/; |
| 11 | +const COMPANY_URL_RE = /^\/company\/([^/?#]+)/; |
| 12 | +const LINKEDIN_COMPANY_HOSTS = new Set(['linkedin.com', LINKEDIN_DOMAIN]); |
| 13 | + |
| 14 | +// Accept a bare universal name (`nvidia`), a `/company/<slug>` path, or a full |
| 15 | +// company URL, and return the canonical about-page URL. |
| 16 | +function normalizeCompanyUrl(value) { |
| 17 | + const raw = normalizeWhitespace(value || ''); |
| 18 | + if (!raw) { |
| 19 | + throw new CommandExecutionError('LinkedIn company requires a company universal name or URL'); |
| 20 | + } |
| 21 | + let slug = raw; |
| 22 | + if (/^https?:\/\//i.test(raw) || raw.startsWith('/company/')) { |
| 23 | + let parsed; |
| 24 | + try { |
| 25 | + parsed = raw.startsWith('/') ? new URL(raw, `https://${LINKEDIN_DOMAIN}`) : new URL(raw); |
| 26 | + } catch { |
| 27 | + throw new CommandExecutionError(`LinkedIn company received a malformed URL: ${raw}`); |
| 28 | + } |
| 29 | + if (parsed.protocol !== 'https:' || parsed.username || parsed.password || parsed.port || !LINKEDIN_COMPANY_HOSTS.has(parsed.hostname.toLowerCase())) { |
| 30 | + throw new CommandExecutionError('LinkedIn company URL must point to linkedin.com'); |
| 31 | + } |
| 32 | + const m = parsed.pathname.match(COMPANY_URL_RE); |
| 33 | + if (!m) throw new CommandExecutionError('LinkedIn company URL must look like /company/<name>'); |
| 34 | + try { |
| 35 | + slug = decodeURIComponent(m[1]); |
| 36 | + } catch { |
| 37 | + throw new CommandExecutionError(`LinkedIn company URL has a malformed company slug: ${m[1]}`); |
| 38 | + } |
| 39 | + } |
| 40 | + if (!SLUG_RE.test(slug)) { |
| 41 | + throw new CommandExecutionError(`LinkedIn company name has unexpected characters: ${slug}`); |
| 42 | + } |
| 43 | + return `https://www.linkedin.com/company/${encodeURIComponent(slug)}/about/`; |
| 44 | +} |
| 45 | + |
| 46 | +function buildCompanyExtractionScript() { |
| 47 | + return String.raw`(() => { |
| 48 | + const clean = (s) => String(s || '').replace(/[ ]+/g, ' ').replace(/\s+/g, ' ').trim(); |
| 49 | + const facts = {}; |
| 50 | + for (const dt of Array.from(document.querySelectorAll('dt'))) { |
| 51 | + const key = clean(dt.innerText || dt.textContent || '').toLowerCase().replace(/:$/, ''); |
| 52 | + const dd = dt.nextElementSibling; |
| 53 | + const val = dd ? clean(dd.innerText || dd.textContent || '') : ''; |
| 54 | + if (key && val && !(key in facts)) facts[key] = val; |
| 55 | + } |
| 56 | + const name = clean((document.querySelector('main h1') || document.querySelector('h1'))?.innerText || ''); |
| 57 | + const bodyText = clean(document.body ? (document.body.innerText || '') : ''); |
| 58 | + const followersMatch = bodyText.match(/([\d,]+)\s+followers/i); |
| 59 | + const aboutHeading = Array.from(document.querySelectorAll('main h2, section h2')).find((el) => /^About$|^Overview$/i.test(clean(el.innerText || ''))); |
| 60 | + const aboutSection = aboutHeading ? aboutHeading.closest('section') : null; |
| 61 | + const about = aboutSection ? clean((aboutSection.innerText || '').replace(/^About\s*/i, '').replace(/^Overview\s*/i, '')) : ''; |
| 62 | + return { |
| 63 | + url: window.location.href, |
| 64 | + name, |
| 65 | + industry: facts['industry'] || '', |
| 66 | + size: facts['company size'] || '', |
| 67 | + headquarters: facts['headquarters'] || '', |
| 68 | + founded: facts['founded'] || '', |
| 69 | + website: facts['website'] || '', |
| 70 | + specialties: facts['specialties'] || '', |
| 71 | + followers: followersMatch ? followersMatch[1].replace(/,/g, '') : '', |
| 72 | + about: about.slice(0, 2000), |
| 73 | + }; |
| 74 | + })()`; |
| 75 | +} |
| 76 | + |
| 77 | +function normalizeCompanyOutputUrl(value, fallbackUrl) { |
| 78 | + const raw = normalizeWhitespace(value || fallbackUrl); |
| 79 | + let parsed; |
| 80 | + try { |
| 81 | + parsed = new URL(raw, `https://${LINKEDIN_DOMAIN}`); |
| 82 | + } catch { |
| 83 | + throw new CommandExecutionError('LinkedIn company extraction returned a malformed current URL'); |
| 84 | + } |
| 85 | + if (parsed.protocol !== 'https:' || parsed.username || parsed.password || parsed.port || !LINKEDIN_COMPANY_HOSTS.has(parsed.hostname.toLowerCase())) { |
| 86 | + throw new CommandExecutionError('LinkedIn company extraction ended on a non-LinkedIn page'); |
| 87 | + } |
| 88 | + const match = parsed.pathname.match(COMPANY_URL_RE); |
| 89 | + if (!match?.[1]) { |
| 90 | + throw new CommandExecutionError('LinkedIn company extraction ended outside a company page'); |
| 91 | + } |
| 92 | + let slug; |
| 93 | + try { |
| 94 | + slug = decodeURIComponent(match[1]); |
| 95 | + } catch { |
| 96 | + throw new CommandExecutionError('LinkedIn company extraction returned a malformed company slug'); |
| 97 | + } |
| 98 | + return `https://${LINKEDIN_DOMAIN}/company/${encodeURIComponent(slug)}/about/`; |
| 99 | +} |
| 100 | + |
| 101 | +function normalizeCompanyInfo(info, targetUrl) { |
| 102 | + if (!info || typeof info !== 'object' || Array.isArray(info)) { |
| 103 | + throw new CommandExecutionError('LinkedIn company extraction returned a malformed payload'); |
| 104 | + } |
| 105 | + if (!info.name) { |
| 106 | + throw new CommandExecutionError('LinkedIn company page rendered but no company name was found (layout drift or company not found)'); |
| 107 | + } |
| 108 | + let followers = 0; |
| 109 | + if (info.followers) { |
| 110 | + followers = Number(info.followers); |
| 111 | + if (!Number.isFinite(followers)) { |
| 112 | + throw new CommandExecutionError('LinkedIn company extraction returned a malformed followers count'); |
| 113 | + } |
| 114 | + } |
| 115 | + return { |
| 116 | + name: String(info.name), |
| 117 | + industry: String(info.industry || ''), |
| 118 | + size: String(info.size || ''), |
| 119 | + headquarters: String(info.headquarters || ''), |
| 120 | + founded: String(info.founded || ''), |
| 121 | + website: String(info.website || ''), |
| 122 | + specialties: String(info.specialties || ''), |
| 123 | + followers, |
| 124 | + about: String(info.about || ''), |
| 125 | + url: normalizeCompanyOutputUrl(info.url, targetUrl), |
| 126 | + }; |
| 127 | +} |
| 128 | + |
| 129 | +cli({ |
| 130 | + site: 'linkedin', |
| 131 | + name: 'company', |
| 132 | + access: 'read', |
| 133 | + description: 'Read a LinkedIn company page: industry, size, HQ, founded, website, followers, about', |
| 134 | + domain: LINKEDIN_DOMAIN, |
| 135 | + strategy: Strategy.COOKIE, |
| 136 | + browser: true, |
| 137 | + args: [ |
| 138 | + { name: 'company', type: 'string', required: true, positional: true, help: 'Company universal name (nvidia), /company/<name> path, or full URL' }, |
| 139 | + ], |
| 140 | + columns: ['name', 'industry', 'size', 'headquarters', 'founded', 'website', 'specialties', 'followers', 'about', 'url'], |
| 141 | + func: async (page, kwargs) => { |
| 142 | + const targetUrl = normalizeCompanyUrl(kwargs.company); |
| 143 | + await page.goto(targetUrl); |
| 144 | + await page.wait(2); |
| 145 | + await assertLinkedInAuthenticated(page, 'linkedin company'); |
| 146 | + const info = unwrapEvaluateResult(await page.evaluate(buildCompanyExtractionScript())); |
| 147 | + return [normalizeCompanyInfo(info, targetUrl)]; |
| 148 | + }, |
| 149 | +}); |
| 150 | + |
| 151 | +export const __test__ = { normalizeCompanyUrl, normalizeCompanyInfo, buildCompanyExtractionScript }; |
0 commit comments