mirror of https://github.com/aruppi/aruppi-api.git
Compare commits
18 Commits
Author | SHA1 | Date |
---|---|---|
|
6c44d5a712 | 2 years ago |
|
cef7574083 | 2 years ago |
|
7c6307cb1e | 2 years ago |
|
58ac761098 | 2 years ago |
|
3a633b2ff2 | 2 years ago |
|
e39bb486b0 | 2 years ago |
|
189a5bfb79 | 2 years ago |
|
df30fd785e | 2 years ago |
|
656d4ce1ba | 2 years ago |
|
a90e522c21 | 2 years ago |
|
faf0afee8c | 3 years ago |
|
736ff79707 | 3 years ago |
|
2b4e3d0d7d | 3 years ago |
|
fdb744582f | 3 years ago |
|
c3fa6cd854 | 3 years ago |
|
868a958e36 | 3 years ago |
|
99d636fa3f | 4 years ago |
|
361d0553e9 | 4 years ago |
File diff suppressed because it is too large
Load Diff
@ -1,28 +1,52 @@
|
||||
import got from 'got';
|
||||
import cheerio from 'cheerio';
|
||||
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 aruppi_options: any = {
|
||||
cookieJar,
|
||||
'headers': {
|
||||
'user-agent': `Aruppi-API/${pjson.version} ${got_pjson.name}/${got_pjson.version}`,
|
||||
'x-client': 'aruppi-api'
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
interface Options {
|
||||
scrapy: boolean;
|
||||
parse: boolean;
|
||||
scrapy?: boolean,
|
||||
parse?: boolean,
|
||||
spoof?: boolean
|
||||
}
|
||||
|
||||
export const requestGot = async (
|
||||
url: string,
|
||||
options?: Options,
|
||||
url: string,
|
||||
options?: Options,
|
||||
): Promise<any> => {
|
||||
if (options !== undefined) {
|
||||
if (options.scrapy) {
|
||||
const response = await got(url, { cookieJar });
|
||||
return await cheerio.load(response.body);
|
||||
}
|
||||
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) {
|
||||
const response = await got(url, got_options);
|
||||
return cheerio.load(response.body);
|
||||
}
|
||||
|
||||
if (options.parse) {
|
||||
return await got(url, { cookieJar }).json();
|
||||
if (options.parse) {
|
||||
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;
|
||||
};
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue