This commit is contained in:
killer069
2021-08-12 15:58:55 +05:30
parent 5fbd98ca98
commit 455c7dfb69
8 changed files with 471 additions and 506 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,119 @@
import { getContinuationToken, getPlaylistVideos } from "../utils/parser";
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;
videos!: [];
private _continuation: { api?: string; token?: string; clientVersion?: string } = {};
constructor(data : any, searchResult : Boolean = false){
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
if(searchResult) this.__patchSearch(data)
else this.__patch(data)
}
private __patch(data:any){
this.id = data.id || undefined;
this.title = data.title || undefined;
this.videoCount = data.videoCount || 0;
this.lastUpdate = data.lastUpdate || undefined;
this.views = data.views || 0;
this.url = data.url || undefined;
this.link = data.link || undefined;
this.channel = data.author || undefined;
this.thumbnail = data.thumbnail || undefined;
this.videos = data.videos || [];
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.title = data.title || undefined;
this.thumbnail = data.thumbnail || undefined;
this.channel = data.channel || undefined;
this.videos = [];
this.videoCount = data.videos || 0;
this.url = this.id ? `https://www.youtube.com/playlist?list=${this.id}` : undefined;
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._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 >= max) break;
const res = await this.next();
if (!res.length) break;
}
return this;
}
get type(): "playlist" {
return "playlist";
}
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

@@ -1,3 +1,6 @@
import { Channel } from "./Channel";
import { Thumbnail } from "./Thumbnail";
interface VideoOptions {
id?: string;
url? : string;
@@ -7,8 +10,17 @@ interface VideoOptions {
duration: number;
uploadedAt?: string;
views: number;
thumbnail?: JSON;
channel?: JSON;
thumbnail?: {
id: string | undefined;
width: number;
height: number;
url: string | undefined;
};
channel?: {
name : string,
id : string,
icon : string
};
videos?: Video[];
type : string;
ratings : {
@@ -28,8 +40,8 @@ export class Video {
duration: number;
uploadedAt?: string;
views: number;
thumbnail?: JSON;
channel?: JSON;
thumbnail?: Thumbnail;
channel?: Channel;
videos?: Video[];
likes: number;
dislikes: number;
@@ -39,6 +51,7 @@ export class Video {
constructor(data : any){
if(!data) throw new Error(`Can not initiate ${this.constructor.name} without data`)
this.id = data.id || undefined;
this.title = data.title || undefined;
this.description = data.description || undefined;
@@ -77,8 +90,12 @@ export class Video {
duration: this.duration,
duration_formatted: this.durationFormatted,
uploadedAt: this.uploadedAt,
thumbnail: this.thumbnail,
channel: this.channel,
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,