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

137 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-08-12 15:58:55 +05:30
export interface ChannelIconInterface {
2021-11-18 13:06:55 +05:30
/**
* YouTube Channel Icon URL
*/
url: string;
/**
* YouTube Channel Icon Width
*/
2021-08-12 15:58:55 +05:30
width: number;
2021-11-18 13:06:55 +05:30
/**
* YouTube Channel Icon Height
*/
2021-08-12 15:58:55 +05:30
height: number;
}
2021-09-29 20:23:16 +05:30
/**
2021-11-18 13:06:55 +05:30
* YouTube Channel Class
2021-09-29 20:23:16 +05:30
*/
2021-09-27 22:20:50 +05:30
export class YouTubeChannel {
2021-11-18 13:06:55 +05:30
/**
* YouTube Channel Title
*/
2021-08-12 15:58:55 +05:30
name?: string;
2021-11-18 13:06:55 +05:30
/**
* YouTube Channel Verified status.
*/
2021-08-13 13:16:34 +05:30
verified?: boolean;
2021-11-18 13:06:55 +05:30
/**
* YouTube Channel artist if any.
*/
artist?: boolean;
2021-11-18 13:06:55 +05:30
/**
* YouTube Channel ID.
*/
2021-08-12 15:58:55 +05:30
id?: string;
2021-11-18 13:06:55 +05:30
/**
* YouTube Class type. == "channel"
*/
2021-09-27 22:20:50 +05:30
type: 'video' | 'playlist' | 'channel';
2021-11-18 13:06:55 +05:30
/**
* YouTube Channel Url
*/
2021-08-12 15:58:55 +05:30
url?: string;
2021-11-18 13:06:55 +05:30
/**
2021-11-22 13:13:00 +05:30
* YouTube Channel Icons data.
2021-11-18 13:06:55 +05:30
*/
2021-11-22 13:13:00 +05:30
icons?: ChannelIconInterface[];
2021-11-18 13:06:55 +05:30
/**
* YouTube Channel subscribers count.
*/
2021-08-12 15:58:55 +05:30
subscribers?: string;
2021-11-18 13:06:55 +05:30
/**
* YouTube Channel Constructor
* @param data YouTube Channel data that we recieve from basic info or from search
*/
constructor(data: any = {}) {
2021-08-12 15:58:55 +05:30
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
2021-09-27 22:20:50 +05:30
this.type = 'channel';
2021-08-12 15:58:55 +05:30
this.name = data.name || null;
this.verified = !!data.verified || false;
this.artist = !!data.artist || false;
2021-08-12 15:58:55 +05:30
this.id = data.id || null;
this.url = data.url || null;
2021-11-23 09:49:22 +05:30
this.icons = data.icons || [{ url: null, width: 0, height: 0 }];
2021-08-12 15:58:55 +05:30
this.subscribers = data.subscribers || null;
}
/**
* Returns channel icon url
* @param {object} options Icon options
* @param {number} [options.size=0] Icon size. **Default is 0**
*/
2021-09-17 14:36:32 +05:30
iconURL(options = { size: 0 }): string | undefined {
if (typeof options.size !== 'number' || options.size < 0) throw new Error('invalid icon size');
2021-11-22 13:13:00 +05:30
if (!this.icons?.[0]?.url) return undefined;
const def = this.icons?.[0]?.url.split('=s')[1].split('-c')[0];
return this.icons?.[0]?.url.replace(`=s${def}-c`, `=s${options.size}-c`);
2021-08-12 15:58:55 +05:30
}
2021-11-18 13:06:55 +05:30
/**
* Converts Channel Class to channel name.
* @returns name of channel
*/
2021-08-12 15:58:55 +05:30
toString(): string {
2021-09-17 14:36:32 +05:30
return this.name || '';
2021-08-12 15:58:55 +05:30
}
2021-11-18 13:06:55 +05:30
/**
* Converts Channel Class to JSON format
* @returns json data of the channel
*/
2021-11-23 09:56:08 +05:30
toJSON(): ChannelJSON {
2021-08-12 15:58:55 +05:30
return {
name: this.name,
verified: this.verified,
artist: this.artist,
2021-08-12 15:58:55 +05:30
id: this.id,
url: this.url,
2021-11-22 13:13:00 +05:30
icons: this.icons,
2021-08-12 15:58:55 +05:30
type: this.type,
subscribers: this.subscribers
};
}
2021-09-17 14:36:32 +05:30
}
2021-11-18 13:06:55 +05:30
2021-11-23 09:56:08 +05:30
interface ChannelJSON {
2021-11-18 13:06:55 +05:30
/**
* YouTube Channel Title
*/
2021-11-23 09:56:08 +05:30
name?: string;
/**
* YouTube Channel Verified status.
*/
verified?: boolean;
/**
* YouTube Channel artist if any.
*/
artist?: boolean;
/**
* YouTube Channel ID.
*/
id?: string;
/**
* Type of Class [ Channel ]
*/
type: 'video' | 'playlist' | 'channel';
/**
* YouTube Channel Url
*/
url?: string;
/**
* YouTube Channel Icon data.
*/
icons?: ChannelIconInterface[];
/**
* YouTube Channel subscribers count.
*/
subscribers?: string;
}