Cookies Header function added

This commit is contained in:
killer069 2021-11-01 15:32:51 +05:30
parent 5214250eac
commit 703e36c78d
7 changed files with 24 additions and 13 deletions

View File

@ -178,3 +178,13 @@ let resource = createAudioResource(source.stream, {
inputType : source.type inputType : source.type
}) // This creates resource for playing }) // 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.
```

View File

@ -63,7 +63,7 @@ export function request(req_url: string, options: RequestOpts = { method: 'GET'
return; return;
} }
if (res.headers && res.headers['set-cookie'] && cookies_added) { 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) { if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) {
res = await https_getter(res.headers.location as string, options); 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; return;
} }
if (res.headers && (res.headers as any)['set-cookie'] && cookies_added) { 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) { if (res.statusCode >= 300 && res.statusCode < 400) {
res = await proxy_getter((res.headers as any)['location'], options.proxies, options.headers); res = await proxy_getter((res.headers as any)['location'], options.proxies, options.headers);

View File

@ -1,4 +1,4 @@
export { stream, stream_from_info, YouTubeStream } from './stream'; export { stream, stream_from_info, YouTubeStream } from './stream';
export * from './utils'; export * from './utils';
export { YouTube } from './search'; export { YouTube } from './search';
export { cookieHeaders } from './utils/cookie' export { cookieHeaders } from './utils/cookie';

View File

@ -13,7 +13,7 @@ export enum StreamType {
export interface StreamOptions { export interface StreamOptions {
quality?: number; quality?: number;
proxy?: Proxy[]; proxy?: Proxy[];
htmldata? : boolean htmldata?: boolean;
} }
export interface InfoData { export interface InfoData {
@ -54,7 +54,7 @@ export type YouTubeStream = Stream | LiveStreaming;
* @returns Stream class with type and stream for playing. * @returns Stream class with type and stream for playing.
*/ */
export async function stream(url: string, options: StreamOptions = {}): Promise<YouTubeStream> { export async function stream(url: string, options: StreamOptions = {}): Promise<YouTubeStream> {
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[] = []; const final: any[] = [];
if ( if (
info.LiveStreamData.isLive === true && info.LiveStreamData.isLive === true &&

View File

@ -47,7 +47,8 @@ export function setCookieToken(options: { cookie: string }) {
youtubeData.file = false; youtubeData.file = false;
} }
export function cookieHeaders(headCookie : string[]){ export function cookieHeaders(headCookie: string[]) {
if (!youtubeData?.cookie) return;
headCookie.forEach((x: string) => { headCookie.forEach((x: string) => {
x.split(';').forEach((x) => { x.split(';').forEach((x) => {
const arr = x.split('='); const arr = x.split('=');
@ -58,4 +59,4 @@ export function cookieHeaders(headCookie : string[]){
}); });
}); });
uploadCookie(); uploadCookie();
} }

View File

@ -6,7 +6,7 @@ import { InfoData } from '../stream';
interface InfoOptions { interface InfoOptions {
proxy?: Proxy[]; proxy?: Proxy[];
htmldata? : boolean htmldata?: boolean;
} }
interface PlaylistOptions { interface PlaylistOptions {
@ -80,11 +80,10 @@ export function extractID(url: string): string {
* @returns Data containing video_details, LiveStreamData and formats of video url. * @returns Data containing video_details, LiveStreamData and formats of video url.
*/ */
export async function video_basic_info(url: string, options: InfoOptions = {}) { export async function video_basic_info(url: string, options: InfoOptions = {}) {
let body : string; let body: string;
if(options.htmldata){ if (options.htmldata) {
body = url; body = url;
} } else {
else {
if (yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL'); if (yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL');
const video_id: string = extractID(url); const video_id: string = extractID(url);
const new_url = `https://www.youtube.com/watch?v=${video_id}&has_verified=1`; const new_url = `https://www.youtube.com/watch?v=${video_id}&has_verified=1`;

View File

@ -6,7 +6,8 @@ export {
yt_validate, yt_validate,
extractID, extractID,
YouTube, YouTube,
YouTubeStream YouTubeStream,
cookieHeaders
} from './YouTube'; } from './YouTube';
export { spotify, sp_validate, refreshToken, is_expired, Spotify } from './Spotify'; export { spotify, sp_validate, refreshToken, is_expired, Spotify } from './Spotify';
export { soundcloud, so_validate, SoundCloud, SoundCloudStream, getFreeClientID } from './SoundCloud'; export { soundcloud, so_validate, SoundCloud, SoundCloudStream, getFreeClientID } from './SoundCloud';