diff --git a/play-dl/Spotify/index.ts b/play-dl/Spotify/index.ts index bf0927f..aa11869 100644 --- a/play-dl/Spotify/index.ts +++ b/play-dl/Spotify/index.ts @@ -135,7 +135,7 @@ async function SpotifyAuthorize(data : SpotifyDataOptions): Promise{ access_token : resp_json.access_token, refresh_token : resp_json.refresh_token, 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, market : data.market } @@ -164,7 +164,7 @@ export async function RefreshToken(): Promise{ let resp_json = JSON.parse(response) spotifyData.access_token = resp_json.access_token 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 fs.writeFileSync('.data/spotify.data', JSON.stringify(spotifyData, undefined, 4)) return true diff --git a/play-dl/YouTube/classes/LiveStream.ts b/play-dl/YouTube/classes/LiveStream.ts index 93a0874..597a3f8 100644 --- a/play-dl/YouTube/classes/LiveStream.ts +++ b/play-dl/YouTube/classes/LiveStream.ts @@ -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 }) diff --git a/play-dl/YouTube/stream.ts b/play-dl/YouTube/stream.ts index 68dc54a..3626f5b 100644 --- a/play-dl/YouTube/stream.ts +++ b/play-dl/YouTube/stream.ts @@ -41,16 +41,6 @@ export async function stream(url : string, cookie? : string): Promise { - 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{ @@ -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){ diff --git a/play-dl/YouTube/utils/request.ts b/play-dl/YouTube/utils/request.ts index 0dcc12b..39693a4 100644 --- a/play-dl/YouTube/utils/request.ts +++ b/play-dl/YouTube/utils/request.ts @@ -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) }) } \ No newline at end of file