YouTube : Completed

This commit is contained in:
killer069
2021-08-13 13:16:34 +05:30
parent 455c7dfb69
commit 04770cc929
10 changed files with 215 additions and 76 deletions

View File

@@ -6,10 +6,10 @@ export interface ChannelIconInterface {
export class Channel {
name?: string;
verified!: boolean;
verified?: boolean;
id?: string;
url?: string;
icon!: ChannelIconInterface;
icon?: ChannelIconInterface;
subscribers?: string;
constructor(data: any) {
@@ -36,7 +36,7 @@ export class Channel {
*/
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;
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`);
}

View File

@@ -1,41 +1,47 @@
import { getContinuationToken, getPlaylistVideos } from "../utils/parser";
import { getPlaylistVideos, getContinuationToken } from "../utils/extractor";
import { url_get } from "../utils/request";
import { Thumbnail } from "./Thumbnail";
import { Channel } from "./Channel";
import { Video } from "./Video";
import fs from 'fs'
const BASE_API = "https://www.youtube.com/youtubei/v1/browse?key=";
export class PlayList{
id?: string;
title?: string;
videoCount!: number;
videoCount?: number;
lastUpdate?: string;
views?: number;
url?: string;
link?: string;
channel?: Channel;
thumbnail?: Thumbnail;
videos!: [];
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.url = data.url || undefined;
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>";
@@ -43,12 +49,12 @@ export class PlayList{
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.url = this.id ? `https://www.youtube.com/playlist?list=${this.id}` : undefined;
this.link = undefined;
this.lastUpdate = undefined;
this.views = 0;
@@ -79,8 +85,8 @@ export class PlayList{
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
}
@@ -90,7 +96,8 @@ export class PlayList{
if (max < 1) max = Infinity;
while (typeof this._continuation.token === "string" && this._continuation.token.length) {
if (this.videos.length >= max) break;
if (this.videos?.length as number >= max) break;
this.__count++
const res = await this.next();
if (!res.length) break;
}
@@ -102,6 +109,21 @@ export class 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,

View File

@@ -2,8 +2,8 @@ type ThumbnailType = "default" | "hqdefault" | "mqdefault" | "sddefault" | "maxr
export class Thumbnail {
id?: string;
width!: number;
height!: number;
width?: number;
height?: number;
url?: string;
constructor(data: any) {

View File

@@ -12,8 +12,8 @@ interface VideoOptions {
views: number;
thumbnail?: {
id: string | undefined;
width: number;
height: number;
width: number | undefined ;
height: number | undefined;
url: string | undefined;
};
channel?: {
@@ -34,6 +34,7 @@ interface VideoOptions {
export class Video {
id?: string;
url? : string;
title?: string;
description?: string;
durationFormatted: string;
@@ -53,6 +54,7 @@ export class Video {
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";
@@ -68,11 +70,6 @@ export class Video {
this.tags = data.tags || [];
}
get url(){
if(!this.id) return undefined
else return `https://www.youtube.com/watch?v=${this.id}`;
}
get type(): "video" {
return "video";
}