mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-19 04:21:35 +00:00
refactor(HTTPClient): Use getCookie fn to get SAPISID token
This commit is contained in:
@@ -4,8 +4,8 @@ import {
|
||||
Platform,
|
||||
generateSidAuth,
|
||||
getRandomUserAgent,
|
||||
getStringBetweenStrings,
|
||||
InnertubeError
|
||||
InnertubeError,
|
||||
getCookie
|
||||
} from './Utils.js';
|
||||
|
||||
import type { Context, Session } from '../core/index.js';
|
||||
@@ -14,6 +14,7 @@ import type { FetchFunction } from '../types/index.js';
|
||||
export interface HTTPClientInit {
|
||||
baseURL?: string;
|
||||
}
|
||||
|
||||
export default class HTTPClient {
|
||||
#session: Session;
|
||||
#cookie?: string;
|
||||
@@ -137,10 +138,10 @@ export default class HTTPClient {
|
||||
}
|
||||
|
||||
if (this.#cookie) {
|
||||
const papisid = getStringBetweenStrings(this.#cookie, 'PAPISID=', ';');
|
||||
const sapisid = getCookie(this.#cookie, 'SAPISID');
|
||||
|
||||
if (papisid) {
|
||||
request_headers.set('Authorization', await generateSidAuth(papisid));
|
||||
if (sapisid) {
|
||||
request_headers.set('Authorization', await generateSidAuth(sapisid));
|
||||
request_headers.set('X-Goog-Authuser', this.#session.account_index.toString());
|
||||
}
|
||||
|
||||
|
||||
@@ -239,4 +239,10 @@ export function base64ToU8(base64: string): Uint8Array {
|
||||
|
||||
export function isTextRun(run: TextRun | EmojiRun): run is TextRun {
|
||||
return !('emoji' in run);
|
||||
}
|
||||
|
||||
export function getCookie(cookies: string, name: string, matchWholeName = false): string | undefined {
|
||||
const regex = matchWholeName ? `(^|\\s?)\\b${name}\\b=([^;]+)` : `(^|s?)${name}=([^;]+)`;
|
||||
const match = cookies.match(new RegExp(regex));
|
||||
return match ? match[2] : undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user