Merge pull request #51 from play-dl/developer

Retry Options fixed
This commit is contained in:
Killer069 2021-09-06 16:34:45 +05:30 committed by GitHub
commit 64137c52d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 43 deletions

View File

@ -40,23 +40,19 @@ let id = extractID(url)
## Stream ## Stream
### stream(url : `string`, options : [StreamOptions](https://github.com/play-dl/play-dl/tree/main/docs/YouTube#streamoptions)) ### stream(url : `string`, cookie? : `string`)
*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`, options : [StreamOptions](https://github.com/play-dl/play-dl/tree/main/docs/YouTube#streamoptions)) ### stream_from_info(info : `infoData`)
*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")
@ -66,14 +62,6 @@ 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))

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "play-dl", "name": "play-dl",
"version": "0.8.4", "version": "0.8.5",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {

View File

@ -1,6 +1,6 @@
{ {
"name": "play-dl", "name": "play-dl",
"version": "0.8.4", "version": "0.8.5",
"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,11 +10,6 @@ export enum StreamType{
Opus = 'opus', Opus = 'opus',
} }
interface StreamOptions{
cookie? : string;
retry? : boolean;
}
interface InfoData{ interface InfoData{
LiveStreamData : { LiveStreamData : {
isLive : boolean isLive : boolean
@ -39,24 +34,28 @@ function parseAudioFormats(formats : any[]){
return result return result
} }
export async function stream(url : string, options : StreamOptions = { retry : false }): Promise<Stream | LiveStreaming>{ export async function stream(url : string, cookie? : string): Promise<Stream | LiveStreaming>{
let info = await video_info(url, options.cookie) let info = await video_info(url, 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){ console.time('Time to Retry')
await got(info.format[info.format.length - 1].url, { let resp = 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(() => {
return await stream(info.video_details.url) return 0
}) })
if(resp === 0){
return await stream(info.video_details.url)
} }
else if(typeof resp !== "number") resp.destroy()
console.timeEnd('Time to Retry')
let audioFormat = parseAudioFormats(info.format) let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus") let opusFormats = filterFormat(audioFormat, "opus")
@ -77,23 +76,25 @@ export async function stream(url : string, options : StreamOptions = { retry : f
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, options : StreamOptions = { retry : false }): Promise<Stream | LiveStreaming>{ export async function stream_from_info(info : InfoData): 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)
} }
if(options.retry){ let resp = 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(() => {
}).catch(async () => { return 0
return await stream(info.video_details.url) })
}) if(resp === 0){
return await stream(info.video_details.url)
} }
else if(typeof resp !== "number") resp.destroy()
let audioFormat = parseAudioFormats(info.format) let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus") let opusFormats = filterFormat(audioFormat, "opus")