mirror of
https://github.com/YuzuZensai/play-dl-test.git
synced 2026-01-31 14:58:05 +00:00
search validation added
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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] + '}}');
|
||||
|
||||
@@ -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: '));
|
||||
|
||||
Reference in New Issue
Block a user