Cookies update

This commit is contained in:
killer069
2021-10-08 14:58:06 +05:30
parent 0058e71efd
commit 4608f3d59b
8 changed files with 257 additions and 23 deletions

View File

@@ -130,7 +130,6 @@ export class Stream {
private per_sec_bytes: number;
private content_length: number;
private video_url: string;
private cookie: string;
private timer: Timer;
private quality: number;
private proxy: Proxy[] | undefined;
@@ -141,7 +140,6 @@ export class Stream {
duration: number,
contentLength: number,
video_url: string,
cookie: string,
options: StreamOptions
) {
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 });
@@ -151,7 +149,6 @@ export class Stream {
this.type = type;
this.bytes_count = 0;
this.video_url = video_url;
this.cookie = cookie;
this.per_sec_bytes = Math.ceil(contentLength / duration);
this.content_length = contentLength;
this.request = null;
@@ -166,7 +163,7 @@ export class Stream {
}
private async retry() {
const info = await video_info(this.video_url, { cookie: this.cookie, proxy: this.proxy });
const info = await video_info(this.video_url, { proxy: this.proxy });
const audioFormat = parseAudioFormats(info.format);
this.url = audioFormat[this.quality].url;
}

View File

@@ -12,7 +12,6 @@ export enum StreamType {
export interface StreamOptions {
quality?: number;
cookie?: string;
proxy?: Proxy[];
}
@@ -54,7 +53,7 @@ export type YouTubeStream = Stream | LiveStreaming;
* @returns Stream class with type and stream for playing.
*/
export async function stream(url: string, options: StreamOptions = {}): Promise<YouTubeStream> {
const info = await video_info(url, { cookie: options.cookie, proxy: options.proxy });
const info = await video_info(url, { proxy: options.proxy });
const final: any[] = [];
if (
info.LiveStreamData.isLive === true &&
@@ -83,7 +82,6 @@ export async function stream(url: string, options: StreamOptions = {}): Promise<
info.video_details.durationInSec,
Number(final[0].contentLength),
info.video_details.url,
options.cookie as string,
options
);
}
@@ -122,7 +120,6 @@ export async function stream_from_info(info: InfoData, options: StreamOptions =
info.video_details.durationInSec,
Number(final[0].contentLength),
info.video_details.url,
options.cookie as string,
options
);
}

View 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));
}

View File

@@ -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] + '}}');

View File

@@ -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`));
}