got#Response TypeScript Examples
The following examples show how to use
got#Response.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: instagram.ts From scraper with GNU General Public License v3.0 | 5 votes |
export async function instagramdlv3 (url: string): Promise<InstagramDownloaderV2[]> {
const payload = {
link: url,
submit: ''
}; const headers: Headers = {
'content-type': 'application/x-www-form-urlencoded',
origin: 'https://instasave.website',
referer: 'https://instasave.website/',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}
const body: Response<string> = await got('https://instasave.website/', {
form: payload,
method: 'POST',
headers: headers
}).catch(async (_) => await got('https://server.instasave.website/', {
form: payload,
method: 'POST',
headers: {
...headers,
origin: 'https://server.instasave.website',
referer: 'https://server.instasave.website'
}
}))
const $ = cheerio.load(body.body)
let results: InstagramDownloaderV2[] = []
if ($('#downloadBox > a').length) {
const temp: {
thumbnail?: string;
sourceUrl?: string;
index: number;
url?: string;
}[] = []
$('#downloadBox > video').each(function (i) {
const thumbnail = $(this).attr('poster')
const sourceUrl = $(this).find('source[src]').attr('src')
if (thumbnail) {
temp.push({
thumbnail,
sourceUrl,
index: i
})
}
})
$('#downloadBox > img').each(function (i) {
const j = temp.findIndex(({ index }) => index === i)
const thumbnail = $(this).attr('src')
if (thumbnail) {
if (j !== -1) temp[j].thumbnail = thumbnail
else temp.push({ thumbnail, index: i })
}
})
$('#downloadBox > a').each(function (i) {
const j = temp.findIndex(({ index }) => index === i)
const url = $(this).attr('href')
if (j !== -1) temp[j].url = url
else temp.push({ url, index: i })
})
results = temp.map((tmp) => ({
thumbnail: tmp.thumbnail as string,
sourceUrl: tmp.sourceUrl as string,
url: tmp.url as string
}))
}
if (!results.length) throw new ScraperError(`Can't download!\n${$.html()}`)
return results
}
Example #2
Source File: module.getdata.ts From multi-downloader-nx with MIT License | 4 votes |
getData = async <T = string>(options: Options) => {
const regionHeaders = {};
const gOptions = {
url: options.url,
http2: true,
headers: {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:70.0) Gecko/20100101 Firefox/70.0',
'Accept-Encoding': 'gzip',
...regionHeaders
}
} as OptionsOfUnknownResponseBody;
if(options.responseType) {
gOptions.responseType = options.responseType;
}
if(options.baseUrl){
gOptions.prefixUrl = options.baseUrl;
gOptions.url = gOptions.url?.toString().replace(/^\//,'');
}
if(options.querystring){
gOptions.url += `?${new URLSearchParams(options.querystring).toString()}`;
}
if(options.auth){
gOptions.method = 'POST';
const newHeaders = {
...gOptions.headers,
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Origin': 'https://ww.funimation.com',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0'
};
gOptions.headers = newHeaders;
gOptions.body = `username=${encodeURIComponent(options.auth.user)}&password=${encodeURIComponent(options.auth.pass)}`;
}
if(options.useToken && options.token){
gOptions.headers = {
...gOptions.headers,
Authorization: `Token ${options.token}`
};
}
if(options.dinstid){
gOptions.headers = {
...gOptions.headers,
devicetype: 'Android Phone'
};
}
// debug
gOptions.hooks = {
beforeRequest: [
(gotOpts) => {
if(options.debug){
console.log('[DEBUG] GOT OPTIONS:');
console.log(gotOpts);
}
}
]
};
try {
const res = await got(gOptions);
if(res.body && (options.responseType !== 'buffer' && (res.body as string).match(/^</))){
throw { name: 'HTMLError', res };
}
return {
ok: true,
res: {
...res,
body: res.body as T
},
};
}
catch(_error){
const error = _error as {
name: string,
} & ReadError & {
res: Response<unknown>
};
if(options.debug){
console.log(error);
}
if(error.response && error.response.statusCode && error.response.statusMessage){
console.log(`[ERROR] ${error.name} ${error.response.statusCode}: ${error.response.statusMessage}`);
}
else if(error.name && error.name == 'HTMLError' && error.res && error.res.body){
console.log(`[ERROR] ${error.name}:`);
console.log(error.res.body);
}
else{
console.log(`[ERROR] ${error.name}: ${error.code||error.message}`);
}
return {
ok: false,
error,
};
}
}
Example #3
Source File: module.req.ts From multi-downloader-nx with MIT License | 4 votes |
async getData<T = string> (durl: string, params?: Params) {
params = params || {};
// options
const options: Options & {
minVersion?: string,
maxVersion?: string
curlDebug?: boolean
} = {
method: params.method ? params.method : 'GET',
headers: {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:90.0) Gecko/20100101 Firefox/90.0',
},
};
// additional params
if(params.headers){
options.headers = {...options.headers, ...params.headers};
}
if(options.method == 'POST'){
(options.headers as Headers)['Content-Type'] = 'application/x-www-form-urlencoded';
}
if(params.body){
options.body = params.body;
}
if(params.binary == true){
options.responseType = 'buffer';
}
if(typeof params.followRedirect == 'boolean'){
options.followRedirect = params.followRedirect;
}
// if auth
const loc = new URL(durl);
// avoid cloudflare protection
// debug
options.hooks = {
beforeRequest: [
(options) => {
if(this.debug){
console.log('[DEBUG] GOT OPTIONS:');
console.log(options);
}
}
]
};
if(this.debug){
options.curlDebug = true;
}
// try do request
try {
const res = await got(durl.toString(), options) as unknown as Response<T>;
return {
ok: true,
res
};
}
catch(_error){
const error = _error as {
name: string
} & ReadError & {
res: Response<unknown>
};
if(error.response && error.response.statusCode && error.response.statusMessage){
console.log(`[ERROR] ${error.name} ${error.response.statusCode}: ${error.response.statusMessage}`);
}
else{
console.log(`[ERROR] ${error.name}: ${error.code || error.message}`);
}
if(error.response && !error.res){
error.res = error.response;
const docTitle = (error.res.body as string).match(/<title>(.*)<\/title>/);
if(error.res.body && docTitle){
console.log('[ERROR]', docTitle[1]);
}
}
if(error.res && error.res.body && error.response.statusCode
&& error.response.statusCode != 404 && error.response.statusCode != 403){
console.log('[ERROR] Body:', error.res.body);
}
return {
ok: false,
error,
};
}
}