2021-05-11 11:25:14 +02:00
|
|
|
const axios = require('axios');
|
2021-11-11 15:31:53 +01:00
|
|
|
const axiosRetry = require('axios-retry');
|
|
|
|
|
|
|
|
|
|
axiosRetry(axios, { retryDelay: axiosRetry.exponentialDelay, retries: 3 });
|
2021-05-11 11:25:14 +02:00
|
|
|
|
|
|
|
|
function makeDriver(headers = {}) {
|
|
|
|
|
let cookies = '';
|
|
|
|
|
|
|
|
|
|
return async function driver(context, callback) {
|
|
|
|
|
const url = context.url;
|
|
|
|
|
let result;
|
|
|
|
|
try {
|
|
|
|
|
result = await axios({
|
|
|
|
|
url,
|
|
|
|
|
headers: {
|
|
|
|
|
...headers,
|
|
|
|
|
Cookie: cookies,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (exception) {
|
2021-11-11 15:31:53 +01:00
|
|
|
console.error(`Error while trying to scrape data. Received error: ${exception.message}`);
|
|
|
|
|
callback(null, []);
|
2021-05-11 11:25:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof result.data === 'object' && url.toLowerCase().indexOf('scrapingant') !== -1) {
|
|
|
|
|
//assume we have gotten a response from scrapingAnt
|
|
|
|
|
if (cookies.length === 0) {
|
|
|
|
|
cookies = result.data.cookies;
|
|
|
|
|
}
|
|
|
|
|
callback(null, result.data.content);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, result.data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = makeDriver;
|