commit
1c78dcf164
@ -157,6 +157,7 @@ export class Stream {
|
|||||||
this.loop();
|
this.loop();
|
||||||
}, 265);
|
}, 265);
|
||||||
this.stream.on('close', () => {
|
this.stream.on('close', () => {
|
||||||
|
this.timer.destroy()
|
||||||
this.cleanup();
|
this.cleanup();
|
||||||
});
|
});
|
||||||
this.loop();
|
this.loop();
|
||||||
@ -177,6 +178,7 @@ export class Stream {
|
|||||||
|
|
||||||
private async loop() {
|
private async loop() {
|
||||||
if (this.stream.destroyed) {
|
if (this.stream.destroyed) {
|
||||||
|
this.timer.destroy()
|
||||||
this.cleanup();
|
this.cleanup();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -196,6 +198,7 @@ export class Stream {
|
|||||||
if (Number(stream.statusCode) >= 400) {
|
if (Number(stream.statusCode) >= 400) {
|
||||||
this.cleanup();
|
this.cleanup();
|
||||||
await this.retry();
|
await this.retry();
|
||||||
|
this.timer.reuse()
|
||||||
this.loop();
|
this.loop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -205,6 +208,7 @@ export class Stream {
|
|||||||
stream.once('error', async (err) => {
|
stream.once('error', async (err) => {
|
||||||
this.cleanup();
|
this.cleanup();
|
||||||
await this.retry();
|
await this.retry();
|
||||||
|
this.timer.reuse()
|
||||||
this.loop();
|
this.loop();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -215,6 +219,7 @@ export class Stream {
|
|||||||
stream.on('end', () => {
|
stream.on('end', () => {
|
||||||
if (end >= this.content_length) {
|
if (end >= this.content_length) {
|
||||||
this.timer.destroy();
|
this.timer.destroy();
|
||||||
|
this.cleanup()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -71,9 +71,10 @@ export async function stream(url: string, options: StreamOptions = {}): Promise<
|
|||||||
if (typeof options.quality !== 'number') options.quality = audioFormat.length - 1;
|
if (typeof options.quality !== 'number') options.quality = audioFormat.length - 1;
|
||||||
else if (options.quality <= 0) options.quality = 0;
|
else if (options.quality <= 0) options.quality = 0;
|
||||||
else if (options.quality >= audioFormat.length) options.quality = audioFormat.length - 1;
|
else if (options.quality >= audioFormat.length) options.quality = audioFormat.length - 1;
|
||||||
final.push(audioFormat[options.quality]);
|
if(audioFormat.length !== 0) final.push(audioFormat[options.quality]);
|
||||||
|
else final.push(info.format[info.format.length - 1])
|
||||||
let type: StreamType =
|
let type: StreamType =
|
||||||
audioFormat[options.quality].codec === 'opus' && audioFormat[options.quality].container === 'webm'
|
final[0].codec === 'opus' && final[0].container === 'webm'
|
||||||
? StreamType.WebmOpus
|
? StreamType.WebmOpus
|
||||||
: StreamType.Arbitrary;
|
: StreamType.Arbitrary;
|
||||||
return new Stream(
|
return new Stream(
|
||||||
@ -109,9 +110,10 @@ export async function stream_from_info(info: InfoData, options: StreamOptions =
|
|||||||
if (typeof options.quality !== 'number') options.quality = audioFormat.length - 1;
|
if (typeof options.quality !== 'number') options.quality = audioFormat.length - 1;
|
||||||
else if (options.quality <= 0) options.quality = 0;
|
else if (options.quality <= 0) options.quality = 0;
|
||||||
else if (options.quality >= audioFormat.length) options.quality = audioFormat.length - 1;
|
else if (options.quality >= audioFormat.length) options.quality = audioFormat.length - 1;
|
||||||
final.push(audioFormat[options.quality]);
|
if(audioFormat.length !== 0) final.push(audioFormat[options.quality]);
|
||||||
|
else final.push(info.format[info.format.length - 1])
|
||||||
let type: StreamType =
|
let type: StreamType =
|
||||||
audioFormat[options.quality].codec === 'opus' && audioFormat[options.quality].container === 'webm'
|
final[0].codec === 'opus' && final[0].container === 'webm'
|
||||||
? StreamType.WebmOpus
|
? StreamType.WebmOpus
|
||||||
: StreamType.Arbitrary;
|
: StreamType.Arbitrary;
|
||||||
return new Stream(
|
return new Stream(
|
||||||
|
|||||||
@ -171,8 +171,10 @@ export async function request(url: string, options: RequestOpts = {}): Promise<s
|
|||||||
if(res.headers && res.headers['set-cookie'] && cookies_added){
|
if(res.headers && res.headers['set-cookie'] && cookies_added){
|
||||||
res.headers['set-cookie'].forEach((x) => {
|
res.headers['set-cookie'].forEach((x) => {
|
||||||
x.split(';').forEach((x) => {
|
x.split(';').forEach((x) => {
|
||||||
const [key, value] = x.split('=');
|
const arr = x.split('=')
|
||||||
if (!value) return;
|
if(arr.length <= 1 ) return;
|
||||||
|
const key = arr.shift()?.trim() as string
|
||||||
|
const value = arr.join('=').trim()
|
||||||
setCookie(key, value);
|
setCookie(key, value);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
@ -204,9 +206,10 @@ export async function request(url: string, options: RequestOpts = {}): Promise<s
|
|||||||
let cookies = res.head.filter((x) => x.toLocaleLowerCase().startsWith('set-cookie: '));
|
let cookies = res.head.filter((x) => x.toLocaleLowerCase().startsWith('set-cookie: '));
|
||||||
cookies.forEach((x) => {
|
cookies.forEach((x) => {
|
||||||
x.toLocaleLowerCase().split('set-cookie: ')[1].split(';').forEach((y) => {
|
x.toLocaleLowerCase().split('set-cookie: ')[1].split(';').forEach((y) => {
|
||||||
let [key, value] = y.split('=');
|
const arr = y.split('=')
|
||||||
if (!value)
|
if(arr.length <= 1 ) return;
|
||||||
return;
|
const key = arr.shift()?.trim() as string
|
||||||
|
const value = arr.join('=').trim()
|
||||||
setCookie(key, value);
|
setCookie(key, value);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -183,10 +183,10 @@ export function authorization(): void {
|
|||||||
console.log('Cookies has been added successfully.');
|
console.log('Cookies has been added successfully.');
|
||||||
let cookie: Object = {};
|
let cookie: Object = {};
|
||||||
cook.split(';').forEach((x) => {
|
cook.split(';').forEach((x) => {
|
||||||
let [ key, value ] = x.split('=')
|
const arr = x.split('=')
|
||||||
if(!value) return;
|
if(arr.length <= 1 ) return;
|
||||||
key = key.trim()
|
const key = arr.shift()?.trim() as string
|
||||||
value = value.trim()
|
const value = arr.join('=').trim()
|
||||||
Object.assign(cookie, { [key] : value })
|
Object.assign(cookie, { [key] : value })
|
||||||
})
|
})
|
||||||
fs.writeFileSync('.data/youtube.data', JSON.stringify({ cookie }, undefined, 4));
|
fs.writeFileSync('.data/youtube.data', JSON.stringify({ cookie }, undefined, 4));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user