Fully https working
This commit is contained in:
parent
ad22f873e5
commit
998f16d1ac
@ -135,7 +135,7 @@ async function SpotifyAuthorize(data : SpotifyDataOptions): Promise<boolean>{
|
|||||||
access_token : resp_json.access_token,
|
access_token : resp_json.access_token,
|
||||||
refresh_token : resp_json.refresh_token,
|
refresh_token : resp_json.refresh_token,
|
||||||
expires_in : Number(resp_json.expires_in),
|
expires_in : Number(resp_json.expires_in),
|
||||||
expiry : Date.now() + (Number(resp_json.expires_in) * 1000),
|
expiry : Date.now() + ((resp_json.expires_in - 1) * 1000),
|
||||||
token_type : resp_json.token_type,
|
token_type : resp_json.token_type,
|
||||||
market : data.market
|
market : data.market
|
||||||
}
|
}
|
||||||
@ -164,7 +164,7 @@ export async function RefreshToken(): Promise<true | false>{
|
|||||||
let resp_json = JSON.parse(response)
|
let resp_json = JSON.parse(response)
|
||||||
spotifyData.access_token = resp_json.access_token
|
spotifyData.access_token = resp_json.access_token
|
||||||
spotifyData.expires_in = Number(resp_json.expires_in)
|
spotifyData.expires_in = Number(resp_json.expires_in)
|
||||||
spotifyData.expiry = Date.now() + (Number(resp_json.expires_in) * 1000)
|
spotifyData.expiry = Date.now() + ((resp_json.expires_in - 1) * 1000)
|
||||||
spotifyData.token_type = resp_json.token_type
|
spotifyData.token_type = resp_json.token_type
|
||||||
fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4))
|
fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4))
|
||||||
return true
|
return true
|
||||||
|
|||||||
@ -116,14 +116,22 @@ export class Stream {
|
|||||||
private bytes_count : number;
|
private bytes_count : number;
|
||||||
private per_sec_bytes : number;
|
private per_sec_bytes : number;
|
||||||
private content_length : number;
|
private content_length : number;
|
||||||
|
private video_url : string;
|
||||||
|
private timer : NodeJS.Timer;
|
||||||
|
private cookie : string;
|
||||||
private data_ended : boolean;
|
private data_ended : boolean;
|
||||||
private playing_count : number;
|
private playing_count : number;
|
||||||
private request : IncomingMessage | null
|
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.url = url
|
||||||
this.type = type
|
this.type = type
|
||||||
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
||||||
this.bytes_count = 0
|
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.per_sec_bytes = Math.ceil(contentLength / duration)
|
||||||
this.content_length = contentLength
|
this.content_length = contentLength
|
||||||
this.request = null
|
this.request = null
|
||||||
@ -146,7 +154,13 @@ export class Stream {
|
|||||||
this.loop()
|
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(){
|
private cleanup(){
|
||||||
|
clearInterval(this.timer)
|
||||||
this.request?.unpipe(this.stream)
|
this.request?.unpipe(this.stream)
|
||||||
this.request?.destroy()
|
this.request?.destroy()
|
||||||
this.request = null
|
this.request = null
|
||||||
@ -166,6 +180,11 @@ export class Stream {
|
|||||||
"range" : `bytes=${this.bytes_count}-${end >= this.content_length ? '' : end}`
|
"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
|
this.request = stream
|
||||||
stream.pipe(this.stream, { end : false })
|
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') {
|
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)
|
||||||
}
|
}
|
||||||
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 audioFormat = parseAudioFormats(info.format)
|
||||||
let opusFormats = filterFormat(audioFormat, "opus")
|
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])
|
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>{
|
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)
|
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 audioFormat = parseAudioFormats(info.format)
|
||||||
let opusFormats = filterFormat(audioFormat, "opus")
|
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])
|
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){
|
function filterFormat(formats : any[], codec : string){
|
||||||
|
|||||||
@ -14,7 +14,8 @@ async function https_getter(req_url : string, options : RequestOpts = {}): Promi
|
|||||||
let req_options : RequestOptions = {
|
let req_options : RequestOptions = {
|
||||||
host : s.hostname,
|
host : s.hostname,
|
||||||
path : s.pathname + s.search,
|
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) => {
|
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){
|
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
|
||||||
res = await https_getter(res.headers.location as string, options)
|
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)
|
resolve(res)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user