From 9da3ba60a9e07ec95e48c2ff1f6820ee92fa83c4 Mon Sep 17 00:00:00 2001 From: jj Date: Mon, 23 Dec 2024 10:52:49 +0000 Subject: [PATCH] api/youtube: add support for cookies --- api/src/processing/cookie/manager.js | 1 + api/src/processing/services/youtube.js | 14 ++++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/api/src/processing/cookie/manager.js b/api/src/processing/cookie/manager.js index c2b37801..25f41c2c 100644 --- a/api/src/processing/cookie/manager.js +++ b/api/src/processing/cookie/manager.js @@ -12,6 +12,7 @@ const VALID_SERVICES = new Set([ 'instagram_bearer', 'reddit', 'twitter', + 'youtube', 'youtube_oauth' ]); diff --git a/api/src/processing/services/youtube.js b/api/src/processing/services/youtube.js index c0178b35..6e61efa9 100644 --- a/api/src/processing/services/youtube.js +++ b/api/src/processing/services/youtube.js @@ -66,10 +66,12 @@ const transformSessionData = (cookie) => { const cloneInnertube = async (customFetch) => { const shouldRefreshPlayer = lastRefreshedAt + PLAYER_REFRESH_PERIOD < new Date(); + const cookie = getCookie('youtube')?.toString(); if (!innertube || shouldRefreshPlayer) { innertube = await Innertube.create({ fetch: customFetch, retrieve_player: false, + cookie }); lastRefreshedAt = +new Date(); } @@ -80,30 +82,30 @@ const cloneInnertube = async (customFetch) => { innertube.session.api_version, innertube.session.account_index, innertube.session.player, - undefined, + cookie, customFetch ?? innertube.session.http.fetch, innertube.session.cache ); - const cookie = getCookie('youtube_oauth'); - const oauthData = transformSessionData(cookie); + const oauthCookie = getCookie('youtube_oauth'); + const oauthData = transformSessionData(oauthCookie); if (!session.logged_in && oauthData) { await session.oauth.init(oauthData); session.logged_in = true; } - if (session.logged_in) { + if (session.logged_in && oauthData) { if (session.oauth.shouldRefreshToken()) { await session.oauth.refreshAccessToken(); } - const cookieValues = cookie.values(); + const cookieValues = oauthCookie.values(); const oldExpiry = new Date(cookieValues.expiry_date); const newExpiry = new Date(session.oauth.oauth2_tokens.expiry_date); if (oldExpiry.getTime() !== newExpiry.getTime()) { - updateCookieValues(cookie, { + updateCookieValues(oauthCookie, { ...session.oauth.client_id, ...session.oauth.oauth2_tokens, expiry_date: newExpiry.toISOString()