mirror of
https://github.com/YuzuZensai/play-dl-test.git
synced 2026-01-31 14:58:05 +00:00
SoundCloud work + prettier
This commit is contained in:
@@ -34,19 +34,19 @@ export class Channel {
|
||||
* @param {object} options Icon options
|
||||
* @param {number} [options.size=0] Icon size. **Default is 0**
|
||||
*/
|
||||
iconURL(options = { size: 0 }): string | undefined{
|
||||
if (typeof options.size !== "number" || options.size < 0) throw new Error("invalid icon size");
|
||||
iconURL(options = { size: 0 }): string | undefined {
|
||||
if (typeof options.size !== 'number' || options.size < 0) throw new Error('invalid icon size');
|
||||
if (!this.icon?.url) return undefined;
|
||||
const def = this.icon.url.split("=s")[1].split("-c")[0];
|
||||
const def = this.icon.url.split('=s')[1].split('-c')[0];
|
||||
return this.icon.url.replace(`=s${def}-c`, `=s${options.size}-c`);
|
||||
}
|
||||
|
||||
get type(): "channel" {
|
||||
return "channel";
|
||||
get type(): 'channel' {
|
||||
return 'channel';
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.name || "";
|
||||
return this.name || '';
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
@@ -60,4 +60,4 @@ export class Channel {
|
||||
subscribers: this.subscribers
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,222 +1,235 @@
|
||||
import { PassThrough } from 'stream'
|
||||
import { PassThrough } from 'stream';
|
||||
import { IncomingMessage } from 'http';
|
||||
import { StreamType } from '../stream';
|
||||
import { request, request_stream } from '../utils/request';
|
||||
import { video_info } from '..';
|
||||
|
||||
export interface FormatInterface{
|
||||
url : string;
|
||||
targetDurationSec : number;
|
||||
maxDvrDurationSec : number
|
||||
export interface FormatInterface {
|
||||
url: string;
|
||||
targetDurationSec: number;
|
||||
maxDvrDurationSec: number;
|
||||
}
|
||||
|
||||
export class LiveStreaming{
|
||||
type : StreamType
|
||||
stream : PassThrough
|
||||
private base_url : string
|
||||
private url : string
|
||||
private interval : number
|
||||
private packet_count : number
|
||||
private timer : NodeJS.Timer | null
|
||||
private video_url : string
|
||||
private dash_timer : NodeJS.Timer | null
|
||||
private segments_urls : string[]
|
||||
private request : IncomingMessage | null
|
||||
constructor(dash_url : string, target_interval : number, video_url : string){
|
||||
this.type = StreamType.Arbitrary
|
||||
this.url = dash_url
|
||||
this.base_url = ''
|
||||
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
||||
this.segments_urls = []
|
||||
this.packet_count = 0
|
||||
this.request = null
|
||||
this.timer = null
|
||||
this.video_url = video_url
|
||||
this.interval = target_interval * 1000 || 0
|
||||
export class LiveStreaming {
|
||||
type: StreamType;
|
||||
stream: PassThrough;
|
||||
private base_url: string;
|
||||
private url: string;
|
||||
private interval: number;
|
||||
private packet_count: number;
|
||||
private timer: NodeJS.Timer | null;
|
||||
private video_url: string;
|
||||
private dash_timer: NodeJS.Timer | null;
|
||||
private segments_urls: string[];
|
||||
private request: IncomingMessage | null;
|
||||
constructor(dash_url: string, target_interval: number, video_url: string) {
|
||||
this.type = StreamType.Arbitrary;
|
||||
this.url = dash_url;
|
||||
this.base_url = '';
|
||||
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 });
|
||||
this.segments_urls = [];
|
||||
this.packet_count = 0;
|
||||
this.request = null;
|
||||
this.timer = null;
|
||||
this.video_url = video_url;
|
||||
this.interval = target_interval * 1000 || 0;
|
||||
this.dash_timer = setTimeout(() => {
|
||||
this.dash_updater()
|
||||
}, 1800000)
|
||||
this.dash_updater();
|
||||
}, 1800000);
|
||||
this.stream.on('close', () => {
|
||||
this.cleanup()
|
||||
this.cleanup();
|
||||
});
|
||||
this.start()
|
||||
this.start();
|
||||
}
|
||||
|
||||
private async dash_updater(){
|
||||
let info = await video_info(this.video_url)
|
||||
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0'){
|
||||
this.url = info.LiveStreamData.dashManifestUrl
|
||||
|
||||
private async dash_updater() {
|
||||
const info = await video_info(this.video_url);
|
||||
if (
|
||||
info.LiveStreamData.isLive === true &&
|
||||
info.LiveStreamData.hlsManifestUrl !== null &&
|
||||
info.video_details.durationInSec === '0'
|
||||
) {
|
||||
this.url = info.LiveStreamData.dashManifestUrl;
|
||||
}
|
||||
this.dash_timer = setTimeout(() => {
|
||||
this.dash_updater()
|
||||
}, 1800000)
|
||||
this.dash_updater();
|
||||
}, 1800000);
|
||||
}
|
||||
|
||||
private async dash_getter(){
|
||||
let response = await request(this.url)
|
||||
let audioFormat = response.split('<AdaptationSet id="0"')[1].split('</AdaptationSet>')[0].split('</Representation>')
|
||||
if(audioFormat[audioFormat.length - 1] === '') audioFormat.pop()
|
||||
this.base_url = audioFormat[audioFormat.length - 1].split('<BaseURL>')[1].split('</BaseURL>')[0]
|
||||
let list = audioFormat[audioFormat.length - 1].split('<SegmentList>')[1].split('</SegmentList>')[0]
|
||||
this.segments_urls = list.replace(new RegExp('<SegmentURL media="', 'g'), '').split('"/>')
|
||||
if(this.segments_urls[this.segments_urls.length - 1] === '') this.segments_urls.pop()
|
||||
private async dash_getter() {
|
||||
const response = await request(this.url);
|
||||
const audioFormat = response
|
||||
.split('<AdaptationSet id="0"')[1]
|
||||
.split('</AdaptationSet>')[0]
|
||||
.split('</Representation>');
|
||||
if (audioFormat[audioFormat.length - 1] === '') audioFormat.pop();
|
||||
this.base_url = audioFormat[audioFormat.length - 1].split('<BaseURL>')[1].split('</BaseURL>')[0];
|
||||
const list = audioFormat[audioFormat.length - 1].split('<SegmentList>')[1].split('</SegmentList>')[0];
|
||||
this.segments_urls = list.replace(new RegExp('<SegmentURL media="', 'g'), '').split('"/>');
|
||||
if (this.segments_urls[this.segments_urls.length - 1] === '') this.segments_urls.pop();
|
||||
}
|
||||
|
||||
private cleanup(){
|
||||
clearTimeout(this.timer as NodeJS.Timer)
|
||||
clearTimeout(this.dash_timer as NodeJS.Timer)
|
||||
this.request?.unpipe(this.stream)
|
||||
this.request?.destroy()
|
||||
this.dash_timer = null
|
||||
this.video_url = ''
|
||||
this.request = null
|
||||
this.timer = null
|
||||
this.url = ''
|
||||
this.base_url = ''
|
||||
this.segments_urls = []
|
||||
this.packet_count = 0
|
||||
this.interval = 0
|
||||
private cleanup() {
|
||||
clearTimeout(this.timer as NodeJS.Timer);
|
||||
clearTimeout(this.dash_timer as NodeJS.Timer);
|
||||
this.request?.unpipe(this.stream);
|
||||
this.request?.destroy();
|
||||
this.dash_timer = null;
|
||||
this.video_url = '';
|
||||
this.request = null;
|
||||
this.timer = null;
|
||||
this.url = '';
|
||||
this.base_url = '';
|
||||
this.segments_urls = [];
|
||||
this.packet_count = 0;
|
||||
this.interval = 0;
|
||||
}
|
||||
|
||||
private async start(){
|
||||
if(this.stream.destroyed){
|
||||
this.cleanup()
|
||||
return
|
||||
private async start() {
|
||||
if (this.stream.destroyed) {
|
||||
this.cleanup();
|
||||
return;
|
||||
}
|
||||
await this.dash_getter()
|
||||
if(this.segments_urls.length > 3) this.segments_urls.splice(0, this.segments_urls.length - 3)
|
||||
if(this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('sq/')[1].split('/')[0])
|
||||
for await (let segment of this.segments_urls){
|
||||
if(Number(segment.split('sq/')[1].split('/')[0]) !== this.packet_count){
|
||||
continue
|
||||
await this.dash_getter();
|
||||
if (this.segments_urls.length > 3) this.segments_urls.splice(0, this.segments_urls.length - 3);
|
||||
if (this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('sq/')[1].split('/')[0]);
|
||||
for await (const segment of this.segments_urls) {
|
||||
if (Number(segment.split('sq/')[1].split('/')[0]) !== this.packet_count) {
|
||||
continue;
|
||||
}
|
||||
await new Promise(async(resolve, reject) => {
|
||||
let stream = await request_stream(this.base_url + segment).catch((err: Error) => err)
|
||||
if(stream instanceof Error){
|
||||
this.stream.emit('error', stream)
|
||||
return
|
||||
await new Promise(async (resolve, reject) => {
|
||||
const stream = await request_stream(this.base_url + segment).catch((err: Error) => err);
|
||||
if (stream instanceof Error) {
|
||||
this.stream.emit('error', stream);
|
||||
return;
|
||||
}
|
||||
this.request = stream
|
||||
stream.pipe(this.stream, { end : false })
|
||||
this.request = stream;
|
||||
stream.pipe(this.stream, { end: false });
|
||||
stream.on('end', () => {
|
||||
this.packet_count++
|
||||
resolve('')
|
||||
})
|
||||
this.packet_count++;
|
||||
resolve('');
|
||||
});
|
||||
stream.once('error', (err) => {
|
||||
this.stream.emit('error', err)
|
||||
})
|
||||
})
|
||||
this.stream.emit('error', err);
|
||||
});
|
||||
});
|
||||
}
|
||||
this.timer = setTimeout(() => {
|
||||
this.start()
|
||||
}, this.interval)
|
||||
this.start();
|
||||
}, this.interval);
|
||||
}
|
||||
}
|
||||
|
||||
export class Stream {
|
||||
type : StreamType
|
||||
stream : PassThrough
|
||||
private url : string
|
||||
private bytes_count : number;
|
||||
private per_sec_bytes : number;
|
||||
private content_length : number;
|
||||
private video_url : string;
|
||||
private timer : NodeJS.Timer | null;
|
||||
private cookie : string;
|
||||
private data_ended : boolean;
|
||||
private playing_count : number;
|
||||
private request : IncomingMessage | null
|
||||
constructor(url : string, type : StreamType, duration : number, contentLength : number, video_url : string, cookie : string){
|
||||
this.url = url
|
||||
this.type = type
|
||||
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
||||
this.bytes_count = 0
|
||||
this.video_url = video_url
|
||||
this.cookie = cookie
|
||||
type: StreamType;
|
||||
stream: PassThrough;
|
||||
private url: string;
|
||||
private bytes_count: number;
|
||||
private per_sec_bytes: number;
|
||||
private content_length: number;
|
||||
private video_url: string;
|
||||
private timer: NodeJS.Timer | null;
|
||||
private cookie: string;
|
||||
private data_ended: boolean;
|
||||
private playing_count: number;
|
||||
private request: IncomingMessage | null;
|
||||
constructor(
|
||||
url: string,
|
||||
type: StreamType,
|
||||
duration: number,
|
||||
contentLength: number,
|
||||
video_url: string,
|
||||
cookie: string
|
||||
) {
|
||||
this.url = url;
|
||||
this.type = type;
|
||||
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 });
|
||||
this.bytes_count = 0;
|
||||
this.video_url = video_url;
|
||||
this.cookie = cookie;
|
||||
this.timer = setInterval(() => {
|
||||
this.retry()
|
||||
}, 7200 * 1000)
|
||||
this.per_sec_bytes = Math.ceil(contentLength / duration)
|
||||
this.content_length = contentLength
|
||||
this.request = null
|
||||
this.data_ended = false
|
||||
this.playing_count = 0
|
||||
this.retry();
|
||||
}, 7200 * 1000);
|
||||
this.per_sec_bytes = Math.ceil(contentLength / duration);
|
||||
this.content_length = contentLength;
|
||||
this.request = null;
|
||||
this.data_ended = false;
|
||||
this.playing_count = 0;
|
||||
this.stream.on('close', () => {
|
||||
this.cleanup()
|
||||
})
|
||||
this.cleanup();
|
||||
});
|
||||
this.stream.on('pause', () => {
|
||||
this.playing_count++;
|
||||
if(this.data_ended){
|
||||
this.bytes_count = 0
|
||||
this.per_sec_bytes = 0
|
||||
this.cleanup()
|
||||
this.stream.removeAllListeners('pause')
|
||||
if (this.data_ended) {
|
||||
this.bytes_count = 0;
|
||||
this.per_sec_bytes = 0;
|
||||
this.cleanup();
|
||||
this.stream.removeAllListeners('pause');
|
||||
} else if (this.playing_count === 280) {
|
||||
this.playing_count = 0;
|
||||
this.loop();
|
||||
}
|
||||
else if(this.playing_count === 280){
|
||||
this.playing_count = 0
|
||||
this.loop()
|
||||
}
|
||||
})
|
||||
this.loop()
|
||||
});
|
||||
this.loop();
|
||||
}
|
||||
|
||||
private async retry(){
|
||||
let info = await video_info(this.video_url, this.cookie)
|
||||
this.url = info.format[info.format.length - 1].url
|
||||
private async retry() {
|
||||
const info = await video_info(this.video_url, this.cookie);
|
||||
this.url = info.format[info.format.length - 1].url;
|
||||
}
|
||||
|
||||
private cleanup(){
|
||||
clearInterval(this.timer as NodeJS.Timer)
|
||||
this.request?.unpipe(this.stream)
|
||||
this.request?.destroy()
|
||||
this.timer = null
|
||||
this.request = null
|
||||
this.url = ''
|
||||
private cleanup() {
|
||||
clearInterval(this.timer as NodeJS.Timer);
|
||||
this.request?.unpipe(this.stream);
|
||||
this.request?.destroy();
|
||||
this.timer = null;
|
||||
this.request = null;
|
||||
this.url = '';
|
||||
}
|
||||
|
||||
private async loop(){
|
||||
if(this.stream.destroyed){
|
||||
this.cleanup()
|
||||
return
|
||||
private async loop() {
|
||||
if (this.stream.destroyed) {
|
||||
this.cleanup();
|
||||
return;
|
||||
}
|
||||
let end : number = this.bytes_count + this.per_sec_bytes * 300;
|
||||
let stream = await request_stream(this.url, {
|
||||
headers : {
|
||||
"range" : `bytes=${this.bytes_count}-${end >= this.content_length ? '' : end}`
|
||||
const end: number = this.bytes_count + this.per_sec_bytes * 300;
|
||||
const stream = await request_stream(this.url, {
|
||||
headers: {
|
||||
range: `bytes=${this.bytes_count}-${end >= this.content_length ? '' : end}`
|
||||
}
|
||||
}).catch((err: Error) => err)
|
||||
if(stream instanceof Error){
|
||||
this.stream.emit('error', stream)
|
||||
this.data_ended = true
|
||||
this.bytes_count = 0
|
||||
this.per_sec_bytes = 0
|
||||
this.cleanup()
|
||||
return
|
||||
}).catch((err: Error) => err);
|
||||
if (stream instanceof Error) {
|
||||
this.stream.emit('error', stream);
|
||||
this.data_ended = true;
|
||||
this.bytes_count = 0;
|
||||
this.per_sec_bytes = 0;
|
||||
this.cleanup();
|
||||
return;
|
||||
}
|
||||
if(Number(stream.statusCode) >= 400){
|
||||
this.cleanup()
|
||||
await this.retry()
|
||||
this.loop()
|
||||
if(!this.timer){
|
||||
if (Number(stream.statusCode) >= 400) {
|
||||
this.cleanup();
|
||||
await this.retry();
|
||||
this.loop();
|
||||
if (!this.timer) {
|
||||
this.timer = setInterval(() => {
|
||||
this.retry()
|
||||
}, 7200 * 1000)
|
||||
this.retry();
|
||||
}, 7200 * 1000);
|
||||
}
|
||||
return
|
||||
return;
|
||||
}
|
||||
this.request = stream
|
||||
stream.pipe(this.stream, { end : false })
|
||||
this.request = stream;
|
||||
stream.pipe(this.stream, { end: false });
|
||||
|
||||
stream.once('error', (err) => {
|
||||
this.stream.emit('error', err)
|
||||
})
|
||||
this.stream.emit('error', err);
|
||||
});
|
||||
|
||||
stream.on('data', (chunk: any) => {
|
||||
this.bytes_count += chunk.length
|
||||
})
|
||||
this.bytes_count += chunk.length;
|
||||
});
|
||||
|
||||
stream.on('end', () => {
|
||||
if(end >= this.content_length) this.data_ended = true
|
||||
})
|
||||
if (end >= this.content_length) this.data_ended = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { getPlaylistVideos, getContinuationToken } from "../utils/extractor";
|
||||
import { request } from "../utils/request";
|
||||
import { Thumbnail } from "./Thumbnail";
|
||||
import { Channel } from "./Channel";
|
||||
import { Video } from "./Video";
|
||||
const BASE_API = "https://www.youtube.com/youtubei/v1/browse?key=";
|
||||
import { getPlaylistVideos, getContinuationToken } from '../utils/extractor';
|
||||
import { request } from '../utils/request';
|
||||
import { Thumbnail } from './Thumbnail';
|
||||
import { Channel } from './Channel';
|
||||
import { Video } from './Video';
|
||||
const BASE_API = 'https://www.youtube.com/youtubei/v1/browse?key=';
|
||||
|
||||
export class PlayList{
|
||||
export class PlayList {
|
||||
id?: string;
|
||||
title?: string;
|
||||
videoCount?: number;
|
||||
@@ -16,19 +16,23 @@ export class PlayList{
|
||||
channel?: Channel;
|
||||
thumbnail?: Thumbnail;
|
||||
private videos?: [];
|
||||
private fetched_videos : Map<string, Video[]>
|
||||
private _continuation: { api?: string; token?: string; clientVersion?: string } = {};
|
||||
private __count : number
|
||||
private fetched_videos: Map<string, Video[]>;
|
||||
private _continuation: {
|
||||
api?: string;
|
||||
token?: string;
|
||||
clientVersion?: string;
|
||||
} = {};
|
||||
private __count: number;
|
||||
|
||||
constructor(data : any, searchResult : Boolean = false){
|
||||
constructor(data: any, searchResult = false) {
|
||||
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
|
||||
this.__count = 0
|
||||
this.fetched_videos = new Map()
|
||||
if(searchResult) this.__patchSearch(data)
|
||||
else this.__patch(data)
|
||||
this.__count = 0;
|
||||
this.fetched_videos = new Map();
|
||||
if (searchResult) this.__patchSearch(data);
|
||||
else this.__patch(data);
|
||||
}
|
||||
|
||||
private __patch(data:any){
|
||||
private __patch(data: any) {
|
||||
this.id = data.id || undefined;
|
||||
this.url = data.url || undefined;
|
||||
this.title = data.title || undefined;
|
||||
@@ -39,14 +43,14 @@ export class PlayList{
|
||||
this.channel = data.author || undefined;
|
||||
this.thumbnail = data.thumbnail || undefined;
|
||||
this.videos = data.videos || [];
|
||||
this.__count ++
|
||||
this.fetched_videos.set(`page${this.__count}`, this.videos as Video[])
|
||||
this.__count++;
|
||||
this.fetched_videos.set(`page${this.__count}`, this.videos as Video[]);
|
||||
this._continuation.api = data.continuation?.api ?? undefined;
|
||||
this._continuation.token = data.continuation?.token ?? undefined;
|
||||
this._continuation.clientVersion = data.continuation?.clientVersion ?? "<important data>";
|
||||
this._continuation.clientVersion = data.continuation?.clientVersion ?? '<important data>';
|
||||
}
|
||||
|
||||
private __patchSearch(data: any){
|
||||
private __patchSearch(data: any) {
|
||||
this.id = data.id || undefined;
|
||||
this.url = this.id ? `https://www.youtube.com/playlist?list=${this.id}` : undefined;
|
||||
this.title = data.title || undefined;
|
||||
@@ -59,19 +63,19 @@ export class PlayList{
|
||||
this.views = 0;
|
||||
}
|
||||
|
||||
async next(limit: number = Infinity): Promise<Video[]> {
|
||||
async next(limit = Infinity): Promise<Video[]> {
|
||||
if (!this._continuation || !this._continuation.token) return [];
|
||||
|
||||
let nextPage = await request(`${BASE_API}${this._continuation.api}`, {
|
||||
method: "POST",
|
||||
const nextPage = await request(`${BASE_API}${this._continuation.api}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
continuation: this._continuation.token,
|
||||
context: {
|
||||
client: {
|
||||
utcOffsetMinutes: 0,
|
||||
gl: "US",
|
||||
hl: "en",
|
||||
clientName: "WEB",
|
||||
gl: 'US',
|
||||
hl: 'en',
|
||||
clientName: 'WEB',
|
||||
clientVersion: this._continuation.clientVersion
|
||||
},
|
||||
user: {},
|
||||
@@ -79,24 +83,25 @@ export class PlayList{
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
let contents = JSON.parse(nextPage)?.onResponseReceivedActions[0]?.appendContinuationItemsAction?.continuationItems
|
||||
if(!contents) return []
|
||||
|
||||
let playlist_videos = getPlaylistVideos(contents, limit)
|
||||
this.fetched_videos.set(`page${this.__count}`, playlist_videos)
|
||||
this._continuation.token = getContinuationToken(contents)
|
||||
return playlist_videos
|
||||
const contents =
|
||||
JSON.parse(nextPage)?.onResponseReceivedActions[0]?.appendContinuationItemsAction?.continuationItems;
|
||||
if (!contents) return [];
|
||||
|
||||
const playlist_videos = getPlaylistVideos(contents, limit);
|
||||
this.fetched_videos.set(`page${this.__count}`, playlist_videos);
|
||||
this._continuation.token = getContinuationToken(contents);
|
||||
return playlist_videos;
|
||||
}
|
||||
|
||||
async fetch(max: number = Infinity) {
|
||||
let continuation = this._continuation.token;
|
||||
async fetch(max = Infinity) {
|
||||
const continuation = this._continuation.token;
|
||||
if (!continuation) return this;
|
||||
if (max < 1) max = Infinity;
|
||||
|
||||
while (typeof this._continuation.token === "string" && this._continuation.token.length) {
|
||||
if (this.videos?.length as number >= max) break;
|
||||
this.__count++
|
||||
while (typeof this._continuation.token === 'string' && this._continuation.token.length) {
|
||||
if ((this.videos?.length as number) >= max) break;
|
||||
this.__count++;
|
||||
const res = await this.next();
|
||||
if (!res.length) break;
|
||||
}
|
||||
@@ -104,23 +109,23 @@ export class PlayList{
|
||||
return this;
|
||||
}
|
||||
|
||||
get type(): "playlist" {
|
||||
return "playlist";
|
||||
get type(): 'playlist' {
|
||||
return 'playlist';
|
||||
}
|
||||
|
||||
page(number : number): Video[]{
|
||||
if(!number) throw new Error('Page number is not provided')
|
||||
if(!this.fetched_videos.has(`page${number}`)) throw new Error('Given Page number is invalid')
|
||||
return this.fetched_videos.get(`page${number}`) as Video[]
|
||||
page(number: number): Video[] {
|
||||
if (!number) throw new Error('Page number is not provided');
|
||||
if (!this.fetched_videos.has(`page${number}`)) throw new Error('Given Page number is invalid');
|
||||
return this.fetched_videos.get(`page${number}`) as Video[];
|
||||
}
|
||||
|
||||
get total_pages(){
|
||||
return this.fetched_videos.size
|
||||
get total_pages() {
|
||||
return this.fetched_videos.size;
|
||||
}
|
||||
|
||||
get total_videos(){
|
||||
let page_number: number = this.total_pages
|
||||
return (page_number - 1) * 100 + (this.fetched_videos.get(`page${page_number}`) as Video[]).length
|
||||
get total_videos() {
|
||||
const page_number: number = this.total_pages;
|
||||
return (page_number - 1) * 100 + (this.fetched_videos.get(`page${page_number}`) as Video[]).length;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
@@ -129,12 +134,12 @@ export class PlayList{
|
||||
title: this.title,
|
||||
thumbnail: this.thumbnail,
|
||||
channel: {
|
||||
name : this.channel?.name,
|
||||
id : this.channel?.id,
|
||||
icon : this.channel?.iconURL()
|
||||
name: this.channel?.name,
|
||||
id: this.channel?.id,
|
||||
icon: this.channel?.iconURL()
|
||||
},
|
||||
url: this.url,
|
||||
videos: this.videos
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
type ThumbnailType = "default" | "hqdefault" | "mqdefault" | "sddefault" | "maxresdefault" | "ultrares";
|
||||
type ThumbnailType = 'default' | 'hqdefault' | 'mqdefault' | 'sddefault' | 'maxresdefault' | 'ultrares';
|
||||
|
||||
export class Thumbnail {
|
||||
id?: string;
|
||||
@@ -21,20 +21,21 @@ export class Thumbnail {
|
||||
this.url = data.url || undefined;
|
||||
}
|
||||
|
||||
displayThumbnailURL(thumbnailType: ThumbnailType = "maxresdefault"): string {
|
||||
if (!["default", "hqdefault", "mqdefault", "sddefault", "maxresdefault", "ultrares"].includes(thumbnailType)) throw new Error(`Invalid thumbnail type "${thumbnailType}"!`);
|
||||
if (thumbnailType === "ultrares") return this.url as string;
|
||||
displayThumbnailURL(thumbnailType: ThumbnailType = 'maxresdefault'): string {
|
||||
if (!['default', 'hqdefault', 'mqdefault', 'sddefault', 'maxresdefault', 'ultrares'].includes(thumbnailType))
|
||||
throw new Error(`Invalid thumbnail type "${thumbnailType}"!`);
|
||||
if (thumbnailType === 'ultrares') return this.url as string;
|
||||
return `https://i3.ytimg.com/vi/${this.id}/${thumbnailType}.jpg`;
|
||||
}
|
||||
|
||||
defaultThumbnailURL(id: "0" | "1" | "2" | "3" | "4"): string {
|
||||
if (!id) id = "0";
|
||||
if (!["0", "1", "2", "3", "4"].includes(id)) throw new Error(`Invalid thumbnail id "${id}"!`);
|
||||
defaultThumbnailURL(id: '0' | '1' | '2' | '3' | '4'): string {
|
||||
if (!id) id = '0';
|
||||
if (!['0', '1', '2', '3', '4'].includes(id)) throw new Error(`Invalid thumbnail id "${id}"!`);
|
||||
return `https://i3.ytimg.com/vi/${this.id}/${id}.jpg`;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.url ? `${this.url}` : "";
|
||||
return this.url ? `${this.url}` : '';
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Channel } from "./Channel";
|
||||
import { Thumbnail } from "./Thumbnail";
|
||||
import { Channel } from './Channel';
|
||||
import { Thumbnail } from './Thumbnail';
|
||||
|
||||
interface VideoOptions {
|
||||
id?: string;
|
||||
url? : string;
|
||||
url?: string;
|
||||
title?: string;
|
||||
description?: string;
|
||||
durationRaw: string;
|
||||
@@ -12,21 +12,21 @@ interface VideoOptions {
|
||||
views: number;
|
||||
thumbnail?: {
|
||||
id: string | undefined;
|
||||
width: number | undefined ;
|
||||
width: number | undefined;
|
||||
height: number | undefined;
|
||||
url: string | undefined;
|
||||
};
|
||||
channel?: {
|
||||
name : string,
|
||||
id : string,
|
||||
icon : string
|
||||
name: string;
|
||||
id: string;
|
||||
icon: string;
|
||||
};
|
||||
videos?: Video[];
|
||||
type : string;
|
||||
ratings : {
|
||||
type: string;
|
||||
ratings: {
|
||||
likes: number;
|
||||
dislikes: number;
|
||||
}
|
||||
};
|
||||
live: boolean;
|
||||
private: boolean;
|
||||
tags: string[];
|
||||
@@ -34,7 +34,7 @@ interface VideoOptions {
|
||||
|
||||
export class Video {
|
||||
id?: string;
|
||||
url? : string;
|
||||
url?: string;
|
||||
title?: string;
|
||||
description?: string;
|
||||
durationRaw: string;
|
||||
@@ -50,35 +50,35 @@ export class Video {
|
||||
private: boolean;
|
||||
tags: string[];
|
||||
|
||||
constructor(data : any){
|
||||
if(!data) throw new Error(`Can not initiate ${this.constructor.name} without data`)
|
||||
|
||||
constructor(data: any) {
|
||||
if (!data) throw new Error(`Can not initiate ${this.constructor.name} without data`);
|
||||
|
||||
this.id = data.id || undefined;
|
||||
this.url = `https://www.youtube.com/watch?v=${this.id}`
|
||||
this.url = `https://www.youtube.com/watch?v=${this.id}`;
|
||||
this.title = data.title || undefined;
|
||||
this.description = data.description || undefined;
|
||||
this.durationRaw = data.duration_raw || "0:00";
|
||||
this.durationRaw = data.duration_raw || '0:00';
|
||||
this.durationInSec = (data.duration < 0 ? 0 : data.duration) || 0;
|
||||
this.uploadedAt = data.uploadedAt || undefined;
|
||||
this.views = parseInt(data.views) || 0;
|
||||
this.thumbnail = data.thumbnail || {};
|
||||
this.channel = data.channel || {};
|
||||
this.likes = data.ratings?.likes as number || 0;
|
||||
this.likes = (data.ratings?.likes as number) || 0;
|
||||
this.dislikes = data.ratings?.dislikes || 0;
|
||||
this.live = !!data.live;
|
||||
this.private = !!data.private;
|
||||
this.tags = data.tags || [];
|
||||
}
|
||||
|
||||
get type(): "video" {
|
||||
return "video";
|
||||
get type(): 'video' {
|
||||
return 'video';
|
||||
}
|
||||
|
||||
get toString(): string {
|
||||
return this.url || "";
|
||||
return this.url || '';
|
||||
}
|
||||
|
||||
get toJSON(): VideoOptions{
|
||||
get toJSON(): VideoOptions {
|
||||
return {
|
||||
id: this.id,
|
||||
url: this.url,
|
||||
@@ -104,4 +104,4 @@ export class Video {
|
||||
private: this.private
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export { search } from './search'
|
||||
export { stream, stream_from_info } from './stream'
|
||||
export * from './utils'
|
||||
export { search } from './search';
|
||||
export { stream, stream_from_info } from './stream';
|
||||
export * from './utils';
|
||||
|
||||
@@ -1,36 +1,38 @@
|
||||
import { request } from "./utils/request";
|
||||
import { ParseSearchInterface, ParseSearchResult } from "./utils/parser";
|
||||
import { Video } from "./classes/Video";
|
||||
import { Channel } from "./classes/Channel";
|
||||
import { PlayList } from "./classes/Playlist";
|
||||
|
||||
import { request } from './utils/request';
|
||||
import { ParseSearchInterface, ParseSearchResult } from './utils/parser';
|
||||
import { Video } from './classes/Video';
|
||||
import { Channel } from './classes/Channel';
|
||||
import { PlayList } from './classes/Playlist';
|
||||
|
||||
enum SearchType {
|
||||
Video = 'EgIQAQ%253D%253D',
|
||||
PlayList = 'EgIQAw%253D%253D',
|
||||
Channel = 'EgIQAg%253D%253D',
|
||||
Channel = 'EgIQAg%253D%253D'
|
||||
}
|
||||
|
||||
export async function search(search :string, options : ParseSearchInterface = {}): Promise<(Video | Channel | PlayList)[]> {
|
||||
let url = 'https://www.youtube.com/results?search_query=' + search.replaceAll(' ', '+')
|
||||
options.type ??= "video"
|
||||
if(!url.match('&sp=')){
|
||||
url += '&sp='
|
||||
switch(options?.type){
|
||||
export async function search(
|
||||
search: string,
|
||||
options: ParseSearchInterface = {}
|
||||
): Promise<(Video | Channel | PlayList)[]> {
|
||||
let url = 'https://www.youtube.com/results?search_query=' + search.replaceAll(' ', '+');
|
||||
options.type ??= 'video';
|
||||
if (!url.match('&sp=')) {
|
||||
url += '&sp=';
|
||||
switch (options?.type) {
|
||||
case 'channel':
|
||||
url += SearchType.Channel
|
||||
break
|
||||
url += SearchType.Channel;
|
||||
break;
|
||||
case 'playlist':
|
||||
url += SearchType.PlayList
|
||||
break
|
||||
url += SearchType.PlayList;
|
||||
break;
|
||||
case 'video':
|
||||
url += SearchType.Video
|
||||
break
|
||||
url += SearchType.Video;
|
||||
break;
|
||||
}
|
||||
}
|
||||
let body = await request(url, {
|
||||
headers : {'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7'}
|
||||
})
|
||||
let data = ParseSearchResult(body, options)
|
||||
return data
|
||||
}
|
||||
const body = await request(url, {
|
||||
headers: { 'accept-language': 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7' }
|
||||
});
|
||||
const data = ParseSearchResult(body, options);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -1,97 +1,123 @@
|
||||
import { video_info } from "."
|
||||
import { LiveStreaming, Stream } from "./classes/LiveStream"
|
||||
import { video_info } from '.';
|
||||
import { LiveStreaming, Stream } from './classes/LiveStream';
|
||||
|
||||
export enum StreamType{
|
||||
export enum StreamType {
|
||||
Arbitrary = 'arbitrary',
|
||||
Raw = 'raw',
|
||||
OggOpus = 'ogg/opus',
|
||||
WebmOpus = 'webm/opus',
|
||||
Opus = 'opus',
|
||||
Opus = 'opus'
|
||||
}
|
||||
|
||||
interface InfoData{
|
||||
LiveStreamData : {
|
||||
isLive : boolean
|
||||
dashManifestUrl : string
|
||||
hlsManifestUrl : string
|
||||
}
|
||||
html5player : string
|
||||
format : any[]
|
||||
video_details : any
|
||||
interface InfoData {
|
||||
LiveStreamData: {
|
||||
isLive: boolean;
|
||||
dashManifestUrl: string;
|
||||
hlsManifestUrl: string;
|
||||
};
|
||||
html5player: string;
|
||||
format: any[];
|
||||
video_details: any;
|
||||
}
|
||||
|
||||
function parseAudioFormats(formats : any[]){
|
||||
let result: any[] = []
|
||||
function parseAudioFormats(formats: any[]) {
|
||||
const result: any[] = [];
|
||||
formats.forEach((format) => {
|
||||
let type = format.mimeType as string
|
||||
if(type.startsWith('audio')){
|
||||
format.codec = type.split('codecs="')[1].split('"')[0]
|
||||
format.container = type.split('audio/')[1].split(';')[0]
|
||||
result.push(format)
|
||||
const type = format.mimeType as string;
|
||||
if (type.startsWith('audio')) {
|
||||
format.codec = type.split('codecs="')[1].split('"')[0];
|
||||
format.container = type.split('audio/')[1].split(';')[0];
|
||||
result.push(format);
|
||||
}
|
||||
})
|
||||
return result
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function stream(url : string, cookie? : string): Promise<Stream | LiveStreaming>{
|
||||
let info = await video_info(url, cookie)
|
||||
let final: any[] = [];
|
||||
let type : StreamType;
|
||||
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') {
|
||||
return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url)
|
||||
export async function stream(url: string, cookie?: string): Promise<Stream | LiveStreaming> {
|
||||
const info = await video_info(url, cookie);
|
||||
const final: any[] = [];
|
||||
let type: StreamType;
|
||||
if (
|
||||
info.LiveStreamData.isLive === true &&
|
||||
info.LiveStreamData.hlsManifestUrl !== null &&
|
||||
info.video_details.durationInSec === '0'
|
||||
) {
|
||||
return new LiveStreaming(
|
||||
info.LiveStreamData.dashManifestUrl,
|
||||
info.format[info.format.length - 1].targetDurationSec,
|
||||
info.video_details.url
|
||||
);
|
||||
}
|
||||
|
||||
let audioFormat = parseAudioFormats(info.format)
|
||||
let opusFormats = filterFormat(audioFormat, "opus")
|
||||
const audioFormat = parseAudioFormats(info.format);
|
||||
const opusFormats = filterFormat(audioFormat, 'opus');
|
||||
|
||||
if(opusFormats.length === 0){
|
||||
type = StreamType.Arbitrary
|
||||
if(audioFormat.length === 0){
|
||||
final.push(info.format[info.format.length - 1])
|
||||
}
|
||||
else{
|
||||
final.push(audioFormat[audioFormat.length - 1])
|
||||
}
|
||||
if (opusFormats.length === 0) {
|
||||
type = StreamType.Arbitrary;
|
||||
if (audioFormat.length === 0) {
|
||||
final.push(info.format[info.format.length - 1]);
|
||||
} else {
|
||||
final.push(audioFormat[audioFormat.length - 1]);
|
||||
}
|
||||
} else {
|
||||
type = StreamType.WebmOpus;
|
||||
final.push(opusFormats[opusFormats.length - 1]);
|
||||
}
|
||||
else{
|
||||
type = StreamType.WebmOpus
|
||||
final.push(opusFormats[opusFormats.length - 1])
|
||||
}
|
||||
|
||||
return new Stream(final[0].url, type, info.video_details.durationInSec, Number(final[0].contentLength), info.video_details.url, cookie as string)
|
||||
|
||||
return new Stream(
|
||||
final[0].url,
|
||||
type,
|
||||
info.video_details.durationInSec,
|
||||
Number(final[0].contentLength),
|
||||
info.video_details.url,
|
||||
cookie as string
|
||||
);
|
||||
}
|
||||
|
||||
export async function stream_from_info(info : InfoData, cookie? : string): Promise<Stream | LiveStreaming>{
|
||||
let final: any[] = [];
|
||||
let type : StreamType;
|
||||
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') {
|
||||
return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url)
|
||||
export async function stream_from_info(info: InfoData, cookie?: string): Promise<Stream | LiveStreaming> {
|
||||
const final: any[] = [];
|
||||
let type: StreamType;
|
||||
if (
|
||||
info.LiveStreamData.isLive === true &&
|
||||
info.LiveStreamData.hlsManifestUrl !== null &&
|
||||
info.video_details.durationInSec === '0'
|
||||
) {
|
||||
return new LiveStreaming(
|
||||
info.LiveStreamData.dashManifestUrl,
|
||||
info.format[info.format.length - 1].targetDurationSec,
|
||||
info.video_details.url
|
||||
);
|
||||
}
|
||||
|
||||
let audioFormat = parseAudioFormats(info.format)
|
||||
let opusFormats = filterFormat(audioFormat, "opus")
|
||||
const audioFormat = parseAudioFormats(info.format);
|
||||
const opusFormats = filterFormat(audioFormat, 'opus');
|
||||
|
||||
if(opusFormats.length === 0){
|
||||
type = StreamType.Arbitrary
|
||||
if(audioFormat.length === 0){
|
||||
final.push(info.format[info.format.length - 1])
|
||||
}
|
||||
else{
|
||||
final.push(audioFormat[audioFormat.length - 1])
|
||||
}
|
||||
if (opusFormats.length === 0) {
|
||||
type = StreamType.Arbitrary;
|
||||
if (audioFormat.length === 0) {
|
||||
final.push(info.format[info.format.length - 1]);
|
||||
} else {
|
||||
final.push(audioFormat[audioFormat.length - 1]);
|
||||
}
|
||||
} else {
|
||||
type = StreamType.WebmOpus;
|
||||
final.push(opusFormats[opusFormats.length - 1]);
|
||||
}
|
||||
else{
|
||||
type = StreamType.WebmOpus
|
||||
final.push(opusFormats[opusFormats.length - 1])
|
||||
}
|
||||
|
||||
return new Stream(final[0].url, type, info.video_details.durationInSec, Number(final[0].contentLength), info.video_details.url, cookie as string)
|
||||
|
||||
return new Stream(
|
||||
final[0].url,
|
||||
type,
|
||||
info.video_details.durationInSec,
|
||||
Number(final[0].contentLength),
|
||||
info.video_details.url,
|
||||
cookie as string
|
||||
);
|
||||
}
|
||||
|
||||
function filterFormat(formats : any[], codec : string){
|
||||
let result: any[] = []
|
||||
function filterFormat(formats: any[], codec: string) {
|
||||
const result: any[] = [];
|
||||
formats.forEach((format) => {
|
||||
if(format.codec === codec) result.push(format)
|
||||
})
|
||||
return result
|
||||
if (format.codec === codec) result.push(format);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { URL } from 'url'
|
||||
import { request } from './request'
|
||||
import querystring from 'querystring'
|
||||
import { URL } from 'url';
|
||||
import { request } from './request';
|
||||
import querystring from 'querystring';
|
||||
|
||||
interface formatOptions {
|
||||
url? : string;
|
||||
sp? : string;
|
||||
signatureCipher? : string;
|
||||
cipher?: string;
|
||||
s? : string;
|
||||
url?: string;
|
||||
sp?: string;
|
||||
signatureCipher?: string;
|
||||
cipher?: string;
|
||||
s?: string;
|
||||
}
|
||||
const var_js = '[a-zA-Z_\\$][a-zA-Z_0-9]*';
|
||||
const singlequote_js = `'[^'\\\\]*(:?\\\\[\\s\\S][^'\\\\]*)*'`;
|
||||
@@ -16,159 +16,140 @@ const quote_js = `(?:${singlequote_js}|${duoblequote_js})`;
|
||||
const key_js = `(?:${var_js}|${quote_js})`;
|
||||
const prop_js = `(?:\\.${var_js}|\\[${quote_js}\\])`;
|
||||
const empty_js = `(?:''|"")`;
|
||||
const reverse_function = ':function\\(a\\)\\{' +
|
||||
'(?:return )?a\\.reverse\\(\\)' +
|
||||
'\\}';
|
||||
const slice_function = ':function\\(a,b\\)\\{' +
|
||||
'return a\\.slice\\(b\\)' +
|
||||
'\\}';
|
||||
const splice_function = ':function\\(a,b\\)\\{' +
|
||||
'a\\.splice\\(0,b\\)' +
|
||||
'\\}';
|
||||
const swap_function = ':function\\(a,b\\)\\{' +
|
||||
'var c=a\\[0\\];a\\[0\\]=a\\[b(?:%a\\.length)?\\];a\\[b(?:%a\\.length)?\\]=c(?:;return a)?' +
|
||||
'\\}';
|
||||
const reverse_function = ':function\\(a\\)\\{' + '(?:return )?a\\.reverse\\(\\)' + '\\}';
|
||||
const slice_function = ':function\\(a,b\\)\\{' + 'return a\\.slice\\(b\\)' + '\\}';
|
||||
const splice_function = ':function\\(a,b\\)\\{' + 'a\\.splice\\(0,b\\)' + '\\}';
|
||||
const swap_function =
|
||||
':function\\(a,b\\)\\{' +
|
||||
'var c=a\\[0\\];a\\[0\\]=a\\[b(?:%a\\.length)?\\];a\\[b(?:%a\\.length)?\\]=c(?:;return a)?' +
|
||||
'\\}';
|
||||
const obj_regexp = new RegExp(
|
||||
`var (${var_js})=\\{((?:(?:${
|
||||
key_js}${reverse_function}|${
|
||||
key_js}${slice_function}|${
|
||||
key_js}${splice_function}|${
|
||||
key_js}${swap_function
|
||||
}),?\\r?\\n?)+)\\};`)
|
||||
const function_regexp = new RegExp(`${`function(?: ${var_js})?\\(a\\)\\{` +
|
||||
`a=a\\.split\\(${empty_js}\\);\\s*` +
|
||||
`((?:(?:a=)?${var_js}`}${
|
||||
prop_js
|
||||
}\\(a,\\d+\\);)+)` +
|
||||
`return a\\.join\\(${empty_js}\\)` +
|
||||
`\\}`);
|
||||
`var (${var_js})=\\{((?:(?:${key_js}${reverse_function}|${key_js}${slice_function}|${key_js}${splice_function}|${key_js}${swap_function}),?\\r?\\n?)+)\\};`
|
||||
);
|
||||
const function_regexp = new RegExp(
|
||||
`${
|
||||
`function(?: ${var_js})?\\(a\\)\\{` + `a=a\\.split\\(${empty_js}\\);\\s*` + `((?:(?:a=)?${var_js}`
|
||||
}${prop_js}\\(a,\\d+\\);)+)` +
|
||||
`return a\\.join\\(${empty_js}\\)` +
|
||||
`\\}`
|
||||
);
|
||||
const reverse_regexp = new RegExp(`(?:^|,)(${key_js})${reverse_function}`, 'm');
|
||||
const slice_regexp = new RegExp(`(?:^|,)(${key_js})${slice_function}`, 'm');
|
||||
const splice_regexp = new RegExp(`(?:^|,)(${key_js})${splice_function}`, 'm');
|
||||
const swap_regexp = new RegExp(`(?:^|,)(${key_js})${swap_function}`, 'm');
|
||||
|
||||
export function js_tokens( body:string ) {
|
||||
let function_action = function_regexp.exec(body)
|
||||
let object_action = obj_regexp.exec(body)
|
||||
if(!function_action || !object_action) return null
|
||||
export function js_tokens(body: string) {
|
||||
const function_action = function_regexp.exec(body);
|
||||
const object_action = obj_regexp.exec(body);
|
||||
if (!function_action || !object_action) return null;
|
||||
|
||||
let object = object_action[1].replace(/\$/g, '\\$')
|
||||
let object_body = object_action[2].replace(/\$/g, '\\$')
|
||||
let function_body = function_action[1].replace(/\$/g, '\\$')
|
||||
const object = object_action[1].replace(/\$/g, '\\$');
|
||||
const object_body = object_action[2].replace(/\$/g, '\\$');
|
||||
const function_body = function_action[1].replace(/\$/g, '\\$');
|
||||
|
||||
let result = reverse_regexp.exec(object_body);
|
||||
const reverseKey = result && result[1]
|
||||
.replace(/\$/g, '\\$')
|
||||
.replace(/\$|^'|^"|'$|"$/g, '');
|
||||
|
||||
result = slice_regexp.exec(object_body)
|
||||
const sliceKey = result && result[1]
|
||||
.replace(/\$/g, '\\$')
|
||||
.replace(/\$|^'|^"|'$|"$/g, '');
|
||||
let result = reverse_regexp.exec(object_body);
|
||||
const reverseKey = result && result[1].replace(/\$/g, '\\$').replace(/\$|^'|^"|'$|"$/g, '');
|
||||
|
||||
result = splice_regexp.exec(object_body);
|
||||
const spliceKey = result && result[1]
|
||||
.replace(/\$/g, '\\$')
|
||||
.replace(/\$|^'|^"|'$|"$/g, '');
|
||||
result = slice_regexp.exec(object_body);
|
||||
const sliceKey = result && result[1].replace(/\$/g, '\\$').replace(/\$|^'|^"|'$|"$/g, '');
|
||||
|
||||
result = swap_regexp.exec(object_body);
|
||||
const swapKey = result && result[1]
|
||||
.replace(/\$/g, '\\$')
|
||||
.replace(/\$|^'|^"|'$|"$/g, '');
|
||||
result = splice_regexp.exec(object_body);
|
||||
const spliceKey = result && result[1].replace(/\$/g, '\\$').replace(/\$|^'|^"|'$|"$/g, '');
|
||||
|
||||
const keys = `(${[reverseKey, sliceKey, spliceKey, swapKey].join('|')})`;
|
||||
const myreg = `(?:a=)?${object
|
||||
}(?:\\.${keys}|\\['${keys}'\\]|\\["${keys}"\\])` +
|
||||
`\\(a,(\\d+)\\)`;
|
||||
const tokenizeRegexp = new RegExp(myreg, 'g');
|
||||
const tokens = [];
|
||||
while((result = tokenizeRegexp.exec(function_body)) !== null){
|
||||
let key = result[1] || result[2] || result[3];
|
||||
switch (key) {
|
||||
case swapKey:
|
||||
tokens.push(`sw${result[4]}`);
|
||||
break;
|
||||
case reverseKey:
|
||||
tokens.push('rv');
|
||||
break;
|
||||
case sliceKey:
|
||||
tokens.push(`sl${result[4]}`);
|
||||
break;
|
||||
case spliceKey:
|
||||
tokens.push(`sp${result[4]}`);
|
||||
break;
|
||||
result = swap_regexp.exec(object_body);
|
||||
const swapKey = result && result[1].replace(/\$/g, '\\$').replace(/\$|^'|^"|'$|"$/g, '');
|
||||
|
||||
const keys = `(${[reverseKey, sliceKey, spliceKey, swapKey].join('|')})`;
|
||||
const myreg = `(?:a=)?${object}(?:\\.${keys}|\\['${keys}'\\]|\\["${keys}"\\])` + `\\(a,(\\d+)\\)`;
|
||||
const tokenizeRegexp = new RegExp(myreg, 'g');
|
||||
const tokens = [];
|
||||
while ((result = tokenizeRegexp.exec(function_body)) !== null) {
|
||||
const key = result[1] || result[2] || result[3];
|
||||
switch (key) {
|
||||
case swapKey:
|
||||
tokens.push(`sw${result[4]}`);
|
||||
break;
|
||||
case reverseKey:
|
||||
tokens.push('rv');
|
||||
break;
|
||||
case sliceKey:
|
||||
tokens.push(`sl${result[4]}`);
|
||||
break;
|
||||
case spliceKey:
|
||||
tokens.push(`sp${result[4]}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return tokens
|
||||
return tokens;
|
||||
}
|
||||
|
||||
function deciper_signature(tokens : string[], signature :string){
|
||||
let sig = signature.split('')
|
||||
let len = tokens.length
|
||||
for(let i = 0; i < len; i++ ){
|
||||
let token = tokens[i], pos;
|
||||
switch(token.slice(0,2)){
|
||||
case 'sw':
|
||||
pos = parseInt(token.slice(2))
|
||||
sig = swappositions(sig, pos)
|
||||
break
|
||||
case 'rv':
|
||||
sig = sig.reverse()
|
||||
break
|
||||
case 'sl':
|
||||
pos = parseInt(token.slice(2))
|
||||
sig = sig.slice(pos)
|
||||
break
|
||||
case 'sp':
|
||||
pos = parseInt(token.slice(2))
|
||||
sig.splice(0, pos)
|
||||
break
|
||||
function deciper_signature(tokens: string[], signature: string) {
|
||||
let sig = signature.split('');
|
||||
const len = tokens.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
let token = tokens[i],
|
||||
pos;
|
||||
switch (token.slice(0, 2)) {
|
||||
case 'sw':
|
||||
pos = parseInt(token.slice(2));
|
||||
sig = swappositions(sig, pos);
|
||||
break;
|
||||
case 'rv':
|
||||
sig = sig.reverse();
|
||||
break;
|
||||
case 'sl':
|
||||
pos = parseInt(token.slice(2));
|
||||
sig = sig.slice(pos);
|
||||
break;
|
||||
case 'sp':
|
||||
pos = parseInt(token.slice(2));
|
||||
sig.splice(0, pos);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sig.join('')
|
||||
return sig.join('');
|
||||
}
|
||||
|
||||
|
||||
function swappositions(array : string[], position : number){
|
||||
let first = array[0]
|
||||
array[0] = array[position]
|
||||
array[position] = first
|
||||
return array
|
||||
function swappositions(array: string[], position: number) {
|
||||
const first = array[0];
|
||||
array[0] = array[position];
|
||||
array[position] = first;
|
||||
return array;
|
||||
}
|
||||
|
||||
function download_url(format: formatOptions, sig : string){
|
||||
let decoded_url;
|
||||
if(!format.url) return;
|
||||
decoded_url = format.url
|
||||
function download_url(format: formatOptions, sig: string) {
|
||||
let decoded_url;
|
||||
if (!format.url) return;
|
||||
decoded_url = format.url;
|
||||
|
||||
decoded_url = decodeURIComponent(decoded_url)
|
||||
decoded_url = decodeURIComponent(decoded_url);
|
||||
|
||||
let parsed_url = new URL(decoded_url)
|
||||
parsed_url.searchParams.set('ratebypass', 'yes');
|
||||
const parsed_url = new URL(decoded_url);
|
||||
parsed_url.searchParams.set('ratebypass', 'yes');
|
||||
|
||||
if(sig){
|
||||
parsed_url.searchParams.set(format.sp || 'signature', sig)
|
||||
}
|
||||
format.url = parsed_url.toString();
|
||||
if (sig) {
|
||||
parsed_url.searchParams.set(format.sp || 'signature', sig);
|
||||
}
|
||||
format.url = parsed_url.toString();
|
||||
}
|
||||
|
||||
export async function format_decipher(formats: formatOptions[], html5player : string){
|
||||
let body = await request(html5player)
|
||||
let tokens = js_tokens(body)
|
||||
formats.forEach((format) => {
|
||||
let cipher = format.signatureCipher || format.cipher;
|
||||
if(cipher){
|
||||
Object.assign(format, querystring.parse(cipher))
|
||||
delete format.signatureCipher;
|
||||
delete format.cipher;
|
||||
}
|
||||
let sig;
|
||||
if(tokens && format.s){
|
||||
sig = deciper_signature(tokens, format.s)
|
||||
download_url(format, sig)
|
||||
delete format.s
|
||||
delete format.sp
|
||||
}
|
||||
});
|
||||
return formats
|
||||
}
|
||||
export async function format_decipher(formats: formatOptions[], html5player: string) {
|
||||
const body = await request(html5player);
|
||||
const tokens = js_tokens(body);
|
||||
formats.forEach((format) => {
|
||||
const cipher = format.signatureCipher || format.cipher;
|
||||
if (cipher) {
|
||||
Object.assign(format, querystring.parse(cipher));
|
||||
delete format.signatureCipher;
|
||||
delete format.cipher;
|
||||
}
|
||||
let sig;
|
||||
if (tokens && format.s) {
|
||||
sig = deciper_signature(tokens, format.s);
|
||||
download_url(format, sig);
|
||||
delete format.s;
|
||||
delete format.sp;
|
||||
}
|
||||
});
|
||||
return formats;
|
||||
}
|
||||
|
||||
@@ -1,166 +1,187 @@
|
||||
import { request } from './request'
|
||||
import { format_decipher, js_tokens } from './cipher'
|
||||
import { Video } from '../classes/Video'
|
||||
import { PlayList } from '../classes/Playlist'
|
||||
import { request } from './request';
|
||||
import { format_decipher, js_tokens } from './cipher';
|
||||
import { Video } from '../classes/Video';
|
||||
import { PlayList } from '../classes/Playlist';
|
||||
|
||||
const DEFAULT_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8";
|
||||
const video_pattern = /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/;
|
||||
const playlist_pattern = /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?(youtube\.com)\/(?:(playlist|watch))(.*)?((\?|\&)list=)/
|
||||
const DEFAULT_API_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8';
|
||||
const video_pattern =
|
||||
/^((?:https?:)?\/\/)?(?:(?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/;
|
||||
const playlist_pattern = /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?(youtube\.com)\/(?:(playlist|watch))(.*)?((\?|\&)list=)/;
|
||||
|
||||
export function yt_validate(url : string): "playlist" | "video" | boolean {
|
||||
if(url.indexOf('list=') === -1){
|
||||
if(!url.match(video_pattern)) return false
|
||||
else return "video"
|
||||
}
|
||||
else {
|
||||
if(!url.match(playlist_pattern)) return false
|
||||
let Playlist_id = url.split('list=')[1].split('&')[0]
|
||||
if(Playlist_id.length !== 34 || !Playlist_id.startsWith('PL')){
|
||||
return false
|
||||
export function yt_validate(url: string): 'playlist' | 'video' | boolean {
|
||||
if (url.indexOf('list=') === -1) {
|
||||
if (!url.match(video_pattern)) return false;
|
||||
else return 'video';
|
||||
} else {
|
||||
if (!url.match(playlist_pattern)) return false;
|
||||
const Playlist_id = url.split('list=')[1].split('&')[0];
|
||||
if (Playlist_id.length !== 34 || !Playlist_id.startsWith('PL')) {
|
||||
return false;
|
||||
}
|
||||
return "playlist"
|
||||
return 'playlist';
|
||||
}
|
||||
}
|
||||
|
||||
export function extractID(url : string): string{
|
||||
if(url.startsWith('https')){
|
||||
if(url.indexOf('list=') === -1){
|
||||
let video_id : string;
|
||||
if(url.includes('youtu.be/')) video_id = url.split('youtu.be/')[1].split('/')[0]
|
||||
else if(url.includes('youtube.com/embed/')) video_id = url.split('youtube.com/embed/')[1].split('/')[0]
|
||||
export function extractID(url: string): string {
|
||||
if (url.startsWith('https')) {
|
||||
if (url.indexOf('list=') === -1) {
|
||||
let video_id: string;
|
||||
if (url.includes('youtu.be/')) video_id = url.split('youtu.be/')[1].split('/')[0];
|
||||
else if (url.includes('youtube.com/embed/')) video_id = url.split('youtube.com/embed/')[1].split('/')[0];
|
||||
else video_id = url.split('watch?v=')[1].split('&')[0];
|
||||
return video_id
|
||||
return video_id;
|
||||
} else {
|
||||
return url.split('list=')[1].split('&')[0];
|
||||
}
|
||||
else{
|
||||
return url.split('list=')[1].split('&')[0]
|
||||
}
|
||||
}
|
||||
else return url
|
||||
} else return url;
|
||||
}
|
||||
|
||||
export async function video_basic_info(url : string, cookie? : string){
|
||||
let video_id : string;
|
||||
if(url.startsWith('https')) {
|
||||
if(yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL')
|
||||
video_id = extractID(url)
|
||||
}
|
||||
else video_id = url
|
||||
let new_url = `https://www.youtube.com/watch?v=${video_id}`
|
||||
let body = await request(new_url, {
|
||||
headers : (cookie) ? { 'cookie' : cookie, 'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7' } : {'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7'}
|
||||
})
|
||||
let player_response = JSON.parse(body.split("var ytInitialPlayerResponse = ")[1].split("}};")[0] + '}}')
|
||||
let initial_response = JSON.parse(body.split("var ytInitialData = ")[1].split("}};")[0] + '}}')
|
||||
let badge = initial_response.contents.twoColumnWatchNextResults.results.results.contents[1]?.videoSecondaryInfoRenderer?.owner?.videoOwnerRenderer?.badges && initial_response.contents.twoColumnWatchNextResults.results.results.contents[1]?.videoSecondaryInfoRenderer?.owner?.videoOwnerRenderer?.badges[0]
|
||||
if(player_response.playabilityStatus.status !== 'OK') throw new Error(`While getting info from url\n${player_response.playabilityStatus.errorScreen.playerErrorMessageRenderer?.reason.simpleText ?? player_response.playabilityStatus.errorScreen.playerKavRenderer?.reason.simpleText}`)
|
||||
let html5player = `https://www.youtube.com${body.split('"jsUrl":"')[1].split('"')[0]}`
|
||||
let format = []
|
||||
let vid = player_response.videoDetails
|
||||
let microformat = player_response.microformat.playerMicroformatRenderer
|
||||
let video_details = {
|
||||
id : vid.videoId,
|
||||
url : `https://www.youtube.com/watch?v=${vid.videoId}`,
|
||||
title : vid.title,
|
||||
description : vid.shortDescription,
|
||||
durationInSec : vid.lengthSeconds,
|
||||
durationRaw : parseSeconds(vid.lengthSeconds),
|
||||
uploadedDate : microformat.publishDate,
|
||||
thumbnail : vid.thumbnail.thumbnails[vid.thumbnail.thumbnails.length - 1],
|
||||
channel : {
|
||||
name : vid.author,
|
||||
id : vid.channelId,
|
||||
url : `https://www.youtube.com/channel/${vid.channelId}`,
|
||||
verified : Boolean(badge?.metadataBadgeRenderer?.style?.toLowerCase().includes('verified'))
|
||||
},
|
||||
views : vid.viewCount,
|
||||
tags : vid.keywords,
|
||||
averageRating : vid.averageRating,
|
||||
live : vid.isLiveContent,
|
||||
private : vid.isPrivate
|
||||
}
|
||||
if(!video_details.live) format.push(player_response.streamingData.formats[0])
|
||||
format.push(...player_response.streamingData.adaptiveFormats)
|
||||
let LiveStreamData = {
|
||||
isLive : video_details.live,
|
||||
dashManifestUrl : player_response.streamingData?.dashManifestUrl ?? null,
|
||||
hlsManifestUrl : player_response.streamingData?.hlsManifestUrl ?? null
|
||||
}
|
||||
return {
|
||||
LiveStreamData,
|
||||
html5player,
|
||||
format,
|
||||
video_details
|
||||
}
|
||||
export async function video_basic_info(url: string, cookie?: string) {
|
||||
let video_id: string;
|
||||
if (url.startsWith('https')) {
|
||||
if (yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL');
|
||||
video_id = extractID(url);
|
||||
} else video_id = url;
|
||||
const new_url = `https://www.youtube.com/watch?v=${video_id}`;
|
||||
const body = await request(new_url, {
|
||||
headers: cookie
|
||||
? {
|
||||
'cookie': cookie,
|
||||
'accept-language': 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7'
|
||||
}
|
||||
: { 'accept-language': 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7' }
|
||||
});
|
||||
const player_response = JSON.parse(body.split('var ytInitialPlayerResponse = ')[1].split('}};')[0] + '}}');
|
||||
const initial_response = JSON.parse(body.split('var ytInitialData = ')[1].split('}};')[0] + '}}');
|
||||
const badge =
|
||||
initial_response.contents.twoColumnWatchNextResults.results.results.contents[1]?.videoSecondaryInfoRenderer
|
||||
?.owner?.videoOwnerRenderer?.badges &&
|
||||
initial_response.contents.twoColumnWatchNextResults.results.results.contents[1]?.videoSecondaryInfoRenderer
|
||||
?.owner?.videoOwnerRenderer?.badges[0];
|
||||
if (player_response.playabilityStatus.status !== 'OK')
|
||||
throw new Error(
|
||||
`While getting info from url\n${
|
||||
player_response.playabilityStatus.errorScreen.playerErrorMessageRenderer?.reason.simpleText ??
|
||||
player_response.playabilityStatus.errorScreen.playerKavRenderer?.reason.simpleText
|
||||
}`
|
||||
);
|
||||
const html5player = `https://www.youtube.com${body.split('"jsUrl":"')[1].split('"')[0]}`;
|
||||
const format = [];
|
||||
const vid = player_response.videoDetails;
|
||||
const microformat = player_response.microformat.playerMicroformatRenderer;
|
||||
const video_details = {
|
||||
id: vid.videoId,
|
||||
url: `https://www.youtube.com/watch?v=${vid.videoId}`,
|
||||
title: vid.title,
|
||||
description: vid.shortDescription,
|
||||
durationInSec: vid.lengthSeconds,
|
||||
durationRaw: parseSeconds(vid.lengthSeconds),
|
||||
uploadedDate: microformat.publishDate,
|
||||
thumbnail: vid.thumbnail.thumbnails[vid.thumbnail.thumbnails.length - 1],
|
||||
channel: {
|
||||
name: vid.author,
|
||||
id: vid.channelId,
|
||||
url: `https://www.youtube.com/channel/${vid.channelId}`,
|
||||
verified: Boolean(badge?.metadataBadgeRenderer?.style?.toLowerCase().includes('verified'))
|
||||
},
|
||||
views: vid.viewCount,
|
||||
tags: vid.keywords,
|
||||
averageRating: vid.averageRating,
|
||||
live: vid.isLiveContent,
|
||||
private: vid.isPrivate
|
||||
};
|
||||
if (!video_details.live) format.push(player_response.streamingData.formats[0]);
|
||||
format.push(...player_response.streamingData.adaptiveFormats);
|
||||
const LiveStreamData = {
|
||||
isLive: video_details.live,
|
||||
dashManifestUrl: player_response.streamingData?.dashManifestUrl ?? null,
|
||||
hlsManifestUrl: player_response.streamingData?.hlsManifestUrl ?? null
|
||||
};
|
||||
return {
|
||||
LiveStreamData,
|
||||
html5player,
|
||||
format,
|
||||
video_details
|
||||
};
|
||||
}
|
||||
|
||||
function parseSeconds(seconds : number): string {
|
||||
let d = Number(seconds);
|
||||
var h = Math.floor(d / 3600);
|
||||
var m = Math.floor(d % 3600 / 60);
|
||||
var s = Math.floor(d % 3600 % 60);
|
||||
function parseSeconds(seconds: number): string {
|
||||
const d = Number(seconds);
|
||||
const h = Math.floor(d / 3600);
|
||||
const m = Math.floor((d % 3600) / 60);
|
||||
const s = Math.floor((d % 3600) % 60);
|
||||
|
||||
var hDisplay = h > 0 ? (h < 10 ? `0${h}` : h) + ':' : "";
|
||||
var mDisplay = m > 0 ? (m < 10 ? `0${m}` : m) + ':' : "00:";
|
||||
var sDisplay = s > 0 ? (s < 10 ? `0${s}` : s) : "00";
|
||||
return hDisplay + mDisplay + sDisplay;
|
||||
const hDisplay = h > 0 ? (h < 10 ? `0${h}` : h) + ':' : '';
|
||||
const mDisplay = m > 0 ? (m < 10 ? `0${m}` : m) + ':' : '00:';
|
||||
const sDisplay = s > 0 ? (s < 10 ? `0${s}` : s) : '00';
|
||||
return hDisplay + mDisplay + sDisplay;
|
||||
}
|
||||
|
||||
export async function video_info(url : string, cookie? : string) {
|
||||
let data = await video_basic_info(url, cookie)
|
||||
if(data.LiveStreamData.isLive === true && data.LiveStreamData.hlsManifestUrl !== null){
|
||||
return data
|
||||
}
|
||||
else if(data.format[0].signatureCipher || data.format[0].cipher){
|
||||
data.format = await format_decipher(data.format, data.html5player)
|
||||
return data
|
||||
}
|
||||
else {
|
||||
return data
|
||||
export async function video_info(url: string, cookie?: string) {
|
||||
const data = await video_basic_info(url, cookie);
|
||||
if (data.LiveStreamData.isLive === true && data.LiveStreamData.hlsManifestUrl !== null) {
|
||||
return data;
|
||||
} else if (data.format[0].signatureCipher || data.format[0].cipher) {
|
||||
data.format = await format_decipher(data.format, data.html5player);
|
||||
return data;
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
export async function playlist_info(url : string, parseIncomplete : boolean = false) {
|
||||
if (!url || typeof url !== "string") throw new Error(`Expected playlist url, received ${typeof url}!`);
|
||||
let Playlist_id : string
|
||||
if(url.startsWith('https')){
|
||||
if(yt_validate(url) !== 'playlist') throw new Error('This is not a Playlist URL')
|
||||
Playlist_id = extractID(url)
|
||||
}
|
||||
else Playlist_id = url
|
||||
let new_url = `https://www.youtube.com/playlist?list=${Playlist_id}`
|
||||
|
||||
let body = await request(new_url, {
|
||||
headers : {'accept-language' : 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7'}
|
||||
})
|
||||
let response = JSON.parse(body.split("var ytInitialData = ")[1].split(";</script>")[0])
|
||||
if(response.alerts){
|
||||
if(response.alerts[0].alertWithButtonRenderer?.type === 'INFO') {
|
||||
if(!parseIncomplete) throw new Error(`While parsing playlist url\n${response.alerts[0].alertWithButtonRenderer.text.simpleText}`)
|
||||
}
|
||||
else if(response.alerts[0].alertRenderer?.type === 'ERROR') throw new Error(`While parsing playlist url\n${response.alerts[0].alertRenderer.text.runs[0].text}`)
|
||||
else throw new Error('While parsing playlist url\nUnknown Playlist Error')
|
||||
export async function playlist_info(url: string, parseIncomplete = false) {
|
||||
if (!url || typeof url !== 'string') throw new Error(`Expected playlist url, received ${typeof url}!`);
|
||||
let Playlist_id: string;
|
||||
if (url.startsWith('https')) {
|
||||
if (yt_validate(url) !== 'playlist') throw new Error('This is not a Playlist URL');
|
||||
Playlist_id = extractID(url);
|
||||
} else Playlist_id = url;
|
||||
const new_url = `https://www.youtube.com/playlist?list=${Playlist_id}`;
|
||||
|
||||
const body = await request(new_url, {
|
||||
headers: { 'accept-language': 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7' }
|
||||
});
|
||||
const response = JSON.parse(body.split('var ytInitialData = ')[1].split(';</script>')[0]);
|
||||
if (response.alerts) {
|
||||
if (response.alerts[0].alertWithButtonRenderer?.type === 'INFO') {
|
||||
if (!parseIncomplete)
|
||||
throw new Error(
|
||||
`While parsing playlist url\n${response.alerts[0].alertWithButtonRenderer.text.simpleText}`
|
||||
);
|
||||
} else if (response.alerts[0].alertRenderer?.type === 'ERROR')
|
||||
throw new Error(`While parsing playlist url\n${response.alerts[0].alertRenderer.text.runs[0].text}`);
|
||||
else throw new Error('While parsing playlist url\nUnknown Playlist Error');
|
||||
}
|
||||
|
||||
let rawJSON = `${body.split('{"playlistVideoListRenderer":{"contents":')[1].split('}],"playlistId"')[0]}}]`;
|
||||
let parsed = JSON.parse(rawJSON);
|
||||
let playlistDetails = JSON.parse(body.split('{"playlistSidebarRenderer":')[1].split("}};</script>")[0]).items;
|
||||
const rawJSON = `${body.split('{"playlistVideoListRenderer":{"contents":')[1].split('}],"playlistId"')[0]}}]`;
|
||||
const parsed = JSON.parse(rawJSON);
|
||||
const playlistDetails = JSON.parse(body.split('{"playlistSidebarRenderer":')[1].split('}};</script>')[0]).items;
|
||||
|
||||
let API_KEY = body.split('INNERTUBE_API_KEY":"')[1]?.split('"')[0] ?? body.split('innertubeApiKey":"')[1]?.split('"')[0] ?? DEFAULT_API_KEY;
|
||||
let videos = getPlaylistVideos(parsed, 100);
|
||||
const API_KEY =
|
||||
body.split('INNERTUBE_API_KEY":"')[1]?.split('"')[0] ??
|
||||
body.split('innertubeApiKey":"')[1]?.split('"')[0] ??
|
||||
DEFAULT_API_KEY;
|
||||
const videos = getPlaylistVideos(parsed, 100);
|
||||
|
||||
let data = playlistDetails[0].playlistSidebarPrimaryInfoRenderer;
|
||||
const data = playlistDetails[0].playlistSidebarPrimaryInfoRenderer;
|
||||
if (!data.title.runs || !data.title.runs.length) return undefined;
|
||||
|
||||
let author = playlistDetails[1]?.playlistSidebarSecondaryInfoRenderer.videoOwner;
|
||||
let views = data.stats.length === 3 ? data.stats[1].simpleText.replace(/[^0-9]/g, "") : 0;
|
||||
let lastUpdate = data.stats.find((x: any) => "runs" in x && x["runs"].find((y: any) => y.text.toLowerCase().includes("last update")))?.runs.pop()?.text ?? null;
|
||||
let videosCount = data.stats[0].runs[0].text.replace(/[^0-9]/g, "") || 0;
|
||||
const author = playlistDetails[1]?.playlistSidebarSecondaryInfoRenderer.videoOwner;
|
||||
const views = data.stats.length === 3 ? data.stats[1].simpleText.replace(/[^0-9]/g, '') : 0;
|
||||
const lastUpdate =
|
||||
data.stats
|
||||
.find((x: any) => 'runs' in x && x['runs'].find((y: any) => y.text.toLowerCase().includes('last update')))
|
||||
?.runs.pop()?.text ?? null;
|
||||
const videosCount = data.stats[0].runs[0].text.replace(/[^0-9]/g, '') || 0;
|
||||
|
||||
let res = new PlayList({
|
||||
const res = new PlayList({
|
||||
continuation: {
|
||||
api: API_KEY,
|
||||
token: getContinuationToken(parsed),
|
||||
clientVersion: body.split('"INNERTUBE_CONTEXT_CLIENT_VERSION":"')[1]?.split('"')[0] ?? body.split('"innertube_context_client_version":"')[1]?.split('"')[0] ?? "<some version>"
|
||||
clientVersion:
|
||||
body.split('"INNERTUBE_CONTEXT_CLIENT_VERSION":"')[1]?.split('"')[0] ??
|
||||
body.split('"innertube_context_client_version":"')[1]?.split('"')[0] ??
|
||||
'<some version>'
|
||||
},
|
||||
id: data.title.runs[0].navigationEndpoint.watchEndpoint.playlistId,
|
||||
title: data.title.runs[0].text,
|
||||
@@ -174,16 +195,27 @@ export async function playlist_info(url : string, parseIncomplete : boolean = fa
|
||||
? {
|
||||
name: author.videoOwnerRenderer.title.runs[0].text,
|
||||
id: author.videoOwnerRenderer.title.runs[0].navigationEndpoint.browseEndpoint.browseId,
|
||||
url: `https://www.youtube.com${author.videoOwnerRenderer.navigationEndpoint.commandMetadata.webCommandMetadata.url || author.videoOwnerRenderer.navigationEndpoint.browseEndpoint.canonicalBaseUrl}`,
|
||||
icon: author.videoOwnerRenderer.thumbnail.thumbnails.length ? author.videoOwnerRenderer.thumbnail.thumbnails[author.videoOwnerRenderer.thumbnail.thumbnails.length - 1].url : null
|
||||
url: `https://www.youtube.com${
|
||||
author.videoOwnerRenderer.navigationEndpoint.commandMetadata.webCommandMetadata.url ||
|
||||
author.videoOwnerRenderer.navigationEndpoint.browseEndpoint.canonicalBaseUrl
|
||||
}`,
|
||||
icon: author.videoOwnerRenderer.thumbnail.thumbnails.length
|
||||
? author.videoOwnerRenderer.thumbnail.thumbnails[
|
||||
author.videoOwnerRenderer.thumbnail.thumbnails.length - 1
|
||||
].url
|
||||
: null
|
||||
}
|
||||
: {},
|
||||
thumbnail: data.thumbnailRenderer.playlistVideoThumbnailRenderer?.thumbnail.thumbnails.length ? data.thumbnailRenderer.playlistVideoThumbnailRenderer.thumbnail.thumbnails[data.thumbnailRenderer.playlistVideoThumbnailRenderer.thumbnail.thumbnails.length - 1].url : null
|
||||
thumbnail: data.thumbnailRenderer.playlistVideoThumbnailRenderer?.thumbnail.thumbnails.length
|
||||
? data.thumbnailRenderer.playlistVideoThumbnailRenderer.thumbnail.thumbnails[
|
||||
data.thumbnailRenderer.playlistVideoThumbnailRenderer.thumbnail.thumbnails.length - 1
|
||||
].url
|
||||
: null
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
export function getPlaylistVideos(data:any, limit : number = Infinity) : Video[] {
|
||||
export function getPlaylistVideos(data: any, limit = Infinity): Video[] {
|
||||
const videos = [];
|
||||
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
@@ -196,7 +228,7 @@ export function getPlaylistVideos(data:any, limit : number = Infinity) : Video[]
|
||||
id: info.videoId,
|
||||
index: parseInt(info.index?.simpleText) || 0,
|
||||
duration: parseDuration(info.lengthText?.simpleText) || 0,
|
||||
duration_raw: info.lengthText?.simpleText ?? "0:00",
|
||||
duration_raw: info.lengthText?.simpleText ?? '0:00',
|
||||
thumbnail: {
|
||||
id: info.videoId,
|
||||
url: info.thumbnail.thumbnails[info.thumbnail.thumbnails.length - 1].url,
|
||||
@@ -207,18 +239,21 @@ export function getPlaylistVideos(data:any, limit : number = Infinity) : Video[]
|
||||
channel: {
|
||||
id: info.shortBylineText.runs[0].navigationEndpoint.browseEndpoint.browseId || undefined,
|
||||
name: info.shortBylineText.runs[0].text || undefined,
|
||||
url: `https://www.youtube.com${info.shortBylineText.runs[0].navigationEndpoint.browseEndpoint.canonicalBaseUrl || info.shortBylineText.runs[0].navigationEndpoint.commandMetadata.webCommandMetadata.url}`,
|
||||
url: `https://www.youtube.com${
|
||||
info.shortBylineText.runs[0].navigationEndpoint.browseEndpoint.canonicalBaseUrl ||
|
||||
info.shortBylineText.runs[0].navigationEndpoint.commandMetadata.webCommandMetadata.url
|
||||
}`,
|
||||
icon: undefined
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
return videos
|
||||
return videos;
|
||||
}
|
||||
|
||||
function parseDuration(duration: string): number {
|
||||
duration ??= "0:00";
|
||||
const args = duration.split(":");
|
||||
duration ??= '0:00';
|
||||
const args = duration.split(':');
|
||||
let dur = 0;
|
||||
|
||||
switch (args.length) {
|
||||
@@ -235,8 +270,8 @@ function parseDuration(duration: string): number {
|
||||
return dur;
|
||||
}
|
||||
|
||||
|
||||
export function getContinuationToken(data:any): string {
|
||||
const continuationToken = data.find((x: any) => Object.keys(x)[0] === "continuationItemRenderer")?.continuationItemRenderer.continuationEndpoint?.continuationCommand?.token;
|
||||
export function getContinuationToken(data: any): string {
|
||||
const continuationToken = data.find((x: any) => Object.keys(x)[0] === 'continuationItemRenderer')
|
||||
?.continuationItemRenderer.continuationEndpoint?.continuationCommand?.token;
|
||||
return continuationToken;
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
export { video_basic_info, video_info, playlist_info, yt_validate, extractID } from './extractor'
|
||||
export { video_basic_info, video_info, playlist_info, yt_validate, extractID } from './extractor';
|
||||
|
||||
@@ -1,50 +1,51 @@
|
||||
import { Video } from "../classes/Video";
|
||||
import { PlayList } from "../classes/Playlist";
|
||||
import { Channel } from "../classes/Channel";
|
||||
import { Video } from '../classes/Video';
|
||||
import { PlayList } from '../classes/Playlist';
|
||||
import { Channel } from '../classes/Channel';
|
||||
|
||||
export interface ParseSearchInterface {
|
||||
type?: "video" | "playlist" | "channel" ;
|
||||
type?: 'video' | 'playlist' | 'channel';
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export interface thumbnail{
|
||||
export interface thumbnail {
|
||||
width: string;
|
||||
height : string;
|
||||
url : string
|
||||
height: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export function ParseSearchResult(html : string, options? : ParseSearchInterface): (Video | PlayList | Channel)[] {
|
||||
if(!html) throw new Error('Can\'t parse Search result without data')
|
||||
if (!options) options = { type: "video", limit: 0 };
|
||||
if (!options.type) options.type = "video";
|
||||
export function ParseSearchResult(html: string, options?: ParseSearchInterface): (Video | PlayList | Channel)[] {
|
||||
if (!html) throw new Error("Can't parse Search result without data");
|
||||
if (!options) options = { type: 'video', limit: 0 };
|
||||
if (!options.type) options.type = 'video';
|
||||
|
||||
let data = html.split("var ytInitialData = ")[1].split("}};")[0] + '}}';
|
||||
let json_data = JSON.parse(data)
|
||||
let results = []
|
||||
let details = json_data.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[0].itemSectionRenderer.contents
|
||||
for(let i = 0; i < details.length; i++){
|
||||
if (typeof options.limit === "number" && options.limit > 0 && results.length >= options.limit) break;
|
||||
if (options.type === "video") {
|
||||
const data = html.split('var ytInitialData = ')[1].split('}};')[0] + '}}';
|
||||
const json_data = JSON.parse(data);
|
||||
const results = [];
|
||||
const details =
|
||||
json_data.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[0]
|
||||
.itemSectionRenderer.contents;
|
||||
for (let i = 0; i < details.length; i++) {
|
||||
if (typeof options.limit === 'number' && options.limit > 0 && results.length >= options.limit) break;
|
||||
if (options.type === 'video') {
|
||||
const parsed = parseVideo(details[i]);
|
||||
if (!parsed) continue;
|
||||
results.push(parsed)
|
||||
} else if (options.type === "channel") {
|
||||
results.push(parsed);
|
||||
} else if (options.type === 'channel') {
|
||||
const parsed = parseChannel(details[i]);
|
||||
if (!parsed) continue;
|
||||
results.push(parsed)
|
||||
} else if (options.type === "playlist") {
|
||||
results.push(parsed);
|
||||
} else if (options.type === 'playlist') {
|
||||
const parsed = parsePlaylist(details[i]);
|
||||
if (!parsed) continue;
|
||||
results.push(parsed)
|
||||
results.push(parsed);
|
||||
}
|
||||
}
|
||||
return results
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
function parseDuration(duration: string): number {
|
||||
duration ??= "0:00";
|
||||
const args = duration.split(":");
|
||||
duration ??= '0:00';
|
||||
const args = duration.split(':');
|
||||
let dur = 0;
|
||||
|
||||
switch (args.length) {
|
||||
@@ -64,18 +65,27 @@ function parseDuration(duration: string): number {
|
||||
export function parseChannel(data?: any): Channel | void {
|
||||
if (!data || !data.channelRenderer) return;
|
||||
const badge = data.channelRenderer.ownerBadges && data.channelRenderer.ownerBadges[0];
|
||||
let url = `https://www.youtube.com${data.channelRenderer.navigationEndpoint.browseEndpoint.canonicalBaseUrl || data.channelRenderer.navigationEndpoint.commandMetadata.webCommandMetadata.url}`;
|
||||
let res = new Channel({
|
||||
const url = `https://www.youtube.com${
|
||||
data.channelRenderer.navigationEndpoint.browseEndpoint.canonicalBaseUrl ||
|
||||
data.channelRenderer.navigationEndpoint.commandMetadata.webCommandMetadata.url
|
||||
}`;
|
||||
const res = new Channel({
|
||||
id: data.channelRenderer.channelId,
|
||||
name: data.channelRenderer.title.simpleText,
|
||||
icon: {
|
||||
url : data.channelRenderer.thumbnail.thumbnails[data.channelRenderer.thumbnail.thumbnails.length - 1].url.replace('//', 'https://'),
|
||||
width : data.channelRenderer.thumbnail.thumbnails[data.channelRenderer.thumbnail.thumbnails.length - 1].width,
|
||||
height: data.channelRenderer.thumbnail.thumbnails[data.channelRenderer.thumbnail.thumbnails.length - 1].height
|
||||
url: data.channelRenderer.thumbnail.thumbnails[
|
||||
data.channelRenderer.thumbnail.thumbnails.length - 1
|
||||
].url.replace('//', 'https://'),
|
||||
width: data.channelRenderer.thumbnail.thumbnails[data.channelRenderer.thumbnail.thumbnails.length - 1]
|
||||
.width,
|
||||
height: data.channelRenderer.thumbnail.thumbnails[data.channelRenderer.thumbnail.thumbnails.length - 1]
|
||||
.height
|
||||
},
|
||||
url: url,
|
||||
verified: Boolean(badge?.metadataBadgeRenderer?.style?.toLowerCase().includes("verified")),
|
||||
subscribers: (data.channelRenderer.subscriberCountText?.simpleText) ? data.channelRenderer.subscriberCountText.simpleText : '0 subscribers'
|
||||
verified: Boolean(badge?.metadataBadgeRenderer?.style?.toLowerCase().includes('verified')),
|
||||
subscribers: data.channelRenderer.subscriberCountText?.simpleText
|
||||
? data.channelRenderer.subscriberCountText.simpleText
|
||||
: '0 subscribers'
|
||||
});
|
||||
|
||||
return res;
|
||||
@@ -85,34 +95,42 @@ export function parseVideo(data?: any): Video | void {
|
||||
if (!data || !data.videoRenderer) return;
|
||||
|
||||
const badge = data.videoRenderer.ownerBadges && data.videoRenderer.ownerBadges[0];
|
||||
let res = new Video({
|
||||
const res = new Video({
|
||||
id: data.videoRenderer.videoId,
|
||||
url: `https://www.youtube.com/watch?v=${data.videoRenderer.videoId}`,
|
||||
title: data.videoRenderer.title.runs[0].text,
|
||||
description: data.videoRenderer.descriptionSnippet && data.videoRenderer.descriptionSnippet.runs[0] ? data.videoRenderer.descriptionSnippet.runs[0].text : "",
|
||||
description:
|
||||
data.videoRenderer.descriptionSnippet && data.videoRenderer.descriptionSnippet.runs[0]
|
||||
? data.videoRenderer.descriptionSnippet.runs[0].text
|
||||
: '',
|
||||
duration: data.videoRenderer.lengthText ? parseDuration(data.videoRenderer.lengthText.simpleText) : 0,
|
||||
duration_raw: data.videoRenderer.lengthText ? data.videoRenderer.lengthText.simpleText : null,
|
||||
thumbnail: data.videoRenderer.thumbnail.thumbnails[data.videoRenderer.thumbnail.thumbnails.length - 1],
|
||||
channel: {
|
||||
id: data.videoRenderer.ownerText.runs[0].navigationEndpoint.browseEndpoint.browseId || null,
|
||||
name: data.videoRenderer.ownerText.runs[0].text || null,
|
||||
url: `https://www.youtube.com${data.videoRenderer.ownerText.runs[0].navigationEndpoint.browseEndpoint.canonicalBaseUrl || data.videoRenderer.ownerText.runs[0].navigationEndpoint.commandMetadata.webCommandMetadata.url}`,
|
||||
url: `https://www.youtube.com${
|
||||
data.videoRenderer.ownerText.runs[0].navigationEndpoint.browseEndpoint.canonicalBaseUrl ||
|
||||
data.videoRenderer.ownerText.runs[0].navigationEndpoint.commandMetadata.webCommandMetadata.url
|
||||
}`,
|
||||
icon: {
|
||||
url: data.videoRenderer.channelThumbnailSupportedRenderers.channelThumbnailWithLinkRenderer.thumbnail.thumbnails[0].url,
|
||||
width: data.videoRenderer.channelThumbnailSupportedRenderers.channelThumbnailWithLinkRenderer.thumbnail.thumbnails[0].width,
|
||||
height: data.videoRenderer.channelThumbnailSupportedRenderers.channelThumbnailWithLinkRenderer.thumbnail.thumbnails[0].height
|
||||
url: data.videoRenderer.channelThumbnailSupportedRenderers.channelThumbnailWithLinkRenderer.thumbnail
|
||||
.thumbnails[0].url,
|
||||
width: data.videoRenderer.channelThumbnailSupportedRenderers.channelThumbnailWithLinkRenderer.thumbnail
|
||||
.thumbnails[0].width,
|
||||
height: data.videoRenderer.channelThumbnailSupportedRenderers.channelThumbnailWithLinkRenderer.thumbnail
|
||||
.thumbnails[0].height
|
||||
},
|
||||
verified: Boolean(badge?.metadataBadgeRenderer?.style?.toLowerCase().includes("verified"))
|
||||
verified: Boolean(badge?.metadataBadgeRenderer?.style?.toLowerCase().includes('verified'))
|
||||
},
|
||||
uploadedAt: data.videoRenderer.publishedTimeText?.simpleText ?? null,
|
||||
views: data.videoRenderer.viewCountText?.simpleText?.replace(/[^0-9]/g, "") ?? 0,
|
||||
live : data.videoRenderer.lengthText ? false : true,
|
||||
views: data.videoRenderer.viewCountText?.simpleText?.replace(/[^0-9]/g, '') ?? 0,
|
||||
live: data.videoRenderer.lengthText ? false : true
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
export function parsePlaylist(data?: any): PlayList | void {
|
||||
if (!data.playlistRenderer) return;
|
||||
|
||||
@@ -122,19 +140,25 @@ export function parsePlaylist(data?: any): PlayList | void {
|
||||
title: data.playlistRenderer.title.simpleText,
|
||||
thumbnail: {
|
||||
id: data.playlistRenderer.playlistId,
|
||||
url: data.playlistRenderer.thumbnails[0].thumbnails[data.playlistRenderer.thumbnails[0].thumbnails.length - 1].url,
|
||||
height: data.playlistRenderer.thumbnails[0].thumbnails[data.playlistRenderer.thumbnails[0].thumbnails.length - 1].height,
|
||||
width: data.playlistRenderer.thumbnails[0].thumbnails[data.playlistRenderer.thumbnails[0].thumbnails.length - 1].width
|
||||
url: data.playlistRenderer.thumbnails[0].thumbnails[
|
||||
data.playlistRenderer.thumbnails[0].thumbnails.length - 1
|
||||
].url,
|
||||
height: data.playlistRenderer.thumbnails[0].thumbnails[
|
||||
data.playlistRenderer.thumbnails[0].thumbnails.length - 1
|
||||
].height,
|
||||
width: data.playlistRenderer.thumbnails[0].thumbnails[
|
||||
data.playlistRenderer.thumbnails[0].thumbnails.length - 1
|
||||
].width
|
||||
},
|
||||
channel: {
|
||||
id: data.playlistRenderer.shortBylineText.runs[0].navigationEndpoint.browseEndpoint.browseId,
|
||||
name: data.playlistRenderer.shortBylineText.runs[0].text,
|
||||
url: `https://www.youtube.com${data.playlistRenderer.shortBylineText.runs[0].navigationEndpoint.commandMetadata.webCommandMetadata.url}`
|
||||
},
|
||||
videos: parseInt(data.playlistRenderer.videoCount.replace(/[^0-9]/g, ""))
|
||||
videos: parseInt(data.playlistRenderer.videoCount.replace(/[^0-9]/g, ''))
|
||||
},
|
||||
true
|
||||
);
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +1,61 @@
|
||||
import https, { RequestOptions } from 'https'
|
||||
import { IncomingMessage } from 'http'
|
||||
import { URL } from 'url'
|
||||
import https, { RequestOptions } from 'https';
|
||||
import { IncomingMessage } from 'http';
|
||||
import { URL } from 'url';
|
||||
|
||||
interface RequestOpts extends RequestOptions{
|
||||
body? : string;
|
||||
method? : "GET" | "POST"
|
||||
interface RequestOpts extends RequestOptions {
|
||||
body?: string;
|
||||
method?: 'GET' | 'POST';
|
||||
}
|
||||
|
||||
function https_getter(req_url : string, options : RequestOpts = {}): Promise<IncomingMessage>{
|
||||
function https_getter(req_url: string, options: RequestOpts = {}): Promise<IncomingMessage> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let s = new URL(req_url)
|
||||
options.method ??= "GET"
|
||||
let req_options : RequestOptions = {
|
||||
host : s.hostname,
|
||||
path : s.pathname + s.search,
|
||||
headers : options.headers ?? {},
|
||||
method : options.method
|
||||
}
|
||||
const s = new URL(req_url);
|
||||
options.method ??= 'GET';
|
||||
const req_options: RequestOptions = {
|
||||
host: s.hostname,
|
||||
path: s.pathname + s.search,
|
||||
headers: options.headers ?? {},
|
||||
method: options.method
|
||||
};
|
||||
|
||||
let req = https.request(req_options, resolve)
|
||||
const req = https.request(req_options, resolve);
|
||||
req.on('error', (err) => {
|
||||
reject(err)
|
||||
})
|
||||
if(options.method === "POST") req.write(options.body)
|
||||
req.end()
|
||||
})
|
||||
reject(err);
|
||||
});
|
||||
if (options.method === 'POST') req.write(options.body);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
export async function request(url : string, options? : RequestOpts): Promise<string>{
|
||||
export async function request(url: string, options?: RequestOpts): Promise<string> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let data = ''
|
||||
let res = await https_getter(url, options).catch((err: Error) => err)
|
||||
if(res instanceof Error) {reject(res); return}
|
||||
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
|
||||
res = await https_getter(res.headers.location as string , options)
|
||||
let data = '';
|
||||
let res = await https_getter(url, options).catch((err: Error) => err);
|
||||
if (res instanceof Error) {
|
||||
reject(res);
|
||||
return;
|
||||
}
|
||||
else if(Number(res.statusCode) > 400){
|
||||
reject(new Error(`Got ${res.statusCode} from the request`))
|
||||
if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) {
|
||||
res = await https_getter(res.headers.location as string, options);
|
||||
} else if (Number(res.statusCode) > 400) {
|
||||
reject(new Error(`Got ${res.statusCode} from the request`));
|
||||
}
|
||||
res.setEncoding('utf-8')
|
||||
res.on('data', (c) => data+=c)
|
||||
res.on('end', () => resolve(data))
|
||||
})
|
||||
res.setEncoding('utf-8');
|
||||
res.on('data', (c) => (data += c));
|
||||
res.on('end', () => resolve(data));
|
||||
});
|
||||
}
|
||||
|
||||
export async function request_stream(url : string, options? : RequestOpts): Promise<IncomingMessage>{
|
||||
export async function request_stream(url: string, options?: RequestOpts): Promise<IncomingMessage> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let res = await https_getter(url, options).catch((err: Error) => err)
|
||||
if(res instanceof Error) {reject(res); return}
|
||||
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
|
||||
res = await https_getter(res.headers.location as string, options)
|
||||
let res = await https_getter(url, options).catch((err: Error) => err);
|
||||
if (res instanceof Error) {
|
||||
reject(res);
|
||||
return;
|
||||
}
|
||||
resolve(res)
|
||||
})
|
||||
if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) {
|
||||
res = await https_getter(res.headers.location as string, options);
|
||||
}
|
||||
resolve(res);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user