Files
play-dl-test/play-dl/YouTube/classes/Thumbnail.ts

50 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-09-17 14:36:32 +05:30
type ThumbnailType = 'default' | 'hqdefault' | 'mqdefault' | 'sddefault' | 'maxresdefault' | 'ultrares';
2021-08-12 15:58:55 +05:30
export class Thumbnail {
id?: string;
2021-08-13 13:16:34 +05:30
width?: number;
height?: number;
2021-08-12 15:58:55 +05:30
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;
}
2021-09-17 14:36:32 +05:30
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;
2021-08-12 15:58:55 +05:30
return `https://i3.ytimg.com/vi/${this.id}/${thumbnailType}.jpg`;
}
2021-09-17 14:36:32 +05:30
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}"!`);
2021-08-12 15:58:55 +05:30
return `https://i3.ytimg.com/vi/${this.id}/${id}.jpg`;
}
toString(): string {
2021-09-17 14:36:32 +05:30
return this.url ? `${this.url}` : '';
2021-08-12 15:58:55 +05:30
}
toJSON() {
return {
id: this.id,
width: this.width,
height: this.height,
url: this.url
};
}
}