mirror of
https://github.com/YuzuZensai/play-dl-test.git
synced 2026-01-31 14:58:05 +00:00
SoundCloud work + prettier
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { getPlaylistVideos, getContinuationToken } from "../utils/extractor";
|
||||
import { request } from "../utils/request";
|
||||
import { Thumbnail } from "./Thumbnail";
|
||||
import { Channel } from "./Channel";
|
||||
import { Video } from "./Video";
|
||||
const BASE_API = "https://www.youtube.com/youtubei/v1/browse?key=";
|
||||
import { getPlaylistVideos, getContinuationToken } from '../utils/extractor';
|
||||
import { request } from '../utils/request';
|
||||
import { Thumbnail } from './Thumbnail';
|
||||
import { Channel } from './Channel';
|
||||
import { Video } from './Video';
|
||||
const BASE_API = 'https://www.youtube.com/youtubei/v1/browse?key=';
|
||||
|
||||
export class PlayList{
|
||||
export class PlayList {
|
||||
id?: string;
|
||||
title?: string;
|
||||
videoCount?: number;
|
||||
@@ -16,19 +16,23 @@ export class PlayList{
|
||||
channel?: Channel;
|
||||
thumbnail?: Thumbnail;
|
||||
private videos?: [];
|
||||
private fetched_videos : Map<string, Video[]>
|
||||
private _continuation: { api?: string; token?: string; clientVersion?: string } = {};
|
||||
private __count : number
|
||||
private fetched_videos: Map<string, Video[]>;
|
||||
private _continuation: {
|
||||
api?: string;
|
||||
token?: string;
|
||||
clientVersion?: string;
|
||||
} = {};
|
||||
private __count: number;
|
||||
|
||||
constructor(data : any, searchResult : Boolean = false){
|
||||
constructor(data: any, searchResult = 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)
|
||||
this.__count = 0;
|
||||
this.fetched_videos = new Map();
|
||||
if (searchResult) this.__patchSearch(data);
|
||||
else this.__patch(data);
|
||||
}
|
||||
|
||||
private __patch(data:any){
|
||||
private __patch(data: any) {
|
||||
this.id = data.id || undefined;
|
||||
this.url = data.url || undefined;
|
||||
this.title = data.title || undefined;
|
||||
@@ -39,14 +43,14 @@ export class PlayList{
|
||||
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.__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>";
|
||||
this._continuation.clientVersion = data.continuation?.clientVersion ?? '<important data>';
|
||||
}
|
||||
|
||||
private __patchSearch(data: any){
|
||||
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;
|
||||
@@ -59,19 +63,19 @@ export class PlayList{
|
||||
this.views = 0;
|
||||
}
|
||||
|
||||
async next(limit: number = Infinity): Promise<Video[]> {
|
||||
async next(limit = Infinity): Promise<Video[]> {
|
||||
if (!this._continuation || !this._continuation.token) return [];
|
||||
|
||||
let nextPage = await request(`${BASE_API}${this._continuation.api}`, {
|
||||
method: "POST",
|
||||
const nextPage = await request(`${BASE_API}${this._continuation.api}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
continuation: this._continuation.token,
|
||||
context: {
|
||||
client: {
|
||||
utcOffsetMinutes: 0,
|
||||
gl: "US",
|
||||
hl: "en",
|
||||
clientName: "WEB",
|
||||
gl: 'US',
|
||||
hl: 'en',
|
||||
clientName: 'WEB',
|
||||
clientVersion: this._continuation.clientVersion
|
||||
},
|
||||
user: {},
|
||||
@@ -79,24 +83,25 @@ export class PlayList{
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
let contents = JSON.parse(nextPage)?.onResponseReceivedActions[0]?.appendContinuationItemsAction?.continuationItems
|
||||
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
|
||||
const contents =
|
||||
JSON.parse(nextPage)?.onResponseReceivedActions[0]?.appendContinuationItemsAction?.continuationItems;
|
||||
if (!contents) return [];
|
||||
|
||||
const playlist_videos = getPlaylistVideos(contents, limit);
|
||||
this.fetched_videos.set(`page${this.__count}`, playlist_videos);
|
||||
this._continuation.token = getContinuationToken(contents);
|
||||
return playlist_videos;
|
||||
}
|
||||
|
||||
async fetch(max: number = Infinity) {
|
||||
let continuation = this._continuation.token;
|
||||
async fetch(max = Infinity) {
|
||||
const continuation = this._continuation.token;
|
||||
if (!continuation) return this;
|
||||
if (max < 1) max = Infinity;
|
||||
|
||||
while (typeof this._continuation.token === "string" && this._continuation.token.length) {
|
||||
if (this.videos?.length as number >= max) break;
|
||||
this.__count++
|
||||
while (typeof this._continuation.token === 'string' && this._continuation.token.length) {
|
||||
if ((this.videos?.length as number) >= max) break;
|
||||
this.__count++;
|
||||
const res = await this.next();
|
||||
if (!res.length) break;
|
||||
}
|
||||
@@ -104,23 +109,23 @@ export class PlayList{
|
||||
return this;
|
||||
}
|
||||
|
||||
get type(): "playlist" {
|
||||
return "playlist";
|
||||
get type(): 'playlist' {
|
||||
return 'playlist';
|
||||
}
|
||||
|
||||
page(number : number): Video[]{
|
||||
if(!number) throw new Error('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[]
|
||||
page(number: number): Video[] {
|
||||
if (!number) throw new Error('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_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
|
||||
get total_videos() {
|
||||
const page_number: number = this.total_pages;
|
||||
return (page_number - 1) * 100 + (this.fetched_videos.get(`page${page_number}`) as Video[]).length;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
@@ -129,12 +134,12 @@ export class PlayList{
|
||||
title: this.title,
|
||||
thumbnail: this.thumbnail,
|
||||
channel: {
|
||||
name : this.channel?.name,
|
||||
id : this.channel?.id,
|
||||
icon : this.channel?.iconURL()
|
||||
name: this.channel?.name,
|
||||
id: this.channel?.id,
|
||||
icon: this.channel?.iconURL()
|
||||
},
|
||||
url: this.url,
|
||||
videos: this.videos
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user