-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
308 lines (268 loc) · 8.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const cheerio = require("cheerio");
const axios = require("axios");
const randomUseragent = require("random-useragent");
// Utility functions
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// Cache implementation
class JobCache {
constructor() {
this.cache = new Map();
this.TTL = 1000 * 60 * 60; // 1 hour
}
set(key, value) {
this.cache.set(key, {
data: value,
timestamp: Date.now(),
});
}
get(key) {
const item = this.cache.get(key);
if (!item) return null;
if (Date.now() - item.timestamp > this.TTL) {
this.cache.delete(key);
return null;
}
return item.data;
}
clear() {
const now = Date.now();
for (const [key, value] of this.cache.entries()) {
if (now - value.timestamp > this.TTL) {
this.cache.delete(key);
}
}
}
}
const cache = new JobCache();
// Main query function
module.exports.query = (queryObject) => {
const query = new Query(queryObject);
return query.getJobs();
};
// Query constructor
function Query(queryObj) {
this.host = queryObj.host || "www.linkedin.com";
this.keyword = queryObj.keyword?.trim().replace(/\s+/g, "+") || "";
this.location = queryObj.location?.trim().replace(/\s+/g, "+") || "";
this.dateSincePosted = queryObj.dateSincePosted || "";
this.jobType = queryObj.jobType || "";
this.remoteFilter = queryObj.remoteFilter || "";
this.salary = queryObj.salary || "";
this.experienceLevel = queryObj.experienceLevel || "";
this.sortBy = queryObj.sortBy || "";
this.limit = Number(queryObj.limit) || 0;
this.page = Number(queryObj.page) || 0;
}
// Query prototype methods
Query.prototype.getDateSincePosted = function () {
const dateRange = {
"past month": "r2592000",
"past week": "r604800",
"24hr": "r86400",
};
return dateRange[this.dateSincePosted.toLowerCase()] || "";
};
Query.prototype.getExperienceLevel = function () {
const experienceRange = {
internship: "1",
"entry level": "2",
associate: "3",
senior: "4",
director: "5",
executive: "6",
};
return experienceRange[this.experienceLevel.toLowerCase()] || "";
};
Query.prototype.getJobType = function () {
const jobTypeRange = {
"full time": "F",
"full-time": "F",
"part time": "P",
"part-time": "P",
contract: "C",
temporary: "T",
volunteer: "V",
internship: "I",
};
return jobTypeRange[this.jobType.toLowerCase()] || "";
};
Query.prototype.getRemoteFilter = function () {
const remoteFilterRange = {
"on-site": "1",
"on site": "1",
remote: "2",
hybrid: "3",
};
return remoteFilterRange[this.remoteFilter.toLowerCase()] || "";
};
Query.prototype.getSalary = function () {
const salaryRange = {
40000: "1",
60000: "2",
80000: "3",
100000: "4",
120000: "5",
};
return salaryRange[this.salary] || "";
};
Query.prototype.getPage = function () {
return this.page * 25;
};
Query.prototype.url = function (start) {
let query = `https://${this.host}/jobs-guest/jobs/api/seeMoreJobPostings/search?`;
const params = new URLSearchParams();
if (this.keyword) params.append("keywords", this.keyword);
if (this.location) params.append("location", this.location);
if (this.getDateSincePosted())
params.append("f_TPR", this.getDateSincePosted());
if (this.getSalary()) params.append("f_SB2", this.getSalary());
if (this.getExperienceLevel())
params.append("f_E", this.getExperienceLevel());
if (this.getRemoteFilter()) params.append("f_WT", this.getRemoteFilter());
if (this.getJobType()) params.append("f_JT", this.getJobType());
params.append("start", start + this.getPage());
if (this.sortBy === "recent") params.append("sortBy", "DD");
else if (this.sortBy === "relevant") params.append("sortBy", "R");
return query + params.toString();
};
Query.prototype.getJobs = async function () {
let allJobs = [];
let start = 0;
const BATCH_SIZE = 25;
let hasMore = true;
let consecutiveErrors = 0;
const MAX_CONSECUTIVE_ERRORS = 3;
try {
// Check cache first
const cacheKey = this.url(0);
const cachedJobs = cache.get(cacheKey);
if (cachedJobs) {
console.log("Returning cached results");
return cachedJobs;
}
while (hasMore) {
try {
const jobs = await this.fetchJobBatch(start);
if (!jobs || jobs.length === 0) {
hasMore = false;
break;
}
allJobs.push(...jobs);
console.log(`Fetched ${jobs.length} jobs. Total: ${allJobs.length}`);
if (this.limit && allJobs.length >= this.limit) {
allJobs = allJobs.slice(0, this.limit);
break;
}
// Reset error counter on successful fetch
consecutiveErrors = 0;
start += BATCH_SIZE;
// Add reasonable delay between requests
await delay(2000 + Math.random() * 1000);
} catch (error) {
consecutiveErrors++;
console.error(
`Error fetching batch (attempt ${consecutiveErrors}):`,
error.message
);
if (consecutiveErrors >= MAX_CONSECUTIVE_ERRORS) {
console.log("Max consecutive errors reached. Stopping.");
break;
}
// Exponential backoff
await delay(Math.pow(2, consecutiveErrors) * 1000);
}
}
// Cache results if we got any
if (allJobs.length > 0) {
cache.set(this.url(0), allJobs);
}
return allJobs;
} catch (error) {
console.error("Fatal error in job fetching:", error);
throw error;
}
};
Query.prototype.fetchJobBatch = async function (start) {
const headers = {
"User-Agent": randomUseragent.getRandom(),
Accept: "application/json, text/javascript, */*; q=0.01",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
Referer: "https://www.linkedin.com/jobs",
"X-Requested-With": "XMLHttpRequest",
Connection: "keep-alive",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Cache-Control": "no-cache",
Pragma: "no-cache",
};
try {
const response = await axios.get(this.url(start), {
headers,
validateStatus: function (status) {
return status === 200;
},
timeout: 10000,
});
return parseJobList(response.data);
} catch (error) {
if (error.response?.status === 429) {
throw new Error("Rate limit reached");
}
throw error;
}
};
function parseJobList(jobData) {
try {
const $ = cheerio.load(jobData);
const jobs = $("li");
return jobs
.map((index, element) => {
try {
const job = $(element);
const position = job.find(".base-search-card__title").text().trim();
const company = job.find(".base-search-card__subtitle").text().trim();
const location = job.find(".job-search-card__location").text().trim();
const dateElement = job.find("time");
const date = dateElement.attr("datetime");
const salary = job
.find(".job-search-card__salary-info")
.text()
.trim()
.replace(/\s+/g, " ");
const jobUrl = job.find(".base-card__full-link").attr("href");
const companyLogo = job
.find(".artdeco-entity-image")
.attr("data-delayed-url");
const agoTime = job.find(".job-search-card__listdate").text().trim();
// Only return job if we have at least position and company
if (!position || !company) {
return null;
}
return {
position,
company,
location,
date,
salary: salary || "Not specified",
jobUrl: jobUrl || "",
companyLogo: companyLogo || "",
agoTime: agoTime || "",
};
} catch (err) {
console.warn(`Error parsing job at index ${index}:`, err.message);
return null;
}
})
.get()
.filter(Boolean);
} catch (error) {
console.error("Error parsing job list:", error);
return [];
}
}
// Export additional utilities for testing and monitoring
module.exports.JobCache = JobCache;
module.exports.clearCache = () => cache.clear();
module.exports.getCacheSize = () => cache.cache.size;