-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchFunctions.js
259 lines (210 loc) · 8.6 KB
/
searchFunctions.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
const puppeteer = require('puppeteer');
const chromium = require('chrome-aws-lambda');
const axios = require('axios');
const { load } = require('cheerio');
//regEx helper function
const regExFindPhrase = (string, html) => {
const regex = new RegExp(`\\b${string}\\b`,"gi");
const phraseFound = regex.test(html);
return phraseFound;
};
//job-posting string length helper function
const evalInnerHtmlLength = (innerHtmlStr) => {
if(innerHtmlStr.length > 100) return "Description Very Long - Check Website"
if(innerHtmlStr.length <= 100) return innerHtmlStr;
};
const getJobsObj = (searchPhraseArr, urlArr) => {
//create resultObj: structure is -->
// { 'url1': [job1, job2], 'url2': [job1, job2]}
let resultObj = {};
if(!searchPhraseArr.length) return 'Search Phrases Empty';
if(!urlArr) return 'No url addresses';
const urlKeyArr = Object.keys(urlArr);
try{
urlKeyArr.forEach(elementUrl => {
const htmlArr = urlArr[elementUrl];
// error output if url status code is 404 for bad url
if(!htmlArr.length && typeof htmlArr === 'object') {
const objKeys = Object.keys(htmlArr);
resultObj[elementUrl] = objKeys;
} else {
htmlArr.forEach((currHtml) => {
//load a url into cherrio load method)
const $ = load(currHtml);
//traverse url/html
$('a').each((i, e) => { // specifically targets <a> tags only
const rowInnerHtml = $(e).text();
//iterate over every html row/element
// use 'every' method to only push a row once, if a phrase match is found
searchPhraseArr.every(elementPhrase => {
//search for phrase in html
const phraseFound = regExFindPhrase(elementPhrase, rowInnerHtml);
if(phraseFound) { // add found phrases to output
//if new phrase found, create 'url': [current elem]
// also add paginated text as > 1 htmlArr elem === website pager exists
if(htmlArr.length > 1 && !resultObj[elementUrl]) {
resultObj[elementUrl] = ["( Paginated Webpage )"];
};
if(!resultObj[elementUrl]) {
resultObj[elementUrl] = [evalInnerHtmlLength(rowInnerHtml)];
} else if(resultObj[elementUrl]) {
const elementArr = resultObj[elementUrl];
elementArr.push(evalInnerHtmlLength(rowInnerHtml));
};
return false;
};
return true;
});
});
});
};
//What if other urls found a phrase, though not current url?: Let User know in result
const currUrlArr = resultObj[elementUrl]
if(!currUrlArr) resultObj[elementUrl] = ['No phrases found.']
});
}catch(err) {
return(err);
console.log("url Error, Please check inputs")
}
//What if no phrases found for all urls?: Let user know in result
//? make this front end filter, instead ?
if(!Object.keys(resultObj).length) resultObj["All urls"] = ["No Jobs Found."]; //output if result empty
return resultObj;
};
//* currently using this func for scraping to help with sites using Javascript
const headlessBrowser = async(url) => {
console.log('inside headlessBrowser-->',url);
let browser = null;
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: true,
// headless: chromium.headless, //* turned off to test chromium library
ignoreHTTPSErrors: true,
});
const page = await browser.newPage();
//setting timeout to be less than AWS Gateway default of 30 secs
await page.setDefaultNavigationTimeout(25000);
let errorArr = []; // for error output in place of html if url bad
await page.on('response', response => {
if(response.status() === 404) {
errorArr.push(`Url Status: ${response.status()}`);
};
});
await page.goto(url, { waitUntil: 'networkidle0' });
// for website with pagination
const paginationBoolean = await page.$('.pagination');
let htmlArr = [];
if(paginationBoolean) {
let pageNumber = 1;
// to slowdown loop due to race-condition when loading JS-SPA page
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds)
)};
//iterate & scrape while a page has a 'next' link or button
while (true) {
await sleep(2000);
// const origPageUrl = await page.url(); //* for if 'next' link remains at end of pager
// console.log('origPageUrl-->:', origPageUrl)
// eval if pagination using btns or anchor href elements
let pagerArr;
const pagerButtonsHtml = await page.$$('.pagination button', links => links.map(n => n));
const pagerAnchorLinks = await page.$$('.pagination a', links => links.map(n => n));
// prep pagerArr w/ html-element of either buttons or anchors or both
if(pagerButtonsHtml.length > 0 && pagerAnchorLinks.length > 0) {
pagerArr = pagerButtonsHtml;
pagerAnchorLinks.map(currElem => pagerArr.push(currElem));
} else if(pagerButtonsHtml.length > 0){
pagerArr = pagerButtonsHtml;
} else if(pagerAnchorLinks.length > 0) {
pagerArr = pagerAnchorLinks;
};
// scrape current page
htmlArr.push(await page.content());
console.log(`Scraping page ${pageNumber}`);
// func for finding next element
const isNextElement = async(array) => {
if(!array) return undefined;
for(let i = 0; i <= array.length; i++) {
const currElement = array[i];
if(currElement){
const valueHandle = await currElement.getProperty('innerText');
const linkText = await valueHandle.jsonValue();
const foundNextLink = regExFindPhrase('next', linkText);
if(foundNextLink) return currElement;
}
};
return undefined;
};
// look for element with 'next' text
const nextLink = await isNextElement(pagerArr);
// console.log('nextLink-->:', nextLink)
//*testing for url change, if next link/btn doesn't go away in DOM
//* for if 'next' link remains at end of pager
// const isPageUrlSame = (url) => {
// if(origPageUrl !== url) return false;
// return true;
// };
if(nextLink) {
try{
await Promise.all([
nextLink.click(),
page.waitForNavigation({ waitUntil: 'networkidle0' })
]);
} catch(error) {
console.log(error);
break;
};
//* for if 'next' link remains at end of pager
// const pageUrl = page.url();
// console.log('pageUrl-->', pageUrl)
//break while loop if new url matches current url (edge case)
// if(isPageUrlSame(pageUrl)) break;
pageNumber++;
} else {
//stop while loop if no 'next' links on page
break;
};
};
} else {
// scrape current page if no pagination
htmlArr.push(await page.content());
};
// const html = await page.content();
await browser.close();
if(errorArr.length) return errorArr;
return htmlArr;
// return html;
}catch(err) {
console.log(err);
return {Error:err};
};
}
// basic html scraper (NOT capatible with sites using Javascript)
const getHtml = async(urlStr) => {
try {
const html = await axios.get(urlStr);
return html;
} catch(err) {
console.log('html fetch error:', urlStr)
}
}
// 3rd Party API scraper (if more robust scraper needed, etc....)
const resScrapeNinja = async(urlStr) => {
// const jsonUrl = JSON.stringify({url: urlStr});
return await axios.post('https://scrapeninja.p.rapidapi.com/scrape-js', {
url: urlStr
}, {
headers: {
'content-type': 'application/json',
'X-RapidAPI-Key': process.env.XRAPIDAPIKEY,
'X-RapidAPI-Host': process.env.XRAPIDAPIHOST
}
});
};
exports.getJobsObj = getJobsObj;
exports.headlessBrowser = headlessBrowser;
exports.getHtml = getHtml;
exports.resScrapeNinja = resScrapeNinja;