Some playlist and seek fixes

This commit is contained in:
killer069 2021-12-27 12:09:08 +05:30
parent 8f805bbbdd
commit 6ff34c2baa
3 changed files with 25 additions and 19 deletions

View File

@ -92,8 +92,8 @@ export class SeekStream {
* @param sec No of seconds to seek to * @param sec No of seconds to seek to
* @returns Nothing * @returns Nothing
*/ */
private async seek(sec: number) { private async seek(sec: number): Promise<void> {
await new Promise(async (res) => { const parse = await new Promise(async (res, rej) => {
if (!this.stream.headerparsed) { if (!this.stream.headerparsed) {
const stream = await request_stream(this.url, { const stream = await request_stream(this.url, {
headers: { headers: {
@ -102,13 +102,13 @@ export class SeekStream {
}).catch((err: Error) => err); }).catch((err: Error) => err);
if (stream instanceof Error) { if (stream instanceof Error) {
this.stream.emit('error', stream); rej(stream)
this.bytes_count = 0; return;
this.per_sec_bytes = 0; }
this.cleanup(); if (Number(stream.statusCode) >= 400) {
rej(400)
return; return;
} }
this.request = stream; this.request = stream;
stream.pipe(this.stream, { end: false }); stream.pipe(this.stream, { end: false });
@ -117,8 +117,19 @@ export class SeekStream {
res(''); res('');
}); });
} else res(''); } else res('');
}); }).catch((err) => err);
if(parse instanceof Error){
this.stream.emit('error', parse);
this.bytes_count = 0;
this.per_sec_bytes = 0;
this.cleanup();
return;
}
else if(parse === 400){
await this.retry();
this.timer.reuse()
return this.seek(sec);
}
const bytes = this.stream.seek(sec); const bytes = this.stream.seek(sec);
if (bytes instanceof Error) { if (bytes instanceof Error) {
this.stream.emit('error', bytes); this.stream.emit('error', bytes);

View File

@ -85,7 +85,7 @@ export class WebmSeeker extends Duplex {
break; break;
} else continue; } else continue;
} }
if (position === 0) return Error('Failed to find Cluster Position'); if (position === 0) return new Error('Failed to find Cluster Position');
else return position; else return position;
} }

View File

@ -20,7 +20,7 @@ const DEFAULT_API_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8';
const video_pattern = const video_pattern =
/^((?:https?:)?\/\/)?(?:(?:www|m|music)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|shorts\/|embed\/|v\/)?)([\w\-]+)(\S+)?$/; /^((?:https?:)?\/\/)?(?:(?:www|m|music)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|shorts\/|embed\/|v\/)?)([\w\-]+)(\S+)?$/;
const playlist_pattern = const playlist_pattern =
/^((?:https?:)?\/\/)?(?:(?:www|m)\.)?(youtube\.com)\/(?:(playlist|watch))(.*)?((\?|\&)list=)(PL|UU|LL|RD|OL)[a-zA-Z\d_-]{10,}(.*)?$/; /^((?:https?:)?\/\/)?(?:(?:www|m|music)\.)?(youtube\.com)\/(?:(playlist|watch))(.*)?((\?|\&)list=)(PL|UU|LL|RD|OL)[a-zA-Z\d_-]{10,}(.*)?$/;
/** /**
* Validate YouTube URL or ID. * Validate YouTube URL or ID.
* *
@ -335,11 +335,6 @@ export async function playlist_info(url: string, options: PlaylistOptions = {}):
if (!url.startsWith('https')) url = `https://www.youtube.com/playlist?list=${url}`; if (!url.startsWith('https')) url = `https://www.youtube.com/playlist?list=${url}`;
if (url.indexOf('list=') === -1) throw new Error('This is not a Playlist URL'); if (url.indexOf('list=') === -1) throw new Error('This is not a Playlist URL');
if (yt_validate(url) === 'playlist') {
const id = extractID(url);
url = `https://www.youtube.com/playlist?list=${id}`;
}
const body = await request(url, { const body = await request(url, {
headers: { headers: {
'accept-language': options.language || 'en-US;q=0.9' 'accept-language': options.language || 'en-US;q=0.9'
@ -364,7 +359,7 @@ export async function playlist_info(url: string, options: PlaylistOptions = {}):
else throw new Error('While parsing playlist url\nUnknown Playlist Error'); else throw new Error('While parsing playlist url\nUnknown Playlist Error');
} }
if (url.indexOf('watch?v=') !== -1) { if (url.indexOf('watch?v=') !== -1) {
return getWatchPlaylist(response, body); return getWatchPlaylist(response, body, url);
} else return getNormalPlaylist(response, body); } else return getNormalPlaylist(response, body);
} }
/** /**
@ -412,7 +407,7 @@ export function getContinuationToken(data: any): string {
.continuationEndpoint?.continuationCommand?.token; .continuationEndpoint?.continuationCommand?.token;
} }
function getWatchPlaylist(response: any, body: any): YouTubePlayList { function getWatchPlaylist(response: any, body: any, url : string): YouTubePlayList {
const playlist_details = response.contents.twoColumnWatchNextResults.playlist.playlist; const playlist_details = response.contents.twoColumnWatchNextResults.playlist.playlist;
const videos = getWatchPlaylistVideos(playlist_details.contents); const videos = getWatchPlaylistVideos(playlist_details.contents);
@ -438,7 +433,7 @@ function getWatchPlaylist(response: any, body: any): YouTubePlayList {
title: playlist_details.title || '', title: playlist_details.title || '',
videoCount: parseInt(videoCount) || 0, videoCount: parseInt(videoCount) || 0,
videos: videos, videos: videos,
url: `https://www.youtube.com/playlist?list=${playlist_details.playlistId}`, url: url,
channel: { channel: {
id: channel?.navigationEndpoint?.browseEndpoint?.browseId || null, id: channel?.navigationEndpoint?.browseEndpoint?.browseId || null,
name: channel?.text || null, name: channel?.text || null,