pretty codes and LiveStream issues fixed 100%

This commit is contained in:
killer069 2021-09-28 21:05:43 +05:30
parent 9ab641d1f0
commit 20984ce9e9
5 changed files with 120 additions and 96 deletions

View File

@ -1,6 +1,6 @@
import { PassThrough } from 'stream'; import { PassThrough } from 'stream';
import { IncomingMessage } from 'http'; import { IncomingMessage } from 'http';
import { StreamType } from '../stream'; import { parseAudioFormats, StreamType } from '../stream';
import { request, request_stream } from '../utils/request'; import { request, request_stream } from '../utils/request';
import { video_info } from '..'; import { video_info } from '..';
@ -132,6 +132,7 @@ export class Stream {
private cookie: string; private cookie: string;
private data_ended: boolean; private data_ended: boolean;
private playing_count: number; private playing_count: number;
private quality: number;
private request: IncomingMessage | null; private request: IncomingMessage | null;
constructor( constructor(
url: string, url: string,
@ -139,10 +140,12 @@ export class Stream {
duration: number, duration: number,
contentLength: number, contentLength: number,
video_url: string, video_url: string,
cookie: string cookie: string,
quality: number
) { ) {
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 }); this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 });
this.url = url; this.url = url;
this.quality = quality;
this.type = type; this.type = type;
this.bytes_count = 0; this.bytes_count = 0;
this.video_url = video_url; this.video_url = video_url;
@ -174,8 +177,9 @@ export class Stream {
} }
private async retry() { private async retry() {
const info = await video_info(this.video_url, { cookie : this.cookie }); const info = await video_info(this.video_url, { cookie: this.cookie });
this.url = info.format[info.format.length - 1].url; const audioFormat = parseAudioFormats(info.format);
this.url = audioFormat[this.quality].url;
} }
private cleanup() { private cleanup() {
@ -220,8 +224,8 @@ export class Stream {
this.request = stream; this.request = stream;
stream.pipe(this.stream, { end: false }); stream.pipe(this.stream, { end: false });
stream.once('error', async(err) => { stream.once('error', async (err) => {
this.cleanup() this.cleanup();
await this.retry(); await this.retry();
this.loop(); this.loop();
if (!this.timer) { if (!this.timer) {

View File

@ -1,5 +1,6 @@
import { video_info } from '.'; import { video_info } from '.';
import { LiveStreaming, Stream } from './classes/LiveStream'; import { LiveStreaming, Stream } from './classes/LiveStream';
import { Proxy } from './utils/request';
export enum StreamType { export enum StreamType {
Arbitrary = 'arbitrary', Arbitrary = 'arbitrary',
@ -12,6 +13,7 @@ export enum StreamType {
export interface StreamOptions { export interface StreamOptions {
quality?: number; quality?: number;
cookie?: string; cookie?: string;
proxy?: Proxy[];
} }
export interface InfoData { export interface InfoData {
@ -25,7 +27,7 @@ export interface InfoData {
video_details: any; video_details: any;
} }
function parseAudioFormats(formats: any[]) { export function parseAudioFormats(formats: any[]) {
const result: any[] = []; const result: any[] = [];
formats.forEach((format) => { formats.forEach((format) => {
const type = format.mimeType as string; const type = format.mimeType as string;
@ -41,7 +43,7 @@ function parseAudioFormats(formats: any[]) {
export type YouTubeStream = Stream | LiveStreaming; export type YouTubeStream = Stream | LiveStreaming;
export async function stream(url: string, options: StreamOptions = {}): Promise<YouTubeStream> { export async function stream(url: string, options: StreamOptions = {}): Promise<YouTubeStream> {
const info = await video_info(url, { cookie : options.cookie }); const info = await video_info(url, { cookie: options.cookie, proxy: options.proxy });
const final: any[] = []; const final: any[] = [];
if ( if (
info.LiveStreamData.isLive === true && info.LiveStreamData.isLive === true &&
@ -70,7 +72,8 @@ export async function stream(url: string, options: StreamOptions = {}): Promise<
info.video_details.durationInSec, info.video_details.durationInSec,
Number(final[0].contentLength), Number(final[0].contentLength),
info.video_details.url, info.video_details.url,
options.cookie as string options.cookie as string,
options.quality
); );
} }
@ -103,6 +106,7 @@ export async function stream_from_info(info: InfoData, options: StreamOptions =
info.video_details.durationInSec, info.video_details.durationInSec,
Number(final[0].contentLength), Number(final[0].contentLength),
info.video_details.url, info.video_details.url,
options.cookie as string options.cookie as string,
options.quality
); );
} }

View File

@ -3,14 +3,14 @@ import { format_decipher } from './cipher';
import { YouTubeVideo } from '../classes/Video'; import { YouTubeVideo } from '../classes/Video';
import { YouTubePlayList } from '../classes/Playlist'; import { YouTubePlayList } from '../classes/Playlist';
interface InfoOptions{ interface InfoOptions {
cookie? : string; cookie?: string;
proxy? : Proxy[] proxy?: Proxy[];
} }
interface PlaylistOptions { interface PlaylistOptions {
incomplete? : boolean; incomplete?: boolean;
proxy? : Proxy[] proxy?: Proxy[];
} }
const DEFAULT_API_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'; const DEFAULT_API_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8';
@ -54,7 +54,7 @@ export async function video_basic_info(url: string, options: InfoOptions = {}) {
} else video_id = url; } else video_id = url;
const new_url = `https://www.youtube.com/watch?v=${video_id}`; const new_url = `https://www.youtube.com/watch?v=${video_id}`;
const body = await request(new_url, { const body = await request(new_url, {
proxies : options.proxy ?? [], proxies: options.proxy ?? [],
headers: options.cookie headers: options.cookie
? { ? {
'cookie': options.cookie, 'cookie': options.cookie,
@ -148,7 +148,7 @@ export async function video_info(url: string, options: InfoOptions = {}) {
} }
} }
export async function playlist_info(url: string, options : PlaylistOptions = {}) { export async function playlist_info(url: string, options: PlaylistOptions = {}) {
if (!url || typeof url !== 'string') throw new Error(`Expected playlist url, received ${typeof url}!`); if (!url || typeof url !== 'string') throw new Error(`Expected playlist url, received ${typeof url}!`);
let Playlist_id: string; let Playlist_id: string;
if (url.startsWith('https')) { if (url.startsWith('https')) {
@ -158,7 +158,7 @@ export async function playlist_info(url: string, options : PlaylistOptions = {})
const new_url = `https://www.youtube.com/playlist?list=${Playlist_id}`; const new_url = `https://www.youtube.com/playlist?list=${Playlist_id}`;
const body = await request(new_url, { const body = await request(new_url, {
proxies : options.proxy ?? [], proxies: options.proxy ?? [],
headers: { 'accept-language': 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7' } headers: { 'accept-language': 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7' }
}); });
const response = JSON.parse(body.split('var ytInitialData = ')[1].split(';</script>')[0]); const response = JSON.parse(body.split('var ytInitialData = ')[1].split(';</script>')[0]);

View File

@ -1,29 +1,29 @@
import https, { RequestOptions } from 'https'; import https, { RequestOptions } from 'https';
import tls from 'tls'; import tls from 'tls';
import http , { ClientRequest, IncomingMessage } from 'http'; import http, { ClientRequest, IncomingMessage } from 'http';
import { URL } from 'url'; import { URL } from 'url';
export type Proxy = ProxyOpts | string export type Proxy = ProxyOpts | string;
interface ProxyOpts { interface ProxyOpts {
host : string, host: string;
port : number, port: number;
authentication? : { authentication?: {
username : string; username: string;
password : string; password: string;
} };
} }
interface ProxyOutput { interface ProxyOutput {
statusCode : number; statusCode: number;
head : string; head: string;
body : string; body: string;
} }
interface RequestOpts extends RequestOptions { interface RequestOpts extends RequestOptions {
body?: string; body?: string;
method?: 'GET' | 'POST'; method?: 'GET' | 'POST';
proxies? : Proxy[] proxies?: Proxy[];
} }
function https_getter(req_url: string, options: RequestOpts = {}): Promise<IncomingMessage> { function https_getter(req_url: string, options: RequestOpts = {}): Promise<IncomingMessage> {
@ -46,86 +46,91 @@ function https_getter(req_url: string, options: RequestOpts = {}): Promise<Incom
}); });
} }
function randomIntFromInterval(min : number, max: number) : number { function randomIntFromInterval(min: number, max: number): number {
let x = Math.floor(Math.random() * (max - min + 1) + min) let x = Math.floor(Math.random() * (max - min + 1) + min);
if(x === 0) return 0 if (x === 0) return 0;
else return x - 1 else return x - 1;
} }
async function proxy_getter(req_url : string, req_proxy : Proxy[]): Promise<ProxyOutput>{ async function proxy_getter(req_url: string, req_proxy: Proxy[]): Promise<ProxyOutput> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const proxy : string | ProxyOpts = req_proxy[randomIntFromInterval(0, req_proxy.length)] const proxy: string | ProxyOpts = req_proxy[randomIntFromInterval(0, req_proxy.length)];
const parsed_url = new URL(req_url) const parsed_url = new URL(req_url);
let opts : ProxyOpts let opts: ProxyOpts;
if(typeof proxy === 'string'){ if (typeof proxy === 'string') {
const parsed = new URL(proxy) const parsed = new URL(proxy);
opts = { opts = {
host : parsed.hostname, host: parsed.hostname,
port : Number(parsed.port), port: Number(parsed.port),
authentication : { authentication: {
username : parsed.username, username: parsed.username,
password : parsed.password password: parsed.password
} }
} };
} } else opts = proxy;
else opts = proxy let req: ClientRequest;
let req : ClientRequest if (opts.authentication?.username.length === 0) {
if(opts.authentication?.username.length === 0){
req = http.request({ req = http.request({
host: opts.host, host: opts.host,
port: opts.port, port: opts.port,
method: 'CONNECT', method: 'CONNECT',
path: `${parsed_url.host}:443`, path: `${parsed_url.host}:443`
}); });
} } else {
else {
req = http.request({ req = http.request({
host: opts.host, host: opts.host,
port: opts.port, port: opts.port,
method: 'CONNECT', method: 'CONNECT',
path: `${parsed_url.host}:443`, path: `${parsed_url.host}:443`,
headers : { headers: {
"Proxy-Authorization" : `Basic ${Buffer.from(`${opts.authentication?.username}:${opts.authentication?.password}`).toString('base64')}` 'Proxy-Authorization': `Basic ${Buffer.from(
`${opts.authentication?.username}:${opts.authentication?.password}`
).toString('base64')}`
} }
}); });
} }
req.on('connect', function (res, socket, head) { req.on('connect', function (res, socket, head) {
console.log('Connected') console.log('Connected');
const tlsConnection = tls.connect({ const tlsConnection = tls.connect(
host : parsed_url.hostname, {
port : 443, host: parsed_url.hostname,
socket : socket, port: 443,
rejectUnauthorized : false socket: socket,
}, function() { rejectUnauthorized: false
tlsConnection.write(`GET ${parsed_url.pathname}${parsed_url.search} HTTP/1.1\r\n` },
+ `Host : ${parsed_url.hostname}\r\n` function () {
+ 'Connection: close\r\n' tlsConnection.write(
+ '\r\n') `GET ${parsed_url.pathname}${parsed_url.search} HTTP/1.1\r\n` +
}) `Host : ${parsed_url.hostname}\r\n` +
'Connection: close\r\n' +
'\r\n'
);
}
);
tlsConnection.setEncoding('utf-8') tlsConnection.setEncoding('utf-8');
let data = '' let data = '';
tlsConnection.once('error', (e) => reject(e)) tlsConnection.once('error', (e) => reject(e));
tlsConnection.on('data', (c) => data+=c) tlsConnection.on('data', (c) => (data += c));
tlsConnection.on('end', () => { tlsConnection.on('end', () => {
const y = data.split('\r\n\r\n') const y = data.split('\r\n\r\n');
const head = y.shift() as string const head = y.shift() as string;
resolve({ resolve({
statusCode : Number(head.split('\n')[0].split(' ')[1]), statusCode: Number(head.split('\n')[0].split(' ')[1]),
head : head, head: head,
body : y.join('\n') body: y.join('\n')
}) });
}) });
}) });
req.on('error', (e : Error) => reject(e)) req.on('error', (e: Error) => reject(e));
req.end() req.end();
}) });
} }
export async function request(url: string, options?: RequestOpts): Promise<string> { export async function request(url: string, options?: RequestOpts): Promise<string> {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
if(!options?.proxies){ if (!options?.proxies) {
let data = ''; let data = '';
let res = await https_getter(url, options).catch((err: Error) => err); let res = await https_getter(url, options).catch((err: Error) => err);
if (res instanceof Error) { if (res instanceof Error) {
@ -140,19 +145,18 @@ export async function request(url: string, options?: RequestOpts): Promise<strin
res.setEncoding('utf-8'); res.setEncoding('utf-8');
res.on('data', (c) => (data += c)); res.on('data', (c) => (data += c));
res.on('end', () => resolve(data)); res.on('end', () => resolve(data));
} } else {
else { let res = await proxy_getter(url, options.proxies).catch((e: Error) => e);
let res = await proxy_getter(url, options.proxies).catch((e : Error) => e)
if (res instanceof Error) { if (res instanceof Error) {
reject(res); reject(res);
return; return;
} }
if(res.statusCode >= 300 && res.statusCode < 400){ if (res.statusCode >= 300 && res.statusCode < 400) {
res = await proxy_getter(res.head.split('Location: ')[1].split('\n')[0], options.proxies) res = await proxy_getter(res.head.split('Location: ')[1].split('\n')[0], options.proxies);
} else if (res.statusCode > 400){ } else if (res.statusCode > 400) {
reject(new Error(`GOT ${res.statusCode} from proxy request`)) reject(new Error(`GOT ${res.statusCode} from proxy request`));
} }
resolve(res.body) resolve(res.body);
} }
}); });
} }

View File

@ -13,7 +13,16 @@ interface SearchOptions {
import readline from 'readline'; import readline from 'readline';
import fs from 'fs'; import fs from 'fs';
import { sp_validate, yt_validate, so_validate, YouTubeStream, SoundCloudStream, YouTube, SoundCloud, Spotify } from '.'; import {
sp_validate,
yt_validate,
so_validate,
YouTubeStream,
SoundCloudStream,
YouTube,
SoundCloud,
Spotify
} from '.';
import { SpotifyAuthorize, sp_search } from './Spotify'; import { SpotifyAuthorize, sp_search } from './Spotify';
import { check_id, so_search, stream as so_stream, stream_from_info as so_stream_info } from './SoundCloud'; import { check_id, so_search, stream as so_stream, stream_from_info as so_stream_info } from './SoundCloud';
import { InfoData, stream as yt_stream, StreamOptions, stream_from_info as yt_stream_info } from './YouTube/stream'; import { InfoData, stream as yt_stream, StreamOptions, stream_from_info as yt_stream_info } from './YouTube/stream';
@ -39,17 +48,20 @@ export async function stream(url: string, options: StreamOptions = {}): Promise<
* @param options contains limit and source to choose. * @param options contains limit and source to choose.
* @returns * @returns
*/ */
export async function search(query: string, options: SearchOptions = {}): Promise<YouTube[] | Spotify[] | SoundCloud[]> { export async function search(
query: string,
options: SearchOptions = {}
): Promise<YouTube[] | Spotify[] | SoundCloud[]> {
if (!options.source) options.source = { youtube: 'video' }; if (!options.source) options.source = { youtube: 'video' };
if (options.source.youtube) return await yt_search(query, { limit: options.limit, type: options.source.youtube }); if (options.source.youtube) return await yt_search(query, { limit: options.limit, type: options.source.youtube });
else if (options.source.spotify) return await sp_search(query, options.source.spotify, options.limit); else if (options.source.spotify) return await sp_search(query, options.source.spotify, options.limit);
else if (options.source.soundcloud) return await so_search(query, options.source.soundcloud, options.limit); else if (options.source.soundcloud) return await so_search(query, options.source.soundcloud, options.limit);
else throw new Error('Not possible to reach Here LOL. Easter Egg of play-dl if someone get this.') else throw new Error('Not possible to reach Here LOL. Easter Egg of play-dl if someone get this.');
} }
/** /**
* Command to be used * Command to be used
* @param info * @param info
* @param options * @param options
* @returns * @returns