Merge pull request #29 from play-dl/developer

Feat 0.6.9
This commit is contained in:
Killer069 2021-08-31 09:28:38 +05:30 committed by GitHub
commit ee4558ae1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 85 additions and 37 deletions

View File

@ -32,7 +32,33 @@ const options = {
const results = await youtube.search('post malone sunflower', 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(<url>) // 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(<url>)
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 # Search

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "play-dl", "name": "play-dl",
"version": "0.6.4", "version": "0.6.9",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "play-dl", "name": "play-dl",
"version": "0.6.4", "version": "0.6.9",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"got": "^11.8.2" "got": "^11.8.2"

View File

@ -1,6 +1,6 @@
{ {
"name": "play-dl", "name": "play-dl",
"version": "0.6.4", "version": "0.6.9",
"description": "YouTube, SoundCloud, Spotify streaming for discord.js bots", "description": "YouTube, SoundCloud, Spotify streaming for discord.js bots",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",
@ -28,8 +28,8 @@
"url": "https://github.com/play-dl/play-dl/issues" "url": "https://github.com/play-dl/play-dl/issues"
}, },
"engines": { "engines": {
"node": ">=16.0.0" "node": ">=16.0.0"
}, },
"homepage": "https://github.com/play-dl/play-dl#readme", "homepage": "https://github.com/play-dl/play-dl#readme",
"files": [ "files": [
"dist/*" "dist/*"

View File

@ -1,7 +1,7 @@
import { PassThrough } from 'stream' import { PassThrough } from 'stream'
import got from 'got' import got from 'got'
import Request from 'got/dist/source/core';
import { StreamType } from '../stream'; import { StreamType } from '../stream';
import Request from 'got/dist/source/core';
export interface FormatInterface{ export interface FormatInterface{
url : string; url : string;
@ -18,6 +18,7 @@ export class LiveStreaming{
private packet_count : number private packet_count : number
private timer : NodeJS.Timer | null private timer : NodeJS.Timer | null
private segments_urls : string[] private segments_urls : string[]
private request : Request | null
constructor(dash_url : string, target_interval : number){ constructor(dash_url : string, target_interval : number){
this.type = StreamType.Arbitrary this.type = StreamType.Arbitrary
this.url = dash_url this.url = dash_url
@ -25,6 +26,7 @@ export class LiveStreaming{
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 }) this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
this.segments_urls = [] this.segments_urls = []
this.packet_count = 0 this.packet_count = 0
this.request = null
this.timer = null this.timer = null
this.interval = target_interval * 1000 || 0 this.interval = target_interval * 1000 || 0
this.stream.on('close', () => { this.stream.on('close', () => {
@ -45,6 +47,8 @@ export class LiveStreaming{
private cleanup(){ private cleanup(){
clearTimeout(this.timer as NodeJS.Timer) clearTimeout(this.timer as NodeJS.Timer)
this.request?.destroy()
this.request = null
this.timer = null this.timer = null
this.url = '' this.url = ''
this.base_url = '' this.base_url = ''
@ -67,6 +71,7 @@ export class LiveStreaming{
await (async () => { await (async () => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let stream = got.stream(this.base_url + segment) let stream = got.stream(this.base_url + segment)
this.request = stream
stream.on('data', (chunk: any) => this.stream.write(chunk)) stream.on('data', (chunk: any) => this.stream.write(chunk))
stream.on('end', () => { stream.on('end', () => {
this.packet_count++ this.packet_count++
@ -88,12 +93,14 @@ export class LiveEnded{
private base_url : string; private base_url : string;
private packet_count : number private packet_count : number
private segments_urls : string[] private segments_urls : string[]
private request : Request | null
constructor(dash_url : string){ constructor(dash_url : string){
this.type = StreamType.Arbitrary this.type = StreamType.Arbitrary
this.url = dash_url this.url = dash_url
this.base_url = '' this.base_url = ''
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 }) this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
this.segments_urls = [] this.segments_urls = []
this.request = null
this.packet_count = 0 this.packet_count = 0
this.stream.on('close', () => { this.stream.on('close', () => {
this.cleanup() this.cleanup()
@ -112,6 +119,8 @@ export class LiveEnded{
} }
private cleanup(){ private cleanup(){
this.request?.destroy()
this.request = null
this.url = '' this.url = ''
this.base_url = '' this.base_url = ''
this.segments_urls = [] this.segments_urls = []
@ -126,12 +135,17 @@ export class LiveEnded{
await this.dash_getter() await this.dash_getter()
if(this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('sq/')[1].split('/')[0]) 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){ 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){ if(Number(segment.split('sq/')[1].split('/')[0]) !== this.packet_count){
continue continue
} }
await (async () => { await (async () => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let stream = got.stream(this.base_url + segment) let stream = got.stream(this.base_url + segment)
this.request = stream
stream.on('data', (chunk: any) => this.stream.write(chunk)) stream.on('data', (chunk: any) => this.stream.write(chunk))
stream.on('end', () => { stream.on('end', () => {
this.packet_count++ this.packet_count++
@ -151,6 +165,7 @@ export class Stream {
private per_sec_bytes : number; private per_sec_bytes : number;
private duration : number; private duration : number;
private timer : NodeJS.Timer | null private timer : NodeJS.Timer | null
private request : Request | null
constructor(url : string, type : StreamType, duration : number){ constructor(url : string, type : StreamType, duration : number){
this.url = url this.url = url
this.type = type this.type = type
@ -158,12 +173,18 @@ export class Stream {
this.bytes_count = 0 this.bytes_count = 0
this.per_sec_bytes = 0 this.per_sec_bytes = 0
this.timer = null this.timer = null
this.request = null
this.stream.on('close', () => {
this.cleanup()
})
this.duration = duration; this.duration = duration;
(duration > 300) ? this.loop_start() : this.normal_start() (duration > 300) ? this.loop_start() : this.normal_start()
} }
private cleanup(){ private cleanup(){
clearTimeout(this.timer as NodeJS.Timer) clearTimeout(this.timer as NodeJS.Timer)
this.request?.destroy()
this.request = null
this.timer = null this.timer = null
this.url = '' this.url = ''
this.bytes_count = 0 this.bytes_count = 0
@ -176,6 +197,7 @@ export class Stream {
return return
} }
let stream = got.stream(this.url) let stream = got.stream(this.url)
this.request = stream
stream.pipe(this.stream) stream.pipe(this.stream)
} }
@ -185,6 +207,7 @@ export class Stream {
return return
} }
let stream = got.stream(this.url) let stream = got.stream(this.url)
this.request = stream
stream.once('data', () => { stream.once('data', () => {
this.per_sec_bytes = Math.ceil((stream.downloadProgress.total as number)/this.duration) 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.bytes_count += chunk.length
this.stream.write(chunk) this.stream.write(chunk)
}) })
stream.on('data', () => { stream.on('data', () => {
if(this.bytes_count > (this.per_sec_bytes * 300)){ if(this.bytes_count > (this.per_sec_bytes * 300)){
stream.destroy() stream.destroy()
@ -216,13 +238,12 @@ export class Stream {
"range" : `bytes=${this.bytes_count}-` "range" : `bytes=${this.bytes_count}-`
} }
}) })
this.request = stream
stream.on('data', (chunk: any) => { stream.on('data', (chunk: any) => {
absolute_bytes += chunk.length absolute_bytes += chunk.length
this.bytes_count += chunk.length this.bytes_count += chunk.length
this.stream.write(chunk) this.stream.write(chunk)
}) })
stream.on('data', () => { stream.on('data', () => {
if(absolute_bytes > (this.per_sec_bytes * 300)){ if(absolute_bytes > (this.per_sec_bytes * 300)){
stream.destroy() stream.destroy()

View File

@ -1,3 +1,4 @@
import got from "got/dist/source"
import { video_info } from "." import { video_info } from "."
import { LiveEnded, LiveStreaming, Stream } from "./classes/LiveStream" import { LiveEnded, LiveStreaming, Stream } from "./classes/LiveStream"
@ -41,6 +42,15 @@ export async function stream(url : string): Promise<Stream | LiveStreaming | Liv
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) { if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) {
return live_stream(info as InfoData) 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 audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus") let opusFormats = filterFormat(audioFormat, "opus")
@ -62,13 +72,22 @@ export async function stream(url : string): Promise<Stream | LiveStreaming | Liv
return new Stream(final[0].url, type, info.video_details.durationInSec) return new Stream(final[0].url, type, info.video_details.durationInSec)
} }
export function stream_from_info(info : InfoData): Stream | LiveStreaming | LiveEnded{ export async function stream_from_info(info : InfoData): Promise<Stream | LiveStreaming | LiveEnded>{
let final: any[] = []; let final: any[] = [];
let type : StreamType; let type : StreamType;
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) { if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) {
return live_stream(info as InfoData) 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 audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus") let opusFormats = filterFormat(audioFormat, "opus")

View File

@ -2,11 +2,15 @@ import { url_get } from './request'
import { format_decipher, js_tokens } from './cipher' import { format_decipher, js_tokens } from './cipher'
import { Video } from '../classes/Video' import { Video } from '../classes/Video'
import { PlayList } from '../classes/Playlist' import { PlayList } from '../classes/Playlist'
import { parseThumbnail } from './parser';
const DEFAULT_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"; const DEFAULT_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8";
const video_pattern = /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/; 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){ export async function video_basic_info(url : string){
if(!url.match(video_pattern)) throw new Error('This is not a YouTube URL') if(!url.match(video_pattern)) throw new Error('This is not a YouTube URL')
let video_id : string; let video_id : string;
@ -30,7 +34,7 @@ export async function video_basic_info(url : string){
durationInSec : vid.lengthSeconds, durationInSec : vid.lengthSeconds,
durationRaw : parseSeconds(vid.lengthSeconds), durationRaw : parseSeconds(vid.lengthSeconds),
uploadedDate : microformat.publishDate, uploadedDate : microformat.publishDate,
thumbnail : parseThumbnail(vid.thumbnail.thumbnails), thumbnail : vid.thumbnail.thumbnails[vid.thumbnail.thumbnails.length - 1],
channel : { channel : {
name : vid.author, name : vid.author,
id : vid.channelId, id : vid.channelId,

View File

@ -1 +1 @@
export { video_basic_info, video_info, playlist_info } from './extractor' export { video_basic_info, video_info, playlist_info, validate } from './extractor'

View File

@ -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 : "", 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: data.videoRenderer.lengthText ? parseDuration(data.videoRenderer.lengthText.simpleText) : 0,
duration_raw: data.videoRenderer.lengthText ? data.videoRenderer.lengthText.simpleText : null, 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: { channel: {
id: data.videoRenderer.ownerText.runs[0].navigationEndpoint.browseEndpoint.browseId || null, id: data.videoRenderer.ownerText.runs[0].navigationEndpoint.browseEndpoint.browseId || null,
name: data.videoRenderer.ownerText.runs[0].text || null, name: data.videoRenderer.ownerText.runs[0].text || null,
@ -149,28 +149,6 @@ export function parseVideo(data?: any): Video | void {
return res; 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 { export function parsePlaylist(data?: any): PlayList | void {
if (!data.playlistRenderer) return; if (!data.playlistRenderer) return;

View File

@ -1 +1 @@
export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info } from "./YouTube"; export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info, validate } from "./YouTube";