mirror of
https://github.com/YuzuZensai/play-dl-test.git
synced 2026-01-31 14:58:05 +00:00
Fully https working
This commit is contained in:
@@ -116,14 +116,22 @@ export class Stream {
|
||||
private bytes_count : number;
|
||||
private per_sec_bytes : number;
|
||||
private content_length : number;
|
||||
private video_url : string;
|
||||
private timer : NodeJS.Timer;
|
||||
private cookie : string;
|
||||
private data_ended : boolean;
|
||||
private playing_count : number;
|
||||
private request : IncomingMessage | null
|
||||
constructor(url : string, type : StreamType, duration : number, contentLength : number){
|
||||
constructor(url : string, type : StreamType, duration : number, contentLength : number, video_url : string, cookie : string){
|
||||
this.url = url
|
||||
this.type = type
|
||||
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
||||
this.bytes_count = 0
|
||||
this.video_url = video_url
|
||||
this.cookie = cookie
|
||||
this.timer = setInterval(() => {
|
||||
this.retry()
|
||||
}, 7200 * 1000)
|
||||
this.per_sec_bytes = Math.ceil(contentLength / duration)
|
||||
this.content_length = contentLength
|
||||
this.request = null
|
||||
@@ -146,7 +154,13 @@ export class Stream {
|
||||
this.loop()
|
||||
}
|
||||
|
||||
private async retry(){
|
||||
let info = await video_info(this.video_url, this.cookie)
|
||||
this.url = info.format[info.format.length - 1].url
|
||||
}
|
||||
|
||||
private cleanup(){
|
||||
clearInterval(this.timer)
|
||||
this.request?.unpipe(this.stream)
|
||||
this.request?.destroy()
|
||||
this.request = null
|
||||
@@ -166,6 +180,11 @@ export class Stream {
|
||||
"range" : `bytes=${this.bytes_count}-${end >= this.content_length ? '' : end}`
|
||||
}
|
||||
})
|
||||
if(Number(stream.statusCode) >= 400){
|
||||
this.cleanup()
|
||||
await this.retry()
|
||||
this.loop()
|
||||
}
|
||||
this.request = stream
|
||||
stream.pipe(this.stream, { end : false })
|
||||
|
||||
|
||||
@@ -41,16 +41,6 @@ export async function stream(url : string, cookie? : string): Promise<Stream | L
|
||||
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)
|
||||
}
|
||||
let resp = await request(info.format[info.format.length - 1].url, {
|
||||
headers : {
|
||||
"range" : `bytes=0-1`
|
||||
},
|
||||
}).catch(() => {
|
||||
return 0
|
||||
})
|
||||
if(resp === 0){
|
||||
return await stream(info.video_details.url, cookie)
|
||||
}
|
||||
|
||||
let audioFormat = parseAudioFormats(info.format)
|
||||
let opusFormats = filterFormat(audioFormat, "opus")
|
||||
@@ -69,7 +59,7 @@ export async function stream(url : string, cookie? : string): Promise<Stream | L
|
||||
final.push(info.format[info.format.length - 1])
|
||||
}
|
||||
|
||||
return new Stream(final[0].url, type, info.video_details.durationInSec, Number(final[0].contentLength))
|
||||
return new Stream(final[0].url, type, info.video_details.durationInSec, Number(final[0].contentLength), info.video_details.url, cookie as string)
|
||||
}
|
||||
|
||||
export async function stream_from_info(info : InfoData, cookie? : string): Promise<Stream | LiveStreaming>{
|
||||
@@ -79,17 +69,6 @@ export async function stream_from_info(info : InfoData, cookie? : string): Promi
|
||||
return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url)
|
||||
}
|
||||
|
||||
let resp = await request(info.format[info.format.length - 1].url, {
|
||||
headers : {
|
||||
"range" : `bytes=0-1`
|
||||
},
|
||||
}).catch(() => {
|
||||
return 0
|
||||
})
|
||||
if(resp === 0){
|
||||
return await stream(info.video_details.url, cookie)
|
||||
}
|
||||
|
||||
let audioFormat = parseAudioFormats(info.format)
|
||||
let opusFormats = filterFormat(audioFormat, "opus")
|
||||
|
||||
@@ -107,7 +86,7 @@ export async function stream_from_info(info : InfoData, cookie? : string): Promi
|
||||
final.push(info.format[info.format.length - 1])
|
||||
}
|
||||
|
||||
return new Stream(final[0].url, type, info.video_details.durationInSec, Number(final[0].contentLength))
|
||||
return new Stream(final[0].url, type, info.video_details.durationInSec, Number(final[0].contentLength), info.video_details.url, cookie as string)
|
||||
}
|
||||
|
||||
function filterFormat(formats : any[], codec : string){
|
||||
|
||||
@@ -14,7 +14,8 @@ async function https_getter(req_url : string, options : RequestOpts = {}): Promi
|
||||
let req_options : RequestOptions = {
|
||||
host : s.hostname,
|
||||
path : s.pathname + s.search,
|
||||
headers : (options.headers) ? options.headers : {}
|
||||
headers : (options.headers) ? options.headers : {},
|
||||
method : options.method
|
||||
}
|
||||
|
||||
let req = https.request(req_options, (response) => {
|
||||
@@ -48,9 +49,6 @@ export async function request_stream(url : string, options? : RequestOpts): Prom
|
||||
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
|
||||
res = await https_getter(res.headers.location as string, options)
|
||||
}
|
||||
else if(Number(res.statusCode) > 400){
|
||||
reject(`Got ${res.statusCode} from the request`)
|
||||
}
|
||||
resolve(res)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user