search validation added

This commit is contained in:
killer069 2021-10-09 16:18:05 +05:30
parent 1ba762d10c
commit 6698a87983
11 changed files with 199 additions and 187 deletions

View File

@ -1,110 +1,111 @@
import tls , { TLSSocket } from "tls";
import { URL } from "url";
import { Timer } from "../YouTube/classes/LiveStream";
import tls, { TLSSocket } from 'tls';
import { URL } from 'url';
import { Timer } from '../YouTube/classes/LiveStream';
interface ResponseOptions extends tls.ConnectionOptions{
interface ResponseOptions extends tls.ConnectionOptions {
body?: string;
method: 'GET' | 'POST';
cookies? : boolean
headers? : Object;
timeout? : number
cookies?: boolean;
headers?: Object;
timeout?: number;
}
export class Response {
parsed_url : URL
statusCode : number;
rawHeaders : string;
headers : Object;
body : string;
socket : TLSSocket;
sentHeaders : string;
sentBody : string;
private options : ResponseOptions;
private timer : Timer | null
constructor(req_url : string, options : ResponseOptions){
this.parsed_url = new URL(req_url)
this.sentHeaders = ''
this.statusCode = 0
this.sentBody = ""
this.rawHeaders = ''
this.body = ''
this.headers = {}
this.timer = null
this.options = options
this.socket = tls.connect({
host : this.parsed_url.hostname,
port : Number(this.parsed_url.port) || 443,
socket : options.socket,
rejectUnauthorized : false
}, () => this.onConnect())
if(options.headers){
for(const [ key, value ] of Object.entries(options.headers)){
this.sentHeaders += `${key}: ${value}\r\n`
parsed_url: URL;
statusCode: number;
rawHeaders: string;
headers: Object;
body: string;
socket: TLSSocket;
sentHeaders: string;
sentBody: string;
private options: ResponseOptions;
private timer: Timer | null;
constructor(req_url: string, options: ResponseOptions) {
this.parsed_url = new URL(req_url);
this.sentHeaders = '';
this.statusCode = 0;
this.sentBody = '';
this.rawHeaders = '';
this.body = '';
this.headers = {};
this.timer = null;
this.options = options;
this.socket = tls.connect(
{
host: this.parsed_url.hostname,
port: Number(this.parsed_url.port) || 443,
socket: options.socket,
rejectUnauthorized: false
},
() => this.onConnect()
);
if (options.headers) {
for (const [key, value] of Object.entries(options.headers)) {
this.sentHeaders += `${key}: ${value}\r\n`;
}
}
if(options.body) this.sentBody = options.body
if (options.body) this.sentBody = options.body;
}
private onConnect(){
private onConnect() {
this.socket.write(
`${this.options.method} ${this.parsed_url.pathname}${this.parsed_url.search} HTTP/1.1\r\n` +
`Host : ${this.parsed_url.hostname}\r\n` +
this.sentHeaders +
`Connection: close\r\n` +
`\r\n` +
this.sentBody
)
`${this.options.method} ${this.parsed_url.pathname}${this.parsed_url.search} HTTP/1.1\r\n` +
`Host : ${this.parsed_url.hostname}\r\n` +
this.sentHeaders +
`Connection: close\r\n` +
`\r\n` +
this.sentBody
);
}
private parseHeaders(){
const head_arr = this.rawHeaders.split('\r\n')
this.statusCode = Number(head_arr.shift()?.split(' ')[1]) ?? -1
for(const head of head_arr){
let [ key, value ] = head.split(': ')
if(!value) break;
key = key.trim().toLowerCase()
value = value.trim()
if(Object.keys(this.headers).includes(key)){
let val = (this.headers as any)[key]
if(typeof val === 'string') val = [val]
Object.assign(this.headers, { [key]: [...val, value] })
}
else Object.assign(this.headers, { [key] : value })
private parseHeaders() {
const head_arr = this.rawHeaders.split('\r\n');
this.statusCode = Number(head_arr.shift()?.split(' ')[1]) ?? -1;
for (const head of head_arr) {
let [key, value] = head.split(': ');
if (!value) break;
key = key.trim().toLowerCase();
value = value.trim();
if (Object.keys(this.headers).includes(key)) {
let val = (this.headers as any)[key];
if (typeof val === 'string') val = [val];
Object.assign(this.headers, { [key]: [...val, value] });
} else Object.assign(this.headers, { [key]: value });
}
}
stream(): Promise<TLSSocket>{
stream(): Promise<TLSSocket> {
return new Promise((resolve, reject) => {
this.timer = new Timer(() => this.socket.end(), this.options.timeout || 1)
this.socket.once('error', (err) => reject(err))
this.timer = new Timer(() => this.socket.end(), this.options.timeout || 1);
this.socket.once('error', (err) => reject(err));
this.socket.once('data', (chunk) => {
this.rawHeaders = chunk.toString('utf-8')
this.parseHeaders()
resolve(this.socket)
})
this.socket.on('data', () => this.timer?.reuse())
this.socket.once('end', () => this.timer?.destroy())
})
this.rawHeaders = chunk.toString('utf-8');
this.parseHeaders();
resolve(this.socket);
});
this.socket.on('data', () => this.timer?.reuse());
this.socket.once('end', () => this.timer?.destroy());
});
}
fetch(): Promise<Response>{
fetch(): Promise<Response> {
return new Promise((resolve, reject) => {
this.socket.setEncoding('utf-8');
this.socket.once('error', (err) => reject(err))
this.socket.once('error', (err) => reject(err));
this.socket.on('data', (chunk: string) => {
if(this.rawHeaders.length === 0){
this.rawHeaders = chunk
this.parseHeaders()
if (this.rawHeaders.length === 0) {
this.rawHeaders = chunk;
this.parseHeaders();
} else {
const arr = chunk.split('\r\n');
if (arr.length > 1 && arr[0].length < 5) arr.shift();
this.body += arr.join('');
}
else {
const arr = chunk.split('\r\n')
if(arr.length > 1 && arr[0].length < 5) arr.shift()
this.body += arr.join('')
}
})
});
this.socket.on('end', () => {
resolve(this)
})
})
resolve(this);
});
});
}
}
}

View File

@ -1,5 +1,4 @@
import { Response } from "./classes";
import { Response } from './classes';
export type Proxy = ProxyOpts | string;
@ -16,38 +15,38 @@ interface RequestOptions {
body?: string;
method: 'GET' | 'POST';
proxies?: Proxy[];
cookies? : boolean
headers? : Object;
timeout? : number
cookies?: boolean;
headers?: Object;
timeout?: number;
}
interface StreamGetterOptions{
interface StreamGetterOptions {
method: 'GET' | 'POST';
cookies? : boolean
headers : Object;
cookies?: boolean;
headers: Object;
}
export function request_stream(req_url : string, options : RequestOptions = {method : "GET"}): Promise<Response>{
return new Promise(async(resolve, reject) => {
let res = new Response(req_url, options)
await res.stream()
export function request_stream(req_url: string, options: RequestOptions = { method: 'GET' }): Promise<Response> {
return new Promise(async (resolve, reject) => {
let res = new Response(req_url, options);
await res.stream();
if (res.statusCode >= 300 && res.statusCode < 400) {
res = await request_stream((res.headers as any).location, options);
await res.stream()
await res.stream();
}
resolve(res)
})
resolve(res);
});
}
export function request(req_url : string, options : RequestOptions = {method : "GET"}): Promise<Response>{
return new Promise(async(resolve, reject) => {
let res = new Response(req_url, options)
await res.fetch()
export function request(req_url: string, options: RequestOptions = { method: 'GET' }): Promise<Response> {
return new Promise(async (resolve, reject) => {
let res = new Response(req_url, options);
await res.fetch();
if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) {
res = await request((res.headers as any).location, options);
} else if (Number(res.statusCode) > 400) {
reject(new Error(`Got ${res.statusCode} from the request`));
}
resolve(res)
})
}
resolve(res);
});
}

View File

@ -260,7 +260,7 @@ export class Stream {
}
private async loop() {
if (this.stream.destroyed ||this.time.length === 0 || this.segment_urls.length === 0) {
if (this.stream.destroyed || this.time.length === 0 || this.segment_urls.length === 0) {
this.cleanup();
return;
}
@ -273,7 +273,7 @@ export class Stream {
return;
}
this.request = stream
this.request = stream;
stream.pipe(this.stream, { end: false });
stream.on('end', () => {
if (this.downloaded_time >= 300) return;

View File

@ -126,8 +126,8 @@ export async function check_id(id: string): Promise<boolean> {
* @param url soundcloud url
* @returns "false" | 'track' | 'playlist'
*/
export async function so_validate(url: string): Promise<false | 'track' | 'playlist'> {
if (!url.match(pattern)) return false;
export async function so_validate(url: string): Promise<false | 'track' | 'playlist' | 'search'> {
if (!url.match(pattern)) return 'search';
const data = await request(
`https://api-v2.soundcloud.com/resolve?url=${url}&client_id=${soundData.client_id}`
).catch((err: Error) => err);

View File

@ -74,8 +74,8 @@ export async function spotify(url: string): Promise<Spotify> {
* @param url url for validation
* @returns type of url or false.
*/
export function sp_validate(url: string): 'track' | 'playlist' | 'album' | false {
if (!url.match(pattern)) return false;
export function sp_validate(url: string): 'track' | 'playlist' | 'album' | 'search' | false {
if (!url.match(pattern)) return 'search';
if (url.indexOf('track/') !== -1) {
return 'track';
} else if (url.indexOf('album/') !== -1) {

View File

@ -112,7 +112,7 @@ export class LiveStreaming {
});
});
}
this.timer.reuse();
}
@ -157,7 +157,7 @@ export class Stream {
this.loop();
}, 265);
this.stream.on('close', () => {
this.timer.destroy()
this.timer.destroy();
this.cleanup();
});
this.loop();
@ -178,7 +178,7 @@ export class Stream {
private async loop() {
if (this.stream.destroyed) {
this.timer.destroy()
this.timer.destroy();
this.cleanup();
return;
}
@ -198,7 +198,7 @@ export class Stream {
if (Number(stream.statusCode) >= 400) {
this.cleanup();
await this.retry();
this.timer.reuse()
this.timer.reuse();
this.loop();
return;
}
@ -208,7 +208,7 @@ export class Stream {
stream.once('error', async (err) => {
this.cleanup();
await this.retry();
this.timer.reuse()
this.timer.reuse();
this.loop();
});
@ -219,7 +219,7 @@ export class Stream {
stream.on('end', () => {
if (end >= this.content_length) {
this.timer.destroy();
this.cleanup()
this.cleanup();
}
});
}

View File

@ -71,12 +71,10 @@ export async function stream(url: string, options: StreamOptions = {}): Promise<
if (typeof options.quality !== 'number') options.quality = audioFormat.length - 1;
else if (options.quality <= 0) options.quality = 0;
else if (options.quality >= audioFormat.length) options.quality = audioFormat.length - 1;
if(audioFormat.length !== 0) final.push(audioFormat[options.quality]);
else final.push(info.format[info.format.length - 1])
if (audioFormat.length !== 0) final.push(audioFormat[options.quality]);
else final.push(info.format[info.format.length - 1]);
let type: StreamType =
final[0].codec === 'opus' && final[0].container === 'webm'
? StreamType.WebmOpus
: StreamType.Arbitrary;
final[0].codec === 'opus' && final[0].container === 'webm' ? StreamType.WebmOpus : StreamType.Arbitrary;
return new Stream(
final[0].url,
type,
@ -110,12 +108,10 @@ export async function stream_from_info(info: InfoData, options: StreamOptions =
if (typeof options.quality !== 'number') options.quality = audioFormat.length - 1;
else if (options.quality <= 0) options.quality = 0;
else if (options.quality >= audioFormat.length) options.quality = audioFormat.length - 1;
if(audioFormat.length !== 0) final.push(audioFormat[options.quality]);
else final.push(info.format[info.format.length - 1])
if (audioFormat.length !== 0) final.push(audioFormat[options.quality]);
else final.push(info.format[info.format.length - 1]);
let type: StreamType =
final[0].codec === 'opus' && final[0].container === 'webm'
? StreamType.WebmOpus
: StreamType.Arbitrary;
final[0].codec === 'opus' && final[0].container === 'webm' ? StreamType.WebmOpus : StreamType.Arbitrary;
return new Stream(
final[0].url,
type,

View File

@ -10,22 +10,22 @@ interface youtubeDataOptions {
}
export function getCookies(): undefined | string {
let result = ''
if(!youtubeData?.cookie) return undefined
for (const [ key, value ] of Object.entries(youtubeData.cookie)){
result+= `${key}=${value};`
let result = '';
if (!youtubeData?.cookie) return undefined;
for (const [key, value] of Object.entries(youtubeData.cookie)) {
result += `${key}=${value};`;
}
return result;
}
export function setCookie(key: string, value: string): boolean {
if (!youtubeData?.cookie) return false;
key = key.trim()
value = value.trim()
Object.assign(youtubeData.cookie, { [key] : value })
return true
key = key.trim();
value = value.trim();
Object.assign(youtubeData.cookie, { [key]: value });
return true;
}
export function uploadCookie() {
if(youtubeData) fs.writeFileSync('.data/youtube.data', JSON.stringify(youtubeData, undefined, 4));
if (youtubeData) fs.writeFileSync('.data/youtube.data', JSON.stringify(youtubeData, undefined, 4));
}

View File

@ -24,23 +24,26 @@ const playlist_pattern =
* @param url Url for validation
* @returns type of url or false.
*/
export function yt_validate(url: string): 'playlist' | 'video' | false {
export function yt_validate(url: string): 'playlist' | 'video' | 'search' | false {
if (url.indexOf('list=') === -1) {
if (url.startsWith('https')) {
if (url.match(video_pattern)) return 'video';
if (url.match(video_pattern)) {
const id = url.split('v=')[1].split('&')[0]
if(id.match(video_id_pattern)) return "video"
else return false
}
else return false;
} else {
if (url.match(video_id_pattern)) return 'video';
else if (url.match(playlist_id_pattern)) return 'playlist';
else return false;
else return 'search';
}
} else {
if (!url.match(playlist_pattern)) return false;
const Playlist_id = url.split('list=')[1].split('&')[0];
if (Playlist_id.length !== 34 || !Playlist_id.startsWith('PL')) {
return false;
}
else return 'playlist';
} else return 'playlist';
}
}
/**
@ -49,7 +52,8 @@ export function yt_validate(url: string): 'playlist' | 'video' | false {
* @returns ID of video or playlist.
*/
export function extractID(url: string): string {
if (!yt_validate(url)) throw new Error('This is not a YouTube url or videoId or PlaylistID');
const check = yt_validate(url);
if (!check || check === 'search') throw new Error('This is not a YouTube url or videoId or PlaylistID');
if (url.startsWith('https')) {
if (url.indexOf('list=') === -1) {
let video_id: string;
@ -69,16 +73,13 @@ export function extractID(url: string): string {
* @returns Data containing video_details, LiveStreamData and formats of video url.
*/
export async function video_basic_info(url: string, options: InfoOptions = {}) {
let video_id: string;
if (url.startsWith('https')) {
if (yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL');
video_id = extractID(url);
} else video_id = url;
if (yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL');
let video_id: string = extractID(url);
const new_url = `https://www.youtube.com/watch?v=${video_id}&has_verified=1`;
const body = await request(new_url, {
proxies: options.proxy ?? undefined,
headers: { 'accept-language': 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7' },
cookies : true
cookies: true
});
const player_response = JSON.parse(body.split('var ytInitialPlayerResponse = ')[1].split('}};')[0] + '}}');
const initial_response = JSON.parse(body.split('var ytInitialData = ')[1].split('}};')[0] + '}}');

View File

@ -27,7 +27,7 @@ interface RequestOpts extends RequestOptions {
body?: string;
method?: 'GET' | 'POST';
proxies?: Proxy[];
cookies? : boolean
cookies?: boolean;
}
/**
* Main module that play-dl uses for making a https request
@ -155,12 +155,12 @@ export async function request(url: string, options: RequestOpts = {}): Promise<s
return new Promise(async (resolve, reject) => {
if (!options?.proxies || options.proxies.length === 0) {
let data = '';
let cookies_added = false
if(options.cookies){
let cook = getCookies()
let cookies_added = false;
if (options.cookies) {
let cook = getCookies();
if (typeof cook === 'string' && options.headers) {
Object.assign(options.headers, { cookie : cook });
cookies_added = true
Object.assign(options.headers, { cookie: cook });
cookies_added = true;
}
}
let res = await https_getter(url, options).catch((err: Error) => err);
@ -168,17 +168,17 @@ export async function request(url: string, options: RequestOpts = {}): Promise<s
reject(res);
return;
}
if(res.headers && res.headers['set-cookie'] && cookies_added){
if (res.headers && res.headers['set-cookie'] && cookies_added) {
res.headers['set-cookie'].forEach((x) => {
x.split(';').forEach((x) => {
const arr = x.split('=')
if(arr.length <= 1 ) return;
const key = arr.shift()?.trim() as string
const value = arr.join('=').trim()
const arr = x.split('=');
if (arr.length <= 1) return;
const key = arr.shift()?.trim() as string;
const value = arr.join('=').trim();
setCookie(key, value);
});
})
uploadCookie()
});
uploadCookie();
}
if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) {
res = await https_getter(res.headers.location as string, options);
@ -189,12 +189,12 @@ export async function request(url: string, options: RequestOpts = {}): Promise<s
res.on('data', (c) => (data += c));
res.on('end', () => resolve(data));
} else {
let cookies_added = false
if(options.cookies){
let cook = getCookies()
let cookies_added = false;
if (options.cookies) {
let cook = getCookies();
if (typeof cook === 'string' && options.headers) {
Object.assign(options.headers, { cookie : cook });
cookies_added = true
Object.assign(options.headers, { cookie: cook });
cookies_added = true;
}
}
let res = await proxy_getter(url, options.proxies).catch((e: Error) => e);
@ -202,18 +202,21 @@ export async function request(url: string, options: RequestOpts = {}): Promise<s
reject(res);
return;
}
if(res.head && cookies_added){
if (res.head && cookies_added) {
let cookies = res.head.filter((x) => x.toLocaleLowerCase().startsWith('set-cookie: '));
cookies.forEach((x) => {
x.toLocaleLowerCase().split('set-cookie: ')[1].split(';').forEach((y) => {
const arr = y.split('=')
if(arr.length <= 1 ) return;
const key = arr.shift()?.trim() as string
const value = arr.join('=').trim()
setCookie(key, value);
});
x.toLocaleLowerCase()
.split('set-cookie: ')[1]
.split(';')
.forEach((y) => {
const arr = y.split('=');
if (arr.length <= 1) return;
const key = arr.shift()?.trim() as string;
const value = arr.join('=').trim();
setCookie(key, value);
});
});
uploadCookie()
uploadCookie();
}
if (res.statusCode >= 300 && res.statusCode < 400) {
let url = res.head.filter((x) => x.startsWith('Location: '));

View File

@ -90,17 +90,29 @@ export async function stream_from_info(
*/
export async function validate(
url: string
): Promise<'so_playlist' | 'so_track' | 'sp_track' | 'sp_album' | 'sp_playlist' | 'yt_video' | 'yt_playlist' | false> {
): Promise<
| 'so_playlist'
| 'so_track'
| 'so_search'
| 'sp_track'
| 'sp_album'
| 'sp_playlist'
| 'sp_search'
| 'yt_video'
| 'yt_playlist'
| 'yt_search'
| false
> {
let check;
if (url.indexOf('spotify') !== -1) {
check = sp_validate(url);
return check !== false ? (('sp_' + check) as 'sp_track' | 'sp_album' | 'sp_playlist') : false;
return check !== false ? (('sp_' + check) as 'sp_track' | 'sp_album' | 'sp_playlist' | 'sp_search') : false;
} else if (url.indexOf('soundcloud') !== -1) {
check = await so_validate(url);
return check !== false ? (('so_' + check) as 'so_playlist' | 'so_track') : false;
return check !== false ? (('so_' + check) as 'so_playlist' | 'so_track' | 'so_search') : false;
} else {
check = yt_validate(url);
return check !== false ? (('yt_' + check) as 'yt_video' | 'yt_playlist') : false;
return check !== false ? (('yt_' + check) as 'yt_video' | 'yt_playlist' | 'yt_search') : false;
}
}
/**
@ -183,12 +195,12 @@ export function authorization(): void {
console.log('Cookies has been added successfully.');
let cookie: Object = {};
cook.split(';').forEach((x) => {
const arr = x.split('=')
if(arr.length <= 1 ) return;
const key = arr.shift()?.trim() as string
const value = arr.join('=').trim()
Object.assign(cookie, { [key] : value })
})
const arr = x.split('=');
if (arr.length <= 1) return;
const key = arr.shift()?.trim() as string;
const value = arr.join('=').trim();
Object.assign(cookie, { [key]: value });
});
fs.writeFileSync('.data/youtube.data', JSON.stringify({ cookie }, undefined, 4));
ask.close();
});
@ -208,4 +220,4 @@ export function attachListeners(player: EventEmitter, resource: YouTubeStream |
player.removeListener(AudioPlayerStatus.AutoPaused, () => resource.pause());
player.removeListener(AudioPlayerStatus.Playing, () => resource.resume());
});
};
}