Types Improved

This commit is contained in:
killer069
2021-09-27 22:20:50 +05:30
parent 842d704bf2
commit 70d1774508
12 changed files with 104 additions and 131 deletions

View File

@@ -4,17 +4,18 @@ export interface ChannelIconInterface {
height: number;
}
export class Channel {
export class YouTubeChannel {
name?: string;
verified?: boolean;
id?: string;
type: 'video' | 'playlist' | 'channel';
url?: string;
icon?: ChannelIconInterface;
subscribers?: string;
constructor(data: any) {
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
this.type = 'channel';
this._patch(data);
}
@@ -41,10 +42,6 @@ export class Channel {
return this.icon.url.replace(`=s${def}-c`, `=s${options.size}-c`);
}
get type(): 'channel' {
return 'channel';
}
toString(): string {
return this.name || '';
}

View File

@@ -1,22 +1,27 @@
import { getPlaylistVideos, getContinuationToken } from '../utils/extractor';
import { request } from '../utils/request';
import { Thumbnail } from './Thumbnail';
import { Channel } from './Channel';
import { Video } from './Video';
import { YouTubeChannel } from './Channel';
import { YouTubeVideo } from './Video';
const BASE_API = 'https://www.youtube.com/youtubei/v1/browse?key=';
export class PlayList {
export class YouTubePlayList {
id?: string;
title?: string;
type: 'video' | 'playlist' | 'channel';
videoCount?: number;
lastUpdate?: string;
views?: number;
url?: string;
link?: string;
channel?: Channel;
thumbnail?: Thumbnail;
channel?: YouTubeChannel;
thumbnail?: {
id: string | undefined;
width: number | undefined;
height: number | undefined;
url: string | undefined;
};
private videos?: [];
private fetched_videos: Map<string, Video[]>;
private fetched_videos: Map<string, YouTubeVideo[]>;
private _continuation: {
api?: string;
token?: string;
@@ -28,6 +33,7 @@ export class PlayList {
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
this.__count = 0;
this.fetched_videos = new Map();
this.type = 'playlist';
if (searchResult) this.__patchSearch(data);
else this.__patch(data);
}
@@ -44,7 +50,7 @@ export class PlayList {
this.thumbnail = data.thumbnail || undefined;
this.videos = data.videos || [];
this.__count++;
this.fetched_videos.set(`${this.__count}`, this.videos as Video[]);
this.fetched_videos.set(`${this.__count}`, this.videos as YouTubeVideo[]);
this._continuation.api = data.continuation?.api ?? undefined;
this._continuation.token = data.continuation?.token ?? undefined;
this._continuation.clientVersion = data.continuation?.clientVersion ?? '<important data>';
@@ -63,7 +69,7 @@ export class PlayList {
this.views = 0;
}
async next(limit = Infinity): Promise<Video[]> {
async next(limit = Infinity): Promise<YouTubeVideo[]> {
if (!this._continuation || !this._continuation.token) return [];
const nextPage = await request(`${BASE_API}${this._continuation.api}`, {
@@ -109,14 +115,10 @@ export class PlayList {
return this;
}
get type(): 'playlist' {
return 'playlist';
}
page(number: number): Video[] {
page(number: number): YouTubeVideo[] {
if (!number) throw new Error('Page number is not provided');
if (!this.fetched_videos.has(`${number}`)) throw new Error('Given Page number is invalid');
return this.fetched_videos.get(`${number}`) as Video[];
return this.fetched_videos.get(`${number}`) as YouTubeVideo[];
}
get total_pages() {
@@ -125,7 +127,7 @@ export class PlayList {
get total_videos() {
const page_number: number = this.total_pages;
return (page_number - 1) * 100 + (this.fetched_videos.get(`${page_number}`) as Video[]).length;
return (page_number - 1) * 100 + (this.fetched_videos.get(`${page_number}`) as YouTubeVideo[]).length;
}
toJSON() {

View File

@@ -1,49 +0,0 @@
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,5 +1,4 @@
import { Channel } from './Channel';
import { Thumbnail } from './Thumbnail';
import { YouTubeChannel } from './Channel';
interface VideoOptions {
id?: string;
@@ -21,7 +20,6 @@ interface VideoOptions {
id: string;
icon: string;
};
videos?: Video[];
type: string;
ratings: {
likes: number;
@@ -32,9 +30,10 @@ interface VideoOptions {
tags: string[];
}
export class Video {
export class YouTubeVideo {
id?: string;
url?: string;
url: string;
type: 'video' | 'playlist' | 'channel';
title?: string;
description?: string;
durationRaw: string;
@@ -47,8 +46,7 @@ export class Video {
height: number | undefined;
url: string | undefined;
};
channel?: Channel;
videos?: Video[];
channel?: YouTubeChannel;
likes: number;
dislikes: number;
live: boolean;
@@ -60,6 +58,7 @@ export class Video {
this.id = data.id || undefined;
this.url = `https://www.youtube.com/watch?v=${this.id}`;
this.type = 'video';
this.title = data.title || undefined;
this.description = data.description || undefined;
this.durationRaw = data.duration_raw || '0:00';
@@ -75,10 +74,6 @@ export class Video {
this.tags = data.tags || [];
}
get type(): 'video' {
return 'video';
}
get toString(): string {
return this.url || '';
}