From 703e36c78dc441af92f5a02261cdf1f3c11ed787 Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Mon, 1 Nov 2021 15:32:51 +0530 Subject: [PATCH] Cookies Header function added --- docs/README.md | 10 ++++++++++ play-dl/Request/index.ts | 4 ++-- play-dl/YouTube/index.ts | 2 +- play-dl/YouTube/stream.ts | 4 ++-- play-dl/YouTube/utils/cookie.ts | 5 +++-- play-dl/YouTube/utils/extractor.ts | 9 ++++----- play-dl/index.ts | 3 ++- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8cafad8..33fd5e3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -178,3 +178,13 @@ let resource = createAudioResource(source.stream, { inputType : source.type }) // This creates resource for playing ``` + +#### cookieHeaders(headersCookie : `string[]`) + +_This is function to update youtube cookies when using external https module._ + +```js +const res = ... // You need to get response. + +play.cookieHeaders(res.headers['set-cookie']) // Updates YouTube Cookies if cookies exists. +``` \ No newline at end of file diff --git a/play-dl/Request/index.ts b/play-dl/Request/index.ts index 8816197..3f489d5 100644 --- a/play-dl/Request/index.ts +++ b/play-dl/Request/index.ts @@ -63,7 +63,7 @@ export function request(req_url: string, options: RequestOpts = { method: 'GET' return; } if (res.headers && res.headers['set-cookie'] && cookies_added) { - cookieHeaders(res.headers['set-cookie']) + cookieHeaders(res.headers['set-cookie']); } if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) { res = await https_getter(res.headers.location as string, options); @@ -89,7 +89,7 @@ export function request(req_url: string, options: RequestOpts = { method: 'GET' return; } if (res.headers && (res.headers as any)['set-cookie'] && cookies_added) { - cookieHeaders((res.headers as any)['set-cookie']) + cookieHeaders((res.headers as any)['set-cookie']); } if (res.statusCode >= 300 && res.statusCode < 400) { res = await proxy_getter((res.headers as any)['location'], options.proxies, options.headers); diff --git a/play-dl/YouTube/index.ts b/play-dl/YouTube/index.ts index b71f77e..30b8e7d 100644 --- a/play-dl/YouTube/index.ts +++ b/play-dl/YouTube/index.ts @@ -1,4 +1,4 @@ export { stream, stream_from_info, YouTubeStream } from './stream'; export * from './utils'; export { YouTube } from './search'; -export { cookieHeaders } from './utils/cookie' \ No newline at end of file +export { cookieHeaders } from './utils/cookie'; diff --git a/play-dl/YouTube/stream.ts b/play-dl/YouTube/stream.ts index 802cd7a..14d0e2e 100644 --- a/play-dl/YouTube/stream.ts +++ b/play-dl/YouTube/stream.ts @@ -13,7 +13,7 @@ export enum StreamType { export interface StreamOptions { quality?: number; proxy?: Proxy[]; - htmldata? : boolean + htmldata?: boolean; } export interface InfoData { @@ -54,7 +54,7 @@ export type YouTubeStream = Stream | LiveStreaming; * @returns Stream class with type and stream for playing. */ export async function stream(url: string, options: StreamOptions = {}): Promise { - const info = await video_info(url, { proxy: options.proxy, htmldata : options.htmldata }); + const info = await video_info(url, { proxy: options.proxy, htmldata: options.htmldata }); const final: any[] = []; if ( info.LiveStreamData.isLive === true && diff --git a/play-dl/YouTube/utils/cookie.ts b/play-dl/YouTube/utils/cookie.ts index bde3b1c..45e3447 100644 --- a/play-dl/YouTube/utils/cookie.ts +++ b/play-dl/YouTube/utils/cookie.ts @@ -47,7 +47,8 @@ export function setCookieToken(options: { cookie: string }) { youtubeData.file = false; } -export function cookieHeaders(headCookie : string[]){ +export function cookieHeaders(headCookie: string[]) { + if (!youtubeData?.cookie) return; headCookie.forEach((x: string) => { x.split(';').forEach((x) => { const arr = x.split('='); @@ -58,4 +59,4 @@ export function cookieHeaders(headCookie : string[]){ }); }); uploadCookie(); -} \ No newline at end of file +} diff --git a/play-dl/YouTube/utils/extractor.ts b/play-dl/YouTube/utils/extractor.ts index 32637f8..e3b87e0 100644 --- a/play-dl/YouTube/utils/extractor.ts +++ b/play-dl/YouTube/utils/extractor.ts @@ -6,7 +6,7 @@ import { InfoData } from '../stream'; interface InfoOptions { proxy?: Proxy[]; - htmldata? : boolean + htmldata?: boolean; } interface PlaylistOptions { @@ -80,11 +80,10 @@ 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 body : string; - if(options.htmldata){ + let body: string; + if (options.htmldata) { body = url; - } - else { + } else { if (yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL'); const video_id: string = extractID(url); const new_url = `https://www.youtube.com/watch?v=${video_id}&has_verified=1`; diff --git a/play-dl/index.ts b/play-dl/index.ts index 85d3312..410450a 100644 --- a/play-dl/index.ts +++ b/play-dl/index.ts @@ -6,7 +6,8 @@ export { yt_validate, extractID, YouTube, - YouTubeStream + YouTubeStream, + cookieHeaders } from './YouTube'; export { spotify, sp_validate, refreshToken, is_expired, Spotify } from './Spotify'; export { soundcloud, so_validate, SoundCloud, SoundCloudStream, getFreeClientID } from './SoundCloud';