[scraper] adds helper class for proxy and user agent

This commit is contained in:
TheBeastLT
2020-04-02 22:47:52 +02:00
parent db78f5873b
commit 2833c9d3a2
6 changed files with 76 additions and 14 deletions

View File

@@ -0,0 +1,26 @@
const UserAgent = require('user-agents');
const PROXY_HOSTS = process.env.PROXY_HOST && process.env.PROXY_HOST.split(',');
const PROXY_USERNAME = process.env.PROXY_USERNAME;
const PROXY_PASSWORD = process.env.PROXY_PASSWORD;
const userAgent = new UserAgent();
function getRandomUserAgent() {
return userAgent.random().toString();
}
function getRandomProxy() {
if (PROXY_HOSTS && PROXY_HOSTS.length && PROXY_USERNAME && PROXY_PASSWORD) {
return `http://${PROXY_USERNAME}:${PROXY_PASSWORD}@${PROXY_HOSTS[Math.floor(Math.random() * PROXY_HOSTS.length)]}`;
}
return undefined;
}
function getProxy() {
if (PROXY_HOSTS && PROXY_HOSTS.length && PROXY_USERNAME && PROXY_PASSWORD) {
return `http://${PROXY_USERNAME}:${PROXY_PASSWORD}@${PROXY_HOSTS[0]}`;
}
return undefined;
}
module.exports = { getRandomUserAgent, getRandomProxy, getProxy };