diff --git a/README.md b/README.md index 9be9c71..b68dda5 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,33 @@ const options = { const results = await youtube.search('post malone sunflower', options); ``` +# Validate +### validate( url : `string` ) + *Much faster and easier way to validate url.* +```js +if(validate(url)) // Will return true if url is a YouTube url +``` +# Stream + +### stream(url : `string`) +*This is basic to create a youtube stream from a url.* +```js + let source = await stream() // This will create a stream Class which contains type and stream to be played. + let resource = createAudioResource(source.stream, { + inputType : source.type + }) // This creates resource for playing +``` + +### stream_from_info(info : `infoData`) +*This is basic to create a youtube stream from a info [ from video_info function ].* +```js +let info = await video_info() + let source = await stream_from_info(info) // This will create a stream Class which contains type and stream to be played. + let resource = createAudioResource(source.stream, { + inputType : source.type + }) // This creates resource for playing +``` # Search diff --git a/package-lock.json b/package-lock.json index 9b04145..6cf2b55 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "play-dl", - "version": "0.6.4", + "version": "0.6.9", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "play-dl", - "version": "0.6.4", + "version": "0.6.9", "license": "MIT", "dependencies": { "got": "^11.8.2" diff --git a/package.json b/package.json index dfbb75e..59111b9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "play-dl", - "version": "0.6.4", + "version": "0.6.9", "description": "YouTube, SoundCloud, Spotify streaming for discord.js bots", "main": "dist/index.js", "typings": "dist/index.d.ts", @@ -28,8 +28,8 @@ "url": "https://github.com/play-dl/play-dl/issues" }, "engines": { - "node": ">=16.0.0" - }, + "node": ">=16.0.0" + }, "homepage": "https://github.com/play-dl/play-dl#readme", "files": [ "dist/*" diff --git a/play-dl/YouTube/classes/LiveStream.ts b/play-dl/YouTube/classes/LiveStream.ts index 37f0183..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 Request from 'got/dist/source/core'; import { StreamType } from '../stream'; +import Request from 'got/dist/source/core'; export interface FormatInterface{ url : string; @@ -18,6 +18,7 @@ export class LiveStreaming{ private packet_count : number private timer : NodeJS.Timer | null private segments_urls : string[] + private request : Request | null constructor(dash_url : string, target_interval : number){ this.type = StreamType.Arbitrary this.url = dash_url @@ -25,6 +26,7 @@ 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.interval = target_interval * 1000 || 0 this.stream.on('close', () => { @@ -45,6 +47,8 @@ export class LiveStreaming{ private cleanup(){ clearTimeout(this.timer as NodeJS.Timer) + this.request?.destroy() + this.request = null this.timer = null this.url = '' this.base_url = '' @@ -67,6 +71,7 @@ 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.on('end', () => { this.packet_count++ @@ -88,12 +93,14 @@ export class LiveEnded{ private base_url : string; private packet_count : number private segments_urls : string[] + 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.stream.on('close', () => { this.cleanup() @@ -112,6 +119,8 @@ export class LiveEnded{ } private cleanup(){ + this.request?.destroy() + this.request = null this.url = '' this.base_url = '' this.segments_urls = [] @@ -126,12 +135,17 @@ export class LiveEnded{ await this.dash_getter() if(this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('sq/')[1].split('/')[0]) for await (let segment of this.segments_urls){ + if(this.stream.destroyed){ + this.cleanup() + break + } if(Number(segment.split('sq/')[1].split('/')[0]) !== this.packet_count){ continue } 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.on('end', () => { this.packet_count++ @@ -151,6 +165,7 @@ export class Stream { private per_sec_bytes : number; private duration : number; private timer : NodeJS.Timer | null + private request : Request | null constructor(url : string, type : StreamType, duration : number){ this.url = url this.type = type @@ -158,12 +173,18 @@ export class Stream { this.bytes_count = 0 this.per_sec_bytes = 0 this.timer = null + this.request = null + this.stream.on('close', () => { + this.cleanup() + }) this.duration = duration; (duration > 300) ? this.loop_start() : this.normal_start() } private cleanup(){ clearTimeout(this.timer as NodeJS.Timer) + this.request?.destroy() + this.request = null this.timer = null this.url = '' this.bytes_count = 0 @@ -176,6 +197,7 @@ export class Stream { return } let stream = got.stream(this.url) + this.request = stream stream.pipe(this.stream) } @@ -185,6 +207,7 @@ 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) }) @@ -193,7 +216,6 @@ export class Stream { this.bytes_count += chunk.length this.stream.write(chunk) }) - stream.on('data', () => { if(this.bytes_count > (this.per_sec_bytes * 300)){ stream.destroy() @@ -216,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.on('data', () => { if(absolute_bytes > (this.per_sec_bytes * 300)){ stream.destroy() diff --git a/play-dl/YouTube/stream.ts b/play-dl/YouTube/stream.ts index ae40fdf..de4c448 100644 --- a/play-dl/YouTube/stream.ts +++ b/play-dl/YouTube/stream.ts @@ -1,3 +1,4 @@ +import got from "got/dist/source" import { video_info } from "." import { LiveEnded, LiveStreaming, Stream } from "./classes/LiveStream" @@ -41,6 +42,15 @@ export async function stream(url : string): Promise= 400){ + return await stream(info.video_details.url) + } let audioFormat = parseAudioFormats(info.format) let opusFormats = filterFormat(audioFormat, "opus") @@ -62,13 +72,22 @@ export async function stream(url : string): Promise{ let final: any[] = []; let type : StreamType; if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) { return live_stream(info as InfoData) } + let response = await got(info.format[info.format.length - 1].url, { + headers : { + "range" : `bytes=0-1` + } + }) + if(response.statusCode >= 400){ + return await stream(info.video_details.url) + } + let audioFormat = parseAudioFormats(info.format) let opusFormats = filterFormat(audioFormat, "opus") diff --git a/play-dl/YouTube/utils/extractor.ts b/play-dl/YouTube/utils/extractor.ts index 16efac6..9bc406c 100644 --- a/play-dl/YouTube/utils/extractor.ts +++ b/play-dl/YouTube/utils/extractor.ts @@ -2,11 +2,15 @@ import { url_get } from './request' import { format_decipher, js_tokens } from './cipher' import { Video } from '../classes/Video' import { PlayList } from '../classes/Playlist' -import { parseThumbnail } from './parser'; 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; @@ -30,7 +34,7 @@ export async function video_basic_info(url : string){ durationInSec : vid.lengthSeconds, durationRaw : parseSeconds(vid.lengthSeconds), uploadedDate : microformat.publishDate, - thumbnail : parseThumbnail(vid.thumbnail.thumbnails), + thumbnail : vid.thumbnail.thumbnails[vid.thumbnail.thumbnails.length - 1], channel : { name : vid.author, id : vid.channelId, 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/YouTube/utils/parser.ts b/play-dl/YouTube/utils/parser.ts index 8082f3f..681a9bd 100644 --- a/play-dl/YouTube/utils/parser.ts +++ b/play-dl/YouTube/utils/parser.ts @@ -130,7 +130,7 @@ export function parseVideo(data?: any): Video | void { description: data.videoRenderer.descriptionSnippet && data.videoRenderer.descriptionSnippet.runs[0] ? data.videoRenderer.descriptionSnippet.runs[0].text : "", duration: data.videoRenderer.lengthText ? parseDuration(data.videoRenderer.lengthText.simpleText) : 0, duration_raw: data.videoRenderer.lengthText ? data.videoRenderer.lengthText.simpleText : null, - thumbnail: parseThumbnail(data.videoRenderer.thumbnail.thumbnails), + thumbnail: data.videoRenderer.thumbnail.thumbnails[data.videoRenderer.thumbnail.thumbnails.length - 1], channel: { id: data.videoRenderer.ownerText.runs[0].navigationEndpoint.browseEndpoint.browseId || null, name: data.videoRenderer.ownerText.runs[0].text || null, @@ -149,28 +149,6 @@ export function parseVideo(data?: any): Video | void { return res; } -export function parseThumbnail(thumbnails :thumbnail[]) : thumbnail{ - let parsed : thumbnail = { - width : '', - height : '', - url : '' - } - thumbnails.forEach((thumb) => { - if(thumb.url.indexOf('maxresdefault') !== -1){ - parsed = { - width : thumb.width, - height : thumb.height, - url : thumb.url - } - } - }) - if(parsed.url.length !== 0){ - return parsed - } - else { - return thumbnails[thumbnails.length - 1] - } -} export function parsePlaylist(data?: any): PlayList | void { if (!data.playlistRenderer) return; 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