mirror of
https://github.com/YuzuZensai/play-dl-test.git
synced 2026-01-31 14:58:05 +00:00
Cookies update
This commit is contained in:
31
play-dl/YouTube/utils/cookie.ts
Normal file
31
play-dl/YouTube/utils/cookie.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import fs from 'fs';
|
||||
|
||||
let youtubeData: youtubeDataOptions;
|
||||
if (fs.existsSync('.data/youtube.data')) {
|
||||
youtubeData = JSON.parse(fs.readFileSync('.data/youtube.data').toString());
|
||||
}
|
||||
|
||||
interface youtubeDataOptions {
|
||||
cookie?: Object;
|
||||
}
|
||||
|
||||
export function getCookies(): undefined | string {
|
||||
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
|
||||
}
|
||||
|
||||
export function uploadCookie() {
|
||||
if(youtubeData) fs.writeFileSync('.data/youtube.data', JSON.stringify(youtubeData, undefined, 4));
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import { YouTubeVideo } from '../classes/Video';
|
||||
import { YouTubePlayList } from '../classes/Playlist';
|
||||
|
||||
interface InfoOptions {
|
||||
cookie?: string;
|
||||
proxy?: Proxy[];
|
||||
}
|
||||
|
||||
@@ -78,12 +77,8 @@ export async function video_basic_info(url: string, options: InfoOptions = {}) {
|
||||
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: options.cookie
|
||||
? {
|
||||
'cookie': options.cookie,
|
||||
'accept-language': 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7'
|
||||
}
|
||||
: { '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' },
|
||||
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] + '}}');
|
||||
|
||||
@@ -2,6 +2,7 @@ import https, { RequestOptions } from 'https';
|
||||
import tls from 'tls';
|
||||
import http, { ClientRequest, IncomingMessage } from 'http';
|
||||
import { URL } from 'url';
|
||||
import { getCookies, setCookie, uploadCookie } from './cookie';
|
||||
/**
|
||||
* Types for Proxy
|
||||
*/
|
||||
@@ -18,7 +19,7 @@ interface ProxyOpts {
|
||||
|
||||
interface ProxyOutput {
|
||||
statusCode: number;
|
||||
head: string;
|
||||
head: string[];
|
||||
body: string;
|
||||
}
|
||||
|
||||
@@ -26,6 +27,7 @@ interface RequestOpts extends RequestOptions {
|
||||
body?: string;
|
||||
method?: 'GET' | 'POST';
|
||||
proxies?: Proxy[];
|
||||
cookies? : boolean
|
||||
}
|
||||
/**
|
||||
* Main module that play-dl uses for making a https request
|
||||
@@ -135,7 +137,7 @@ async function proxy_getter(req_url: string, req_proxy: Proxy[]): Promise<ProxyO
|
||||
const head = y.shift() as string;
|
||||
resolve({
|
||||
statusCode: Number(head.split('\n')[0].split(' ')[1]),
|
||||
head: head,
|
||||
head: head.split('\r\n'),
|
||||
body: y.join('\n')
|
||||
});
|
||||
});
|
||||
@@ -150,15 +152,29 @@ async function proxy_getter(req_url: string, req_proxy: Proxy[]): Promise<ProxyO
|
||||
* @param options Request options for https request
|
||||
* @returns body of that request
|
||||
*/
|
||||
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) => {
|
||||
if (!options?.proxies || options.proxies.length === 0) {
|
||||
let data = '';
|
||||
let cook = getCookies()
|
||||
if (typeof cook === 'string' && options.headers) {
|
||||
Object.assign(options.headers, { cookie : cook });
|
||||
}
|
||||
let res = await https_getter(url, options).catch((err: Error) => err);
|
||||
if (res instanceof Error) {
|
||||
reject(res);
|
||||
return;
|
||||
}
|
||||
if(res.headers && res.headers['set-cookie'] && cook){
|
||||
res.headers['set-cookie'].forEach((x) => {
|
||||
x.split(';').forEach((x) => {
|
||||
const [key, value] = x.split('=');
|
||||
if (!value) return;
|
||||
setCookie(key, value);
|
||||
});
|
||||
})
|
||||
uploadCookie()
|
||||
}
|
||||
if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) {
|
||||
res = await https_getter(res.headers.location as string, options);
|
||||
} else if (Number(res.statusCode) > 400) {
|
||||
@@ -168,13 +184,30 @@ export async function request(url: string, options?: RequestOpts): Promise<strin
|
||||
res.on('data', (c) => (data += c));
|
||||
res.on('end', () => resolve(data));
|
||||
} else {
|
||||
let cook = getCookies()
|
||||
if (typeof cook === 'string' && options.headers) {
|
||||
Object.assign(options.headers, { cookie : cook });
|
||||
}
|
||||
let res = await proxy_getter(url, options.proxies).catch((e: Error) => e);
|
||||
if (res instanceof Error) {
|
||||
reject(res);
|
||||
return;
|
||||
}
|
||||
if(res.head && cook){
|
||||
let cookies = res.head.filter((x) => x.toLocaleLowerCase().startsWith('set-cookie: '));
|
||||
cookies.forEach((x) => {
|
||||
x.toLocaleLowerCase().split('set-cookie: ')[1].split(';').forEach((y) => {
|
||||
let [key, value] = y.split('=');
|
||||
if (!value)
|
||||
return;
|
||||
setCookie(key, value);
|
||||
});
|
||||
});
|
||||
uploadCookie()
|
||||
}
|
||||
if (res.statusCode >= 300 && res.statusCode < 400) {
|
||||
res = await proxy_getter(res.head.split('Location: ')[1].split('\n')[0], options.proxies);
|
||||
let url = res.head.filter((x) => x.startsWith('Location: '));
|
||||
res = await proxy_getter(url[0].split('\n')[0], options.proxies);
|
||||
} else if (res.statusCode > 400) {
|
||||
reject(new Error(`GOT ${res.statusCode} from proxy request`));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user