2020-02-20 19:47:30 +01:00
|
|
|
const nodeFetch = require("node-fetch");
|
2020-02-22 22:15:27 +01:00
|
|
|
const {
|
|
|
|
|
USER_AGENT,
|
|
|
|
|
USE_SCRAPER_API,
|
|
|
|
|
SCRAPER_API_KEY
|
|
|
|
|
} = require("../config/appConfig");
|
2020-02-20 19:47:30 +01:00
|
|
|
|
|
|
|
|
const fetch = async (url, options = {}) => {
|
|
|
|
|
const newOptions = Object.assign({}, options);
|
|
|
|
|
if (!newOptions["headers"]) {
|
|
|
|
|
newOptions["headers"] = {};
|
|
|
|
|
}
|
2020-02-20 21:07:16 +01:00
|
|
|
newOptions["headers"]["User-Agent"] = USER_AGENT;
|
2020-02-23 23:11:21 +01:00
|
|
|
const urlAdaptedForScraping = USE_SCRAPER_API
|
2020-02-22 22:15:27 +01:00
|
|
|
? `http://api.scraperapi.com/?api_key=${SCRAPER_API_KEY}&url=${url}`
|
|
|
|
|
: url;
|
|
|
|
|
|
2020-02-23 23:11:21 +01:00
|
|
|
return nodeFetch(urlAdaptedForScraping, newOptions);
|
2020-02-20 19:47:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = fetch;
|