From eaf18cfca4849bdf4f41e87bdfe2bd9dda0258d6 Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Tue, 31 Aug 2021 09:09:10 +0530 Subject: [PATCH] Validate URL + All Errors Fixed --- play-dl/YouTube/classes/LiveStream.ts | 37 +++++++++++++-------------- play-dl/YouTube/utils/extractor.ts | 5 ++++ play-dl/YouTube/utils/index.ts | 2 +- play-dl/index.ts | 2 +- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/play-dl/YouTube/classes/LiveStream.ts b/play-dl/YouTube/classes/LiveStream.ts index 2aa97dd..3743254 100644 --- a/play-dl/YouTube/classes/LiveStream.ts +++ b/play-dl/YouTube/classes/LiveStream.ts @@ -1,7 +1,7 @@ import { PassThrough } from 'stream' import got from 'got' import { StreamType } from '../stream'; -import { Socket } from 'net' +import Request from 'got/dist/source/core'; export interface FormatInterface{ url : string; @@ -18,7 +18,7 @@ export class LiveStreaming{ private packet_count : number private timer : NodeJS.Timer | null private segments_urls : string[] - private socket : Socket | null + private request : Request | null constructor(dash_url : string, target_interval : number){ this.type = StreamType.Arbitrary this.url = dash_url @@ -26,8 +26,8 @@ export class LiveStreaming{ this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 }) this.segments_urls = [] this.packet_count = 0 + this.request = null this.timer = null - this.socket = null this.interval = target_interval * 1000 || 0 this.stream.on('close', () => { this.cleanup() @@ -47,8 +47,8 @@ export class LiveStreaming{ private cleanup(){ clearTimeout(this.timer as NodeJS.Timer) - this.socket?.destroy() - this.socket = null + this.request?.destroy() + this.request = null this.timer = null this.url = '' this.base_url = '' @@ -71,8 +71,8 @@ export class LiveStreaming{ await (async () => { return new Promise(async (resolve, reject) => { let stream = got.stream(this.base_url + segment) + this.request = stream stream.on('data', (chunk: any) => this.stream.write(chunk)) - stream.once('data', () => {this.socket = stream.socket as Socket}) stream.on('end', () => { this.packet_count++ resolve('') @@ -93,15 +93,15 @@ export class LiveEnded{ private base_url : string; private packet_count : number private segments_urls : string[] - private socket : Socket | null + private request : Request | null constructor(dash_url : string){ this.type = StreamType.Arbitrary this.url = dash_url this.base_url = '' this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 }) this.segments_urls = [] + this.request = null this.packet_count = 0 - this.socket = null this.stream.on('close', () => { this.cleanup() }) @@ -119,8 +119,8 @@ export class LiveEnded{ } private cleanup(){ - this.socket?.destroy() - this.socket = null + this.request?.destroy() + this.request = null this.url = '' this.base_url = '' this.segments_urls = [] @@ -145,8 +145,8 @@ export class LiveEnded{ await (async () => { return new Promise(async (resolve, reject) => { let stream = got.stream(this.base_url + segment) + this.request = stream stream.on('data', (chunk: any) => this.stream.write(chunk)) - stream.once('data', () => {this.socket = stream.socket as Socket}) stream.on('end', () => { this.packet_count++ resolve('') @@ -165,7 +165,7 @@ export class Stream { private per_sec_bytes : number; private duration : number; private timer : NodeJS.Timer | null - private socket : Socket | null + private request : Request | null constructor(url : string, type : StreamType, duration : number){ this.url = url this.type = type @@ -173,7 +173,7 @@ export class Stream { this.bytes_count = 0 this.per_sec_bytes = 0 this.timer = null - this.socket = null + this.request = null this.stream.on('close', () => { this.cleanup() }) @@ -183,8 +183,8 @@ export class Stream { private cleanup(){ clearTimeout(this.timer as NodeJS.Timer) - this.socket?.destroy() - this.socket = null + this.request?.destroy() + this.request = null this.timer = null this.url = '' this.bytes_count = 0 @@ -197,8 +197,8 @@ export class Stream { return } let stream = got.stream(this.url) + this.request = stream stream.pipe(this.stream) - stream.once('data', () => {this.socket = stream.socket as Socket}) } private loop_start(){ @@ -207,9 +207,9 @@ export class Stream { return } let stream = got.stream(this.url) + this.request = stream stream.once('data', () => { this.per_sec_bytes = Math.ceil((stream.downloadProgress.total as number)/this.duration) - this.socket = stream.socket as Socket }) stream.on('data', (chunk: any) => { @@ -238,13 +238,12 @@ export class Stream { "range" : `bytes=${this.bytes_count}-` } }) - + this.request = stream stream.on('data', (chunk: any) => { absolute_bytes += chunk.length this.bytes_count += chunk.length this.stream.write(chunk) }) - stream.once('data', () => {this.socket = stream.socket as Socket}) stream.on('data', () => { if(absolute_bytes > (this.per_sec_bytes * 300)){ stream.destroy() diff --git a/play-dl/YouTube/utils/extractor.ts b/play-dl/YouTube/utils/extractor.ts index 86c3176..9bc406c 100644 --- a/play-dl/YouTube/utils/extractor.ts +++ b/play-dl/YouTube/utils/extractor.ts @@ -6,6 +6,11 @@ import { PlayList } from '../classes/Playlist' const DEFAULT_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"; const video_pattern = /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/; +export function validate(url : string): boolean{ + if(!url.match(video_pattern)) return false + else return true +} + export async function video_basic_info(url : string){ if(!url.match(video_pattern)) throw new Error('This is not a YouTube URL') let video_id : string; diff --git a/play-dl/YouTube/utils/index.ts b/play-dl/YouTube/utils/index.ts index 52ec51e..c87c6e8 100644 --- a/play-dl/YouTube/utils/index.ts +++ b/play-dl/YouTube/utils/index.ts @@ -1 +1 @@ -export { video_basic_info, video_info, playlist_info } from './extractor' \ No newline at end of file +export { video_basic_info, video_info, playlist_info, validate } from './extractor' \ No newline at end of file diff --git a/play-dl/index.ts b/play-dl/index.ts index 3991b71..03737c2 100644 --- a/play-dl/index.ts +++ b/play-dl/index.ts @@ -1 +1 @@ -export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info } from "./YouTube"; \ No newline at end of file +export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info, validate } from "./YouTube"; \ No newline at end of file