newpipe-extractor-js
Version:
JavaScript/Node.js port of NewPipeExtractor
168 lines • 6.35 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultDownloader = exports.Downloader = void 0;
const tough_cookie_1 = require("tough-cookie");
const node_fetch_1 = __importDefault(require("node-fetch"));
class Downloader {
/**
* Convenience method to download JSON and parse it.
*/
async downloadJson(url, localization) {
const response = await this.download(url, localization);
return JSON.parse(response);
}
}
exports.Downloader = Downloader;
class DefaultDownloader extends Downloader {
constructor(cookieJar, userAgent) {
super();
this.cookieJar = cookieJar || new tough_cookie_1.CookieJar();
this.userAgent = userAgent || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
}
async download(url, localization) {
const request = {
httpMethod: 'GET',
url: url,
headers: await this.getDefaultHeaders(localization)
};
if (localization) {
request.localization = localization;
}
const response = await this.execute(request);
return response.responseBody;
}
async downloadPost(url, headers, dataToSend, localization) {
const request = {
httpMethod: 'POST',
url: url,
headers: { ...await this.getDefaultHeaders(localization), ...headers },
dataToSend: dataToSend
};
if (localization) {
request.localization = localization;
}
const response = await this.execute(request);
return response.responseBody;
}
async execute(request) {
try {
// Get cookies for this URL
const cookieString = await this.cookieJar.getCookieString(request.url);
// Prepare headers
const headers = {};
for (const [key, values] of Object.entries(request.headers)) {
headers[key] = values.join(', ');
}
if (cookieString) {
headers['Cookie'] = cookieString;
}
// Prepare fetch options
const fetchOptions = {
method: request.httpMethod,
headers: headers,
redirect: 'follow'
};
if (request.dataToSend) {
fetchOptions.body = request.dataToSend;
}
// Make the request
const response = await (0, node_fetch_1.default)(request.url, fetchOptions);
// Store cookies from response
const setCookieHeader = response.headers.get('set-cookie');
if (setCookieHeader) {
const cookies = setCookieHeader.split(',').map((c) => c.trim());
for (const cookieStr of cookies) {
try {
await this.cookieJar.setCookie(cookieStr, request.url);
}
catch (e) {
// Ignore invalid cookies
}
}
}
// Convert response headers to the expected format
const responseHeaders = {};
response.headers.forEach((value, key) => {
responseHeaders[key] = responseHeaders[key] || [];
responseHeaders[key].push(value);
});
const responseBody = await response.text();
return {
responseCode: response.status,
responseMessage: response.statusText,
responseHeaders: responseHeaders,
responseBody: responseBody,
latestUrl: response.url
};
}
catch (error) {
throw new Error(`Network request failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
async getDefaultHeaders(localization) {
const headers = {
'User-Agent': [this.userAgent],
'Accept': ['text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'],
'Accept-Language': [this.getAcceptLanguageHeader(localization)],
'Accept-Encoding': ['gzip, deflate, br'],
'DNT': ['1'],
'Connection': ['keep-alive'],
'Upgrade-Insecure-Requests': ['1']
};
return headers;
}
getAcceptLanguageHeader(localization) {
if (localization) {
const locale = localization.countryCode
? `${localization.languageCode}-${localization.countryCode}`
: localization.languageCode;
return `${locale},en;q=0.9,*;q=0.5`;
}
return 'en-US,en;q=0.9,*;q=0.5';
}
/**
* Set a custom user agent
*/
setUserAgent(userAgent) {
this.userAgent = userAgent;
}
/**
* Get the cookie jar
*/
getCookieJar() {
return this.cookieJar;
}
/**
* Load cookies from a JSON array
*/
async loadCookiesFromJson(jsonCookies) {
try {
for (const jc of jsonCookies) {
const cookie = new tough_cookie_1.Cookie({
key: jc.name,
value: jc.value,
domain: jc.domain,
path: jc.path || '/',
secure: jc.secure || false,
httpOnly: jc.httpOnly || false,
expires: jc.expirationDate ? new Date(jc.expirationDate * 1000) : undefined,
});
const storeUrl = `https://${(jc.domain || '').startsWith('.') ? (jc.domain || '').substring(1) : (jc.domain || 'example.com')}${jc.path || '/'}`;
try {
await this.cookieJar.setCookie(cookie.toString(), storeUrl);
}
catch (e) {
// Skip invalid cookies
}
}
}
catch (error) {
throw new Error(`Failed to load cookies from JSON: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
}
exports.DefaultDownloader = DefaultDownloader;
//# sourceMappingURL=Downloader.js.map