Retry Option added

This commit is contained in:
killer069 2021-09-06 13:12:35 +05:30
parent 3c0e358e20
commit ca87a9877e
5 changed files with 48 additions and 27 deletions

View File

@ -40,19 +40,23 @@ let id = extractID(url)
## Stream ## Stream
### stream(url : `string`, cookie? : `string`) ### stream(url : `string`, options : `StreamOptions`)
*This is basic to create a youtube stream from a url or videoID.* *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 ```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") // 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, { let resource = createAudioResource(source.stream, {
inputType : source.type inputType : source.type
}) // This creates resource for playing }) // 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 ].* *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 ```js
let info = await video_info("url") let info = await video_info("url")
@ -62,6 +66,14 @@ let info = await video_info("url")
}) // This creates resource for playing }) // 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
### search(url : `string`, options? : [SearchOptions](https://github.com/play-dl/play-dl/tree/main/play-dl/YouTube#searchoptions)) ### search(url : `string`, options? : [SearchOptions](https://github.com/play-dl/play-dl/tree/main/play-dl/YouTube#searchoptions))

View File

@ -17,7 +17,9 @@ client.on('messageCreate', async message => {
}) })
let args = message.content.split('play ')[1].split(' ')[0] 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 OR if you want to get info about youtube link and then stream it

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "play-dl", "name": "play-dl",
"version": "0.8.3", "version": "0.8.4",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "play-dl", "name": "play-dl",
"version": "0.8.3", "version": "0.8.4",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"got": "^11.8.2" "got": "^11.8.2"

View File

@ -1,6 +1,6 @@
{ {
"name": "play-dl", "name": "play-dl",
"version": "0.8.3", "version": "0.8.4",
"description": "YouTube, SoundCloud, Spotify streaming for discord.js bots", "description": "YouTube, SoundCloud, Spotify streaming for discord.js bots",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",

View File

@ -10,6 +10,10 @@ export enum StreamType{
Opus = 'opus', Opus = 'opus',
} }
interface StreamOptions{
cookie? : string;
retry? : boolean;
}
interface InfoData{ interface InfoData{
LiveStreamData : { LiveStreamData : {
@ -35,22 +39,23 @@ function parseAudioFormats(formats : any[]){
return result return result
} }
export async function stream(url : string, cookie? : string): Promise<Stream | LiveStreaming>{ export async function stream(url : string, options : StreamOptions = { retry : false }): Promise<Stream | LiveStreaming>{
let info = await video_info(url, cookie) let info = await video_info(url, options.cookie)
let final: any[] = []; let final: any[] = [];
let type : StreamType; let type : StreamType;
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') { 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) return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url)
} }
if(options.retry){
await got(info.format[info.format.length - 1].url, { await got(info.format[info.format.length - 1].url, {
headers : { headers : {
"range" : `bytes=0-1` "range" : `bytes=0-1`
}, },
retry : 0 retry : 0
}).catch(async () => { }).catch(async () => {
return await stream(info.video_details.url) return await stream(info.video_details.url)
}) })
}
let audioFormat = parseAudioFormats(info.format) let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus") let opusFormats = filterFormat(audioFormat, "opus")
@ -72,21 +77,23 @@ export async function stream(url : string, cookie? : string): Promise<Stream | L
return new Stream(final[0].url, type, info.video_details.durationInSec) return new Stream(final[0].url, type, info.video_details.durationInSec)
} }
export async function stream_from_info(info : InfoData): Promise<Stream | LiveStreaming>{ export async function stream_from_info(info : InfoData, options : StreamOptions = { retry : false }): Promise<Stream | LiveStreaming>{
let final: any[] = []; let final: any[] = [];
let type : StreamType; let type : StreamType;
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') { 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) 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, { if(options.retry){
headers : { await got(info.format[info.format.length - 1].url, {
"range" : `bytes=0-1` headers : {
}, "range" : `bytes=0-1`
retry : 0 },
}).catch(async () => { retry : 0
return await stream(info.video_details.url) }).catch(async () => {
}) return await stream(info.video_details.url)
})
}
let audioFormat = parseAudioFormats(info.format) let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus") let opusFormats = filterFormat(audioFormat, "opus")