mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[scraper] adds helper class for proxy and user agent
This commit is contained in:
@@ -5,6 +5,7 @@ const bing = require('nodejs-bing');
|
||||
const he = require('he');
|
||||
const { cacheWrapImdbId, cacheWrapKitsuId, cacheWrapMetadata } = require('./cache');
|
||||
const { Type } = require('./types');
|
||||
const { getRandomUserAgent } = require('./request_helper');
|
||||
|
||||
const CINEMETA_URL = 'https://v3-cinemeta.strem.io';
|
||||
const KITSU_URL = 'https://anime-kitsu.strem.fun';
|
||||
@@ -107,7 +108,7 @@ async function getImdbId(info, type) {
|
||||
reject(err || new Error('failed imdbId search'));
|
||||
}
|
||||
});
|
||||
}).catch(() => googleIt({ query, disableConsole: true })
|
||||
}).catch(() => googleIt({ query, userAgent: getRandomUserAgent(), disableConsole: true })
|
||||
.catch(() => bing.web(query))
|
||||
.then(results => results
|
||||
.map(result => result.link)
|
||||
|
||||
26
scraper/lib/request_helper.js
Normal file
26
scraper/lib/request_helper.js
Normal 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 };
|
||||
@@ -2,10 +2,7 @@ const { encode } = require('magnet-uri');
|
||||
const RealDebridClient = require('real-debrid-api');
|
||||
const namedQueue = require('named-queue');
|
||||
const { cacheWrapResolvedUrl } = require('../lib/cache');
|
||||
|
||||
const PROXY_HOST = process.env.PROXY_HOST;
|
||||
const PROXY_USERNAME = process.env.PROXY_USERNAME;
|
||||
const PROXY_PASSWORD = process.env.PROXY_PASSWORD;
|
||||
const { getProxy } = require('../lib/request_helper');
|
||||
|
||||
const unrestrictQueue = new namedQueue((task, callback) => task.method()
|
||||
.then(result => callback(false, result))
|
||||
@@ -70,11 +67,4 @@ async function _unrestrictLink(RD, link) {
|
||||
// });
|
||||
}
|
||||
|
||||
function getProxy() {
|
||||
if (PROXY_HOST && PROXY_USERNAME && PROXY_PASSWORD) {
|
||||
return `http://${PROXY_USERNAME}:${PROXY_PASSWORD}@${PROXY_HOST}`;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
module.exports = { resolve };
|
||||
@@ -3,6 +3,7 @@ const needle = require('needle');
|
||||
const moment = require('moment');
|
||||
const decode = require('magnet-uri');
|
||||
const Promises = require('../../lib/promises');
|
||||
const { getRandomProxy, getRandomUserAgent } = require('./../../lib/request_helper');
|
||||
|
||||
const defaultProxies = [
|
||||
'https://katcr.co'
|
||||
@@ -64,8 +65,9 @@ function browse(config = {}, retries = 2) {
|
||||
|
||||
function singleRequest(requestUrl, config = {}) {
|
||||
const timeout = config.timeout || defaultTimeout;
|
||||
const options = { userAgent: getRandomUserAgent(), proxy: getRandomProxy(), open_timeout: timeout, follow: 2 };
|
||||
|
||||
return needle('get', requestUrl, { open_timeout: timeout, follow: 2 })
|
||||
return needle('get', requestUrl, options)
|
||||
.then((response) => {
|
||||
const body = response.body;
|
||||
if (!body) {
|
||||
|
||||
Reference in New Issue
Block a user