From ca87a9877e234f488738dcd88c5fc19409da18b2 Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Mon, 6 Sep 2021 13:12:35 +0530 Subject: [PATCH] Retry Option added --- docs/YouTube/README.md | 18 ++++++++++-- examples/YouTube/play - cookies.js | 4 ++- package-lock.json | 4 +-- package.json | 2 +- play-dl/YouTube/stream.ts | 47 +++++++++++++++++------------- 5 files changed, 48 insertions(+), 27 deletions(-) diff --git a/docs/YouTube/README.md b/docs/YouTube/README.md index 42b22d6..9dce1b7 100644 --- a/docs/YouTube/README.md +++ b/docs/YouTube/README.md @@ -40,19 +40,23 @@ let id = extractID(url) ## Stream -### stream(url : `string`, cookie? : `string`) +### stream(url : `string`, options : `StreamOptions`) *This is basic to create a youtube stream from a url or videoID.* -**[Cookies](https://github.com/play-dl/play-dl/discussions/34) are optional and are required for playing age restricted videos.** ```js let source = await stream("url") // This will create a stream Class which contains type and stream to be played. + + let source = await stream("url", { + cookie : "Your Cookie", + retry : true + }) //This will enable cookies and also prevent 403 Errors from happening. let resource = createAudioResource(source.stream, { inputType : source.type }) // This creates resource for playing ``` -### stream_from_info(info : `infoData`) +### stream_from_info(info : `infoData`, options : `StreamOptions`) *This is basic to create a youtube stream from a info [ from [video_info](https://github.com/play-dl/play-dl#video_infourl--string) function ].* ```js let info = await video_info("url") @@ -62,6 +66,14 @@ let info = await video_info("url") }) // This creates resource for playing ``` +#### StreamOptions + - **cookie** : `string` + - **retry** : `boolean` + +**NOTE :** Setting retry to true will cause performance decrease in stream starting. + +**[Cookies](https://github.com/play-dl/play-dl/discussions/34) are optional and are required for playing age restricted videos.** + ## Search ### search(url : `string`, options? : [SearchOptions](https://github.com/play-dl/play-dl/tree/main/play-dl/YouTube#searchoptions)) diff --git a/examples/YouTube/play - cookies.js b/examples/YouTube/play - cookies.js index 725c585..3afc6b9 100644 --- a/examples/YouTube/play - cookies.js +++ b/examples/YouTube/play - cookies.js @@ -17,7 +17,9 @@ client.on('messageCreate', async message => { }) let args = message.content.split('play ')[1].split(' ')[0] - let stream = await play.stream(args, COOKIE) + let stream = await play.stream(args, { + cookie : COOKIE + }) /* OR if you want to get info about youtube link and then stream it diff --git a/package-lock.json b/package-lock.json index b41e031..b928d38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "play-dl", - "version": "0.8.3", + "version": "0.8.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "play-dl", - "version": "0.8.3", + "version": "0.8.4", "license": "MIT", "dependencies": { "got": "^11.8.2" diff --git a/package.json b/package.json index 8998072..b14f90a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "play-dl", - "version": "0.8.3", + "version": "0.8.4", "description": "YouTube, SoundCloud, Spotify streaming for discord.js bots", "main": "dist/index.js", "typings": "dist/index.d.ts", diff --git a/play-dl/YouTube/stream.ts b/play-dl/YouTube/stream.ts index 2423462..6e5ea81 100644 --- a/play-dl/YouTube/stream.ts +++ b/play-dl/YouTube/stream.ts @@ -10,6 +10,10 @@ export enum StreamType{ Opus = 'opus', } +interface StreamOptions{ + cookie? : string; + retry? : boolean; +} interface InfoData{ LiveStreamData : { @@ -35,22 +39,23 @@ function parseAudioFormats(formats : any[]){ return result } -export async function stream(url : string, cookie? : string): Promise{ - let info = await video_info(url, cookie) +export async function stream(url : string, options : StreamOptions = { retry : false }): Promise{ + let info = await video_info(url, options.cookie) let final: any[] = []; let type : StreamType; if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') { return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url) } - - await got(info.format[info.format.length - 1].url, { - headers : { - "range" : `bytes=0-1` - }, - retry : 0 - }).catch(async () => { - return await stream(info.video_details.url) - }) + if(options.retry){ + await got(info.format[info.format.length - 1].url, { + headers : { + "range" : `bytes=0-1` + }, + retry : 0 + }).catch(async () => { + return await stream(info.video_details.url) + }) + } let audioFormat = parseAudioFormats(info.format) let opusFormats = filterFormat(audioFormat, "opus") @@ -72,21 +77,23 @@ export async function stream(url : string, cookie? : string): Promise{ +export async function stream_from_info(info : InfoData, options : StreamOptions = { retry : false }): Promise{ let final: any[] = []; let type : StreamType; if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') { return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url) } - await got(info.format[info.format.length - 1].url, { - headers : { - "range" : `bytes=0-1` - }, - retry : 0 - }).catch(async () => { - return await stream(info.video_details.url) - }) + if(options.retry){ + await got(info.format[info.format.length - 1].url, { + headers : { + "range" : `bytes=0-1` + }, + retry : 0 + }).catch(async () => { + return await stream(info.video_details.url) + }) + } let audioFormat = parseAudioFormats(info.format) let opusFormats = filterFormat(audioFormat, "opus")