mirror of https://github.com/aruppi/aruppi-api.git
Updated utils/requestCall
Added new options to spoof user agent New Aruppi APi user agent Adapt relevant code to new funcionality fixed line break on urls.tspull/59/head v4.2.0
parent
736ff79707
commit
faf0afee8c
@ -1,28 +1,52 @@
|
|||||||
import got from 'got';
|
import got from 'got';
|
||||||
import cheerio from 'cheerio';
|
import cheerio from 'cheerio';
|
||||||
import { CookieJar } from 'tough-cookie';
|
import { CookieJar } from 'tough-cookie';
|
||||||
|
// @ts-ignore
|
||||||
|
import * as got_pjson from 'got/package.json'
|
||||||
|
const pjson = require('../../package.json');
|
||||||
|
|
||||||
const cookieJar = new CookieJar();
|
const cookieJar = new CookieJar();
|
||||||
|
const aruppi_options: any = {
|
||||||
|
cookieJar,
|
||||||
|
'headers': {
|
||||||
|
'user-agent': `Aruppi-API/${pjson.version} ${got_pjson.name}/${got_pjson.version}`,
|
||||||
|
'x-client': 'aruppi-api'
|
||||||
|
},
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
interface Options {
|
interface Options {
|
||||||
scrapy: boolean;
|
scrapy?: boolean,
|
||||||
parse: boolean;
|
parse?: boolean,
|
||||||
|
spoof?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const requestGot = async (
|
export const requestGot = async (
|
||||||
url: string,
|
url: string,
|
||||||
options?: Options,
|
options?: Options,
|
||||||
): Promise<any> => {
|
): Promise<any> => {
|
||||||
if (options !== undefined) {
|
const got_options: any = {...got.defaults.options, ...aruppi_options}
|
||||||
|
if (options) {
|
||||||
|
if (options.spoof != null) {
|
||||||
|
got_options.headers["user-agent"] = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/69.0";
|
||||||
|
delete got_options.headers['x-client'];
|
||||||
|
if (!options.spoof)
|
||||||
|
got_options.headers['user-agent'] = got.defaults.options.headers['user-agent'];
|
||||||
|
} else if (process.env.ALPI_KEY && (new URL(url)).hostname.match(/\.jeluchu\.xyz$/)) {
|
||||||
|
got_options.headers['x-aruppi-key'] = process.env.ALPI_KEY;
|
||||||
|
}
|
||||||
|
|
||||||
if (options.scrapy) {
|
if (options.scrapy) {
|
||||||
const response = await got(url, { cookieJar });
|
const response = await got(url, got_options);
|
||||||
return await cheerio.load(response.body);
|
return cheerio.load(response.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.parse) {
|
if (options.parse) {
|
||||||
return await got(url, { cookieJar }).json();
|
got_options.responseType = 'json';
|
||||||
|
const response = await got(url, got_options);
|
||||||
|
return response.body;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return await got.get(url, { cookieJar });
|
|
||||||
}
|
}
|
||||||
|
const response = await got.get(url, got_options);
|
||||||
|
return response;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue