moving to node-fetch coz axios is causing issues with scrapingAnt

This commit is contained in:
weakmap@gmail.com
2022-12-20 10:21:15 +01:00
parent 60bb75da57
commit 8a5fbcdf71
6 changed files with 59 additions and 39 deletions

View File

@@ -1,6 +1,6 @@
const service = require('restana')();
const jobRouter = service.newRouter();
const axios = require('axios');
const fetch = require('node-fetch');
const jobStorage = require('../../services/storage/jobStorage');
const userStorage = require('../../services/storage/userStorage');
const immoscoutProvider = require('../../provider/immoscout');
@@ -34,10 +34,8 @@ jobRouter.get('/processingTimes', async (req, res) => {
if (config.scrapingAnt.apiKey != null && config.scrapingAnt.apiKey.length > 0) {
try {
const result = await axios({
url: `https://api.scrapingant.com/v1/usage?x-api-key=${config.scrapingAnt.apiKey}`,
});
scrapingAntData = result.data;
const response = await fetch(`https://api.scrapingant.com/v1/usage?x-api-key=${config.scrapingAnt.apiKey}`);
scrapingAntData = await response.json();
} catch (Exception) {
console.error('Could not query plan data from scraping ant.', Exception);
}

View File

@@ -1,6 +1,6 @@
const { markdown2Html } = require('../../services/markdown');
const { getJob } = require('../../services/storage/jobStorage');
const axios = require('axios');
const fetch = require('node-fetch');
/**
* sends new listings to mattermost
@@ -21,9 +21,13 @@ exports.send = ({ serviceName, newListings, notificationConfig, jobKey }) => {
(o) => `| [${o.title}](${o.link}) | ` + [o.address, o.size.replace(/2m/g, '$m^2$'), o.price].join(' | ') + ' |\n'
);
return axios.post(`${webhook}`, {
channel: channel,
text: message,
return fetch(webhook, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: {
channel: channel,
text: message,
},
});
};

View File

@@ -1,6 +1,6 @@
const { markdown2Html } = require('../../services/markdown');
const { getJob } = require('../../services/storage/jobStorage');
const axios = require('axios');
const fetch = require('node-fetch');
const MAX_ENTITIES_PER_CHUNK = 8;
const RATE_LIMIT_INTERVAL = 1010;
@@ -47,15 +47,22 @@ exports.send = ({ serviceName, newListings, notificationConfig, jobKey }) => {
*/
return new Promise((resolve, reject) => {
setTimeout(() => {
axios
.post(`https://api.telegram.org/bot${token}/sendMessage`, {
fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
method: 'post',
body: JSON.stringify({
chat_id: chatId,
text: message,
parse_mode: 'HTML',
disable_web_page_preview: true,
}),
headers: { 'Content-Type': 'application/json' },
})
.then(() => {
resolve();
})
.then(() => resolve())
.catch(() => reject());
.catch(() => {
reject();
});
}, RATE_LIMIT_INTERVAL);
});
});

View File

@@ -1,4 +1,4 @@
const axios = require('axios');
const fetch = require('node-fetch');
const config = require('../../conf/config.json');
const { makeUrlResidential } = require('./scrapingAnt');
@@ -16,19 +16,19 @@ function makeDriver(headers = {}) {
try {
const url = proxyType === 'residential' ? makeUrlResidential(context.url) : context.url;
const result = await axios({
url,
const response = await fetch(url, {
headers: {
...headers,
Cookie: cookies,
cookie: cookies,
},
});
const result = await response.text();
if (cookies.length === 0) {
cookies = result.data.cookies;
cookies = response.headers.raw()['set-cookie'] || [];
}
callback(null, result.data.content);
callback(null, result);
} catch (exception) {
/* eslint-disable no-console */
if (!EXPECTED_STATUS_CODES.includes(exception.response?.status)) {
@@ -59,15 +59,15 @@ function makeDriver(headers = {}) {
}
try {
const result = await axios({
url: context.url,
const response = await fetch(context.url, {
headers: {
...headers,
Cookie: cookies,
},
});
callback(null, result.data);
const result = await response.text();
callback(null, result);
} catch (exception) {
console.error(`Error while trying to scrape data. Received error: ${exception.message}`);
callback(null, []);