|
@@ -24,6 +24,34 @@ export default class Scraper {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private getProxyConfig(): any {
|
|
|
|
|
+ const proxyUrlString = config.PROXY_URL;
|
|
|
|
|
+ if (!proxyUrlString || proxyUrlString.trim() === "") {
|
|
|
|
|
+ return undefined;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ const proxyUrl = new URL(proxyUrlString);
|
|
|
|
|
+ const proxyConfig: any = {
|
|
|
|
|
+ protocol: proxyUrl.protocol.replace(":", ""),
|
|
|
|
|
+ host: proxyUrl.hostname,
|
|
|
|
|
+ port: Number(proxyUrl.port)
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ if (proxyUrl.username !== "" || proxyUrl.password !== "") {
|
|
|
|
|
+ proxyConfig.auth = {
|
|
|
|
|
+ username: proxyUrl.username,
|
|
|
|
|
+ password: proxyUrl.password
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return proxyConfig;
|
|
|
|
|
+ } catch (err: any) {
|
|
|
|
|
+ console.error(`Invalid PROXY_URL: ${err.message}`);
|
|
|
|
|
+ return undefined;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public async scrape (options: IScraperOptions): Promise<any> {
|
|
public async scrape (options: IScraperOptions): Promise<any> {
|
|
|
this._options = options;
|
|
this._options = options;
|
|
|
let response: any;
|
|
let response: any;
|
|
@@ -33,6 +61,8 @@ export default class Scraper {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
|
|
+ const proxyConfig = this._options.useProxy ? this.getProxyConfig() : undefined;
|
|
|
|
|
+
|
|
|
if (this._options.scraperMethod == ScraperMethods.PUPPETEER) {
|
|
if (this._options.scraperMethod == ScraperMethods.PUPPETEER) {
|
|
|
const payload: any = {
|
|
const payload: any = {
|
|
|
url: this._options.url,
|
|
url: this._options.url,
|
|
@@ -52,9 +82,18 @@ export default class Scraper {
|
|
|
payload.clickSelector = this._options.clickSelector;
|
|
payload.clickSelector = this._options.clickSelector;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (proxyConfig !== undefined) {
|
|
|
|
|
+ payload.proxy = config.PROXY_URL;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
response = await axios.post(config.PUPPETEER_URL, payload, { headers });
|
|
response = await axios.post(config.PUPPETEER_URL, payload, { headers });
|
|
|
} else {
|
|
} else {
|
|
|
- response = await axios.get(this._options.url, { headers });
|
|
|
|
|
|
|
+ const axiosOptions: any = { headers };
|
|
|
|
|
+ if (proxyConfig !== undefined) {
|
|
|
|
|
+ axiosOptions.proxy = proxyConfig;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ response = await axios.get(this._options.url, axiosOptions);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (config.LOG_LEVEL === LogLevels.DEBUG) {
|
|
if (config.LOG_LEVEL === LogLevels.DEBUG) {
|