Files
linkedin-api/classes/Company.js
T

91 lines
3.9 KiB
JavaScript
Raw Normal View History

2024-05-30 15:35:16 -04:00
import axios from "axios";
import * as cheerio from 'cheerio';
2024-05-10 13:27:48 -07:00
import linkedInAPIClass from "../index.js";
2024-05-09 08:10:20 -07:00
import { LinkedInProfile } from "./Profile.js";
export class Company {
2024-05-10 13:27:48 -07:00
/** @type {linkedInAPIClass} */
2024-05-09 08:10:20 -07:00
#APIRef;
/**
* gets the employees of a company
* @param {Number} [limit=Infinity] to make things simpler I used increments of 50, so the limit is essentially ceiled to the nearest multiple of 50
* @param {Boolean} [raw=false] whether or not you want the raw JSON returned
* @returns {Promise<JSON[] | LinkedInProfile[]>}
* @note This function skips over any employees who's profile is marked as private
*/
async getEmployees(limit = Infinity, raw = false) {
2024-05-14 08:08:24 -07:00
const employeesInit = await this.#APIRef._makeReq(`variables=(start:0,origin:FACETED_SEARCH,query:(flagshipSearchIntent:ORGANIZATIONS_PEOPLE_ALUMNI,queryParameters:List((key:currentCompany,value:List(${this.entityNum})),(key:resultType,value:List(ORGANIZATION_ALUMNI))),includeFiltersInResponse:true),count:1)`);
2024-05-09 08:10:20 -07:00
const numEmp = employeesInit.data.data.searchDashClustersByAll.paging.total,
2024-05-14 08:08:24 -07:00
empAll = [];
2024-05-09 08:10:20 -07:00
// since the max cap is 50, we need to iterate until it's over 50
for (let i = 0; i < numEmp; i += 50) {
2024-05-14 08:08:24 -07:00
if (empAll.length >= limit || i >= numEmp) break;
2024-05-09 08:10:20 -07:00
2024-05-14 08:08:24 -07:00
const c = (i + 50 >= numEmp) ? numEmp - i : 50;
2024-05-09 08:10:20 -07:00
2024-05-14 08:08:24 -07:00
const employeeRes = await this.#APIRef._makeReq(`variables=(start:${i},origin:FACETED_SEARCH,query:(flagshipSearchIntent:ORGANIZATIONS_PEOPLE_ALUMNI,queryParameters:List((key:currentCompany,value:List(${this.entityNum})),(key:resultType,value:List(ORGANIZATION_ALUMNI))),includeFiltersInResponse:true),count:${c})`);
2024-05-09 08:10:20 -07:00
const employees = employeeRes.included;
const empParsed = employees.filter(e => (e.$type === "com.linkedin.voyager.dash.search.EntityResultViewModel"))
.filter(o => o.title.text !== "LinkedIn Member");
if (empParsed.length) empAll.push(...empParsed);
await this.#APIRef.evade();
}
return (raw) ? empAll : empAll.map(eRaw => new LinkedInProfile(eRaw, this.#APIRef));
}
2024-05-14 09:45:16 -07:00
toObj() {
return {
data: {
title: { text: this.name },
entityUrn: this.urn,
navigationUrl: this.url,
trackingUrn: `urn:li:company:${this.entityNum}`
}
};
}
2024-05-09 08:10:20 -07:00
/**
* @returns {Promise<JSON[] | LinkedInProfile[]>}
* @param {string} name
2024-05-13 08:44:09 -07:00
* @param {boolean} castToClass
2024-05-10 13:27:48 -07:00
* @param {number} limit
* @note this function calls {@link linkedInAPIClass.searchEmployees}
2024-05-09 08:10:20 -07:00
*/
2024-05-13 08:44:09 -07:00
searchEmployees = (name, limit = 1000, castToClass = true) => this.#APIRef.searchEmployees(name, limit, castToClass, [this.entityNum]);
2024-05-09 08:10:20 -07:00
async getInfo() {
const toAdd = `q=universalName&universalName=${this.urn}`;
return await this.#APIRef._makeReq(toAdd);
}
checkIfCompleted = () => !!(this.name && this.url && this.urn && this.#APIRef);
/**
* @param {{title: {text: String}, entityUrn: String, navigationUrl: String}} data
* @param {import('../index.js').linkedInAPIClass} APIRef
*/
constructor(data, APIRef) {
this.#APIRef = APIRef;
// this.entityUrn = entityUrn?.replace("urn:li:fsd_company:", "");
this.name = data.title.text;
this.urn = data.entityUrn;
this.url = data.navigationUrl;
2024-05-10 13:27:48 -07:00
this.entityNum = data.trackingUrn.replace('urn:li:company:', '');
2024-05-09 08:10:20 -07:00
2024-05-30 15:35:16 -04:00
axios.get(this.url).then(r => {
const $ = cheerio.load(r.data);
const u = $('a[aria-describedby="websiteLinkDescription"]')?.attr('href')?.split('url=http')?.at(1)?.split('&')[0];
if (u) this.websiteurl = `http${decodeURIComponent(u)}`;
}).catch(_ => null);
2024-05-09 08:10:20 -07:00
if (!this.checkIfCompleted()) throw "NOT ALL NEEDED PARAMS FOUND!";
}
}