diff --git a/src/utils/HTTPClient.ts b/src/utils/HTTPClient.ts index 4ba9d892..a7962532 100644 --- a/src/utils/HTTPClient.ts +++ b/src/utils/HTTPClient.ts @@ -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()); } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 7f9068a4..02c86d30 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -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; } \ No newline at end of file