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

294 lines
8.6 KiB
TypeScript
Raw Normal View History

2021-09-17 14:36:32 +05:30
import { getPlaylistVideos, getContinuationToken } from '../utils/extractor';
2021-10-11 18:38:08 +05:30
import { request } from '../../Request';
2021-09-27 22:20:50 +05:30
import { YouTubeChannel } from './Channel';
import { YouTubeVideo } from './Video';
2021-11-18 15:38:25 +05:30
import { YouTubeThumbnail } from './Thumbnail';
2021-09-17 14:36:32 +05:30
const BASE_API = 'https://www.youtube.com/youtubei/v1/browse?key=';
2021-09-29 20:23:16 +05:30
/**
2021-11-18 13:06:55 +05:30
* YouTube Playlist Class containing vital informations about playlist.
2021-09-29 20:23:16 +05:30
*/
2021-09-27 22:20:50 +05:30
export class YouTubePlayList {
2021-11-18 13:06:55 +05:30
/**
* YouTube Playlist ID
*/
2021-08-12 15:58:55 +05:30
id?: string;
2021-11-18 13:06:55 +05:30
/**
* YouTube Playlist Name
*/
2021-08-12 15:58:55 +05:30
title?: string;
2021-11-18 13:06:55 +05:30
/**
* YouTube Class type. == "playlist"
*/
2021-09-27 22:20:50 +05:30
type: 'video' | 'playlist' | 'channel';
2021-11-18 13:06:55 +05:30
/**
* Total no of videos in that playlist
*/
2021-08-13 13:16:34 +05:30
videoCount?: number;
2021-11-18 13:06:55 +05:30
/**
* Time when playlist was last updated
*/
2021-08-12 15:58:55 +05:30
lastUpdate?: string;
2021-11-18 13:06:55 +05:30
/**
* Total views of that playlist
*/
2021-08-12 15:58:55 +05:30
views?: number;
2021-11-18 13:06:55 +05:30
/**
* YouTube Playlist url
*/
2021-08-12 15:58:55 +05:30
url?: string;
2021-11-18 13:06:55 +05:30
/**
* YouTube Playlist url with starting video url.
*/
2021-08-12 15:58:55 +05:30
link?: string;
2021-11-18 13:06:55 +05:30
/**
* YouTube Playlist channel data
*/
2021-09-27 22:20:50 +05:30
channel?: YouTubeChannel;
2021-11-18 13:06:55 +05:30
/**
* YouTube Playlist thumbnail Data
*/
2021-11-23 09:56:08 +05:30
thumbnail?: YouTubeThumbnail;
2021-11-18 13:06:55 +05:30
/**
* Videos array containing data of first 100 videos
*/
private videos?: YouTubeVideo[];
/**
* Map contaning data of all fetched videos
*/
2021-09-27 22:20:50 +05:30
private fetched_videos: Map<string, YouTubeVideo[]>;
2021-11-18 13:06:55 +05:30
/**
* Token containing API key, Token, ClientVersion.
*/
2021-09-17 14:36:32 +05:30
private _continuation: {
api?: string;
token?: string;
clientVersion?: string;
} = {};
2021-11-18 13:06:55 +05:30
/**
* Total no of pages count.
*/
2021-09-17 14:36:32 +05:30
private __count: number;
2021-11-18 13:06:55 +05:30
/**
* Constructor for YouTube Playlist Class
* @param data Json Parsed YouTube Playlist data
* @param searchResult If the data is from search or not
*/
2021-09-17 14:36:32 +05:30
constructor(data: any, searchResult = false) {
2021-08-12 15:58:55 +05:30
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
2021-09-17 14:36:32 +05:30
this.__count = 0;
this.fetched_videos = new Map();
2021-09-27 22:20:50 +05:30
this.type = 'playlist';
2021-09-17 14:36:32 +05:30
if (searchResult) this.__patchSearch(data);
else this.__patch(data);
2021-08-12 15:58:55 +05:30
}
2021-11-18 13:06:55 +05:30
/**
* Updates variable according to a normal data.
* @param data Json Parsed YouTube Playlist data
*/
2021-09-17 14:36:32 +05:30
private __patch(data: any) {
2021-08-12 15:58:55 +05:30
this.id = data.id || undefined;
2021-08-13 13:16:34 +05:30
this.url = data.url || undefined;
2021-08-12 15:58:55 +05:30
this.title = data.title || undefined;
this.videoCount = data.videoCount || 0;
this.lastUpdate = data.lastUpdate || undefined;
this.views = data.views || 0;
this.link = data.link || undefined;
this.channel = data.author || undefined;
this.thumbnail = data.thumbnail || undefined;
this.videos = data.videos || [];
2021-09-17 14:36:32 +05:30
this.__count++;
2021-09-27 22:20:50 +05:30
this.fetched_videos.set(`${this.__count}`, this.videos as YouTubeVideo[]);
2021-08-12 15:58:55 +05:30
this._continuation.api = data.continuation?.api ?? undefined;
this._continuation.token = data.continuation?.token ?? undefined;
2021-09-17 14:36:32 +05:30
this._continuation.clientVersion = data.continuation?.clientVersion ?? '<important data>';
2021-08-12 15:58:55 +05:30
}
2021-11-18 13:06:55 +05:30
/**
* Updates variable according to a searched data.
* @param data Json Parsed YouTube Playlist data
*/
2021-09-17 14:36:32 +05:30
private __patchSearch(data: any) {
2021-08-12 15:58:55 +05:30
this.id = data.id || undefined;
2021-08-13 13:16:34 +05:30
this.url = this.id ? `https://www.youtube.com/playlist?list=${this.id}` : undefined;
2021-08-12 15:58:55 +05:30
this.title = data.title || undefined;
this.thumbnail = data.thumbnail || undefined;
this.channel = data.channel || undefined;
this.videos = [];
this.videoCount = data.videos || 0;
this.link = undefined;
this.lastUpdate = undefined;
this.views = 0;
}
2021-11-18 13:06:55 +05:30
/**
* Parses next segment of videos from playlist and returns parsed data.
* @param limit Total no of videos to parse.
2021-11-23 09:56:08 +05:30
*
2021-11-18 13:06:55 +05:30
* Default = Infinity
* @returns Array of YouTube Video Class
*/
2021-09-27 22:20:50 +05:30
async next(limit = Infinity): Promise<YouTubeVideo[]> {
2021-08-12 15:58:55 +05:30
if (!this._continuation || !this._continuation.token) return [];
2021-09-17 14:36:32 +05:30
const nextPage = await request(`${BASE_API}${this._continuation.api}`, {
method: 'POST',
2021-08-12 15:58:55 +05:30
body: JSON.stringify({
continuation: this._continuation.token,
context: {
client: {
utcOffsetMinutes: 0,
2021-09-17 14:36:32 +05:30
gl: 'US',
hl: 'en',
clientName: 'WEB',
2021-08-12 15:58:55 +05:30
clientVersion: this._continuation.clientVersion
},
user: {},
request: {}
}
})
});
2021-09-17 14:36:32 +05:30
const contents =
JSON.parse(nextPage)?.onResponseReceivedActions[0]?.appendContinuationItemsAction?.continuationItems;
if (!contents) return [];
const playlist_videos = getPlaylistVideos(contents, limit);
this.fetched_videos.set(`${this.__count}`, playlist_videos);
2021-09-17 14:36:32 +05:30
this._continuation.token = getContinuationToken(contents);
return playlist_videos;
2021-08-12 15:58:55 +05:30
}
2021-11-18 13:06:55 +05:30
/**
* Fetches remaining data from playlist
2021-11-23 09:56:08 +05:30
*
2021-11-19 13:49:21 +05:30
* For fetching and getting all songs data, see `total_pages` property.
2021-11-18 13:06:55 +05:30
* @param max Max no of videos to fetch
2021-11-23 09:56:08 +05:30
*
2021-11-18 13:06:55 +05:30
* Default = Infinity
2021-11-23 09:56:08 +05:30
* @returns
2021-11-18 13:06:55 +05:30
*/
async fetch(max = Infinity): Promise<YouTubePlayList> {
2021-09-17 14:36:32 +05:30
const continuation = this._continuation.token;
2021-08-12 15:58:55 +05:30
if (!continuation) return this;
if (max < 1) max = Infinity;
2021-09-17 14:36:32 +05:30
while (typeof this._continuation.token === 'string' && this._continuation.token.length) {
this.__count++;
2021-08-12 15:58:55 +05:30
const res = await this.next();
2021-11-18 13:06:55 +05:30
max -= res.length;
2021-11-23 09:56:08 +05:30
if (max <= 0) break;
2021-08-12 15:58:55 +05:30
if (!res.length) break;
}
return this;
}
2021-11-18 13:06:55 +05:30
/**
* YouTube Playlist is divided into pages.
2021-11-23 09:56:08 +05:30
*
2021-11-18 13:06:55 +05:30
* For example, if you want to get 101 - 200 songs
2021-11-23 09:56:08 +05:30
*
2021-11-18 13:06:55 +05:30
* ```ts
2021-11-19 13:49:21 +05:30
* const playlist = await play.playlist_info('playlist url')
2021-11-23 09:56:08 +05:30
*
2021-11-18 13:06:55 +05:30
* await playlist.fetch()
2021-11-23 09:56:08 +05:30
*
2021-11-18 13:06:55 +05:30
* const result = playlist.page(2)
* ```
* @param number Page number
* @returns Array of YouTube Video Class
*/
2021-09-27 22:20:50 +05:30
page(number: number): YouTubeVideo[] {
2021-09-17 14:36:32 +05:30
if (!number) throw new Error('Page number is not provided');
if (!this.fetched_videos.has(`${number}`)) throw new Error('Given Page number is invalid');
2021-09-27 22:20:50 +05:30
return this.fetched_videos.get(`${number}`) as YouTubeVideo[];
2021-08-13 13:16:34 +05:30
}
2021-11-18 13:06:55 +05:30
/**
* Gets total no of pages in that playlist class.
2021-11-23 09:56:08 +05:30
*
2021-11-18 13:06:55 +05:30
* For getting all songs in a playlist
2021-11-23 09:56:08 +05:30
*
2021-11-18 13:06:55 +05:30
* ```ts
2021-11-19 13:49:21 +05:30
* const playlist = await play.playlist_info('playlist url');
2021-11-23 09:56:08 +05:30
*
2021-11-18 13:06:55 +05:30
* await playlist.fetch();
2021-11-23 09:56:08 +05:30
*
2021-11-18 13:06:55 +05:30
* let result = [];
2021-11-23 09:56:08 +05:30
*
2021-11-19 13:49:21 +05:30
* for (let i = 0; i <= playlist.total_pages; i++) {
2021-11-18 13:06:55 +05:30
* result.push(playlist.page(i));
* }
* ```
*/
2021-09-17 14:36:32 +05:30
get total_pages() {
return this.fetched_videos.size;
2021-08-13 13:16:34 +05:30
}
2021-11-18 13:06:55 +05:30
/**
* This tells total no of videos that have been fetched so far.
2021-11-23 09:56:08 +05:30
*
2021-11-18 13:06:55 +05:30
* This can be equal to videosCount if all videos in playlist have been fetched and they are not hidden.
*/
2021-09-17 14:36:32 +05:30
get total_videos() {
const page_number: number = this.total_pages;
2021-09-27 22:20:50 +05:30
return (page_number - 1) * 100 + (this.fetched_videos.get(`${page_number}`) as YouTubeVideo[]).length;
2021-08-13 13:16:34 +05:30
}
2021-11-18 13:06:55 +05:30
/**
* Converts Playlist Class to a json parsed data.
2021-11-23 09:56:08 +05:30
* @returns
2021-11-18 13:06:55 +05:30
*/
2021-11-23 09:56:08 +05:30
toJSON(): PlaylistJSON {
2021-08-12 15:58:55 +05:30
return {
id: this.id,
title: this.title,
2021-11-18 15:38:25 +05:30
thumbnail: this.thumbnail?.toJSON() || this.thumbnail,
2021-11-18 13:06:55 +05:30
channel: this.channel,
2021-08-12 15:58:55 +05:30
url: this.url,
videos: this.videos
};
}
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 PlaylistJSON {
2021-11-18 13:06:55 +05:30
/**
* YouTube Playlist ID
*/
2021-11-23 09:56:08 +05:30
id?: string;
/**
* YouTube Playlist Name
*/
title?: string;
/**
* Total no of videos in that playlist
*/
videoCount?: number;
/**
* Time when playlist was last updated
*/
lastUpdate?: string;
/**
* Total views of that playlist
*/
views?: number;
/**
* YouTube Playlist url
*/
url?: string;
/**
* YouTube Playlist url with starting video url.
*/
link?: string;
/**
* YouTube Playlist channel data
*/
channel?: YouTubeChannel;
/**
* YouTube Playlist thumbnail Data
*/
thumbnail?: {
id: string | undefined;
width: number | undefined;
height: number | undefined;
url: string | undefined;
};
/**
* first 100 videos in that playlist
*/
videos?: YouTubeVideo[];
}