Huge Update

This commit is contained in:
killer069
2021-08-16 15:19:31 +05:30
parent 1b44ba8001
commit 97b1eed267
19 changed files with 321 additions and 28 deletions

View File

@@ -0,0 +1,63 @@
export interface ChannelIconInterface {
url?: string;
width: number;
height: number;
}
export class Channel {
name?: string;
verified?: boolean;
id?: string;
url?: string;
icon?: ChannelIconInterface;
subscribers?: string;
constructor(data: any) {
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
this._patch(data);
}
private _patch(data: any): void {
if (!data) data = {};
this.name = data.name || null;
this.verified = !!data.verified || false;
this.id = data.id || null;
this.url = data.url || null;
this.icon = data.icon || { url: null, width: 0, height: 0 };
this.subscribers = data.subscribers || null;
}
/**
* Returns channel icon url
* @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");
if (!this.icon?.url) return undefined;
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";
}
toString(): string {
return this.name || "";
}
toJSON() {
return {
name: this.name,
verified: this.verified,
id: this.id,
url: this.url,
iconURL: this.iconURL(),
type: this.type,
subscribers: this.subscribers
};
}
}

View File

@@ -0,0 +1,140 @@
import { getPlaylistVideos, getContinuationToken } from "../utils/extractor";
import { url_get } 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{
id?: string;
title?: string;
videoCount?: number;
lastUpdate?: string;
views?: number;
url?: string;
link?: string;
channel?: Channel;
thumbnail?: Thumbnail;
private videos?: [];
private fetched_videos : Map<string, Video[]>
private _continuation: { api?: string; token?: string; clientVersion?: string } = {};
private __count : number
constructor(data : any, searchResult : Boolean = 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)
}
private __patch(data:any){
this.id = data.id || undefined;
this.url = data.url || undefined;
this.title = data.title || undefined;
this.videoCount = data.videoCount || 0;
this.lastUpdate = data.lastUpdate || undefined;
this.views = data.views || 0;
this.link = data.link || undefined;
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._continuation.api = data.continuation?.api ?? undefined;
this._continuation.token = data.continuation?.token ?? undefined;
this._continuation.clientVersion = data.continuation?.clientVersion ?? "<important data>";
}
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;
this.thumbnail = data.thumbnail || undefined;
this.channel = data.channel || undefined;
this.videos = [];
this.videoCount = data.videos || 0;
this.link = undefined;
this.lastUpdate = undefined;
this.views = 0;
}
async next(limit: number = Infinity): Promise<Video[]> {
if (!this._continuation || !this._continuation.token) return [];
let nextPage = await url_get(`${BASE_API}${this._continuation.api}`, {
method: "POST",
body: JSON.stringify({
continuation: this._continuation.token,
context: {
client: {
utcOffsetMinutes: 0,
gl: "US",
hl: "en",
clientName: "WEB",
clientVersion: this._continuation.clientVersion
},
user: {},
request: {}
}
})
});
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
}
async fetch(max: number = Infinity) {
let 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++
const res = await this.next();
if (!res.length) break;
}
return this;
}
get type(): "playlist" {
return "playlist";
}
page(number : number): Video[]{
if(!number) throw new Error('Given 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_videos(){
let page_number: number = this.total_pages
return (page_number - 1) * 100 + (this.fetched_videos.get(`page${page_number}`) as Video[]).length
}
toJSON() {
return {
id: this.id,
title: this.title,
thumbnail: this.thumbnail,
channel: {
name : this.channel?.name,
id : this.channel?.id,
icon : this.channel?.iconURL()
},
url: this.url,
videos: this.videos
};
}
}

View File

@@ -0,0 +1,48 @@
type ThumbnailType = "default" | "hqdefault" | "mqdefault" | "sddefault" | "maxresdefault" | "ultrares";
export class Thumbnail {
id?: string;
width?: number;
height?: number;
url?: string;
constructor(data: any) {
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
this._patch(data);
}
private _patch(data: any) {
if (!data) data = {};
this.id = data.id || undefined;
this.width = data.width || 0;
this.height = data.height || 0;
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;
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}"!`);
return `https://i3.ytimg.com/vi/${this.id}/${id}.jpg`;
}
toString(): string {
return this.url ? `${this.url}` : "";
}
toJSON() {
return {
id: this.id,
width: this.width,
height: this.height,
url: this.url
};
}
}

View File

@@ -0,0 +1,107 @@
import { Channel } from "./Channel";
import { Thumbnail } from "./Thumbnail";
interface VideoOptions {
id?: string;
url? : string;
title?: string;
description?: string;
duration_formatted: string;
duration: number;
uploadedAt?: string;
views: number;
thumbnail?: {
id: string | undefined;
width: number | undefined ;
height: number | undefined;
url: string | undefined;
};
channel?: {
name : string,
id : string,
icon : string
};
videos?: Video[];
type : string;
ratings : {
likes: number;
dislikes: number;
}
live: boolean;
private: boolean;
tags: string[];
}
export class Video {
id?: string;
url? : string;
title?: string;
description?: string;
durationFormatted: string;
duration: number;
uploadedAt?: string;
views: number;
thumbnail?: Thumbnail;
channel?: Channel;
videos?: Video[];
likes: number;
dislikes: number;
live: boolean;
private: boolean;
tags: string[];
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.title = data.title || undefined;
this.description = data.description || undefined;
this.durationFormatted = data.duration_raw || "0:00";
this.duration = (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.dislikes = data.ratings?.dislikes || 0;
this.live = !!data.live;
this.private = !!data.private;
this.tags = data.tags || [];
}
get type(): "video" {
return "video";
}
get toString(): string {
return this.url || "";
}
get toJSON(): VideoOptions{
return {
id: this.id,
url: this.url,
title: this.title,
description: this.description,
duration: this.duration,
duration_formatted: this.durationFormatted,
uploadedAt: this.uploadedAt,
thumbnail: this.thumbnail?.toJSON(),
channel: {
name: this.channel?.name as string,
id: this.channel?.id as string,
icon: this.channel?.iconURL() as string
},
views: this.views,
type: this.type,
tags: this.tags,
ratings: {
likes: this.likes,
dislikes: this.dislikes
},
live: this.live,
private: this.private
};
}
}