From 2387fc2fbb85590efa8040400044e4b41a60d560 Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Sat, 8 Jun 2024 09:30:12 +0000 Subject: [PATCH] youtube: update access token on change --- src/modules/processing/services/youtube.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/modules/processing/services/youtube.js b/src/modules/processing/services/youtube.js index 3ad956d9..49de029b 100644 --- a/src/modules/processing/services/youtube.js +++ b/src/modules/processing/services/youtube.js @@ -2,7 +2,7 @@ import { Innertube, Session } from 'youtubei.js'; import { env } from '../../config.js'; import { cleanString } from '../../sub/utils.js'; import { fetch } from 'undici' -import { getCookie } from '../cookie/manager.js' +import { getCookie, updateCookieValues } from '../cookie/manager.js' const ytBase = Innertube.create().catch(e => e); @@ -61,7 +61,8 @@ const cloneInnertube = async (customFetch) => { innertube.session.cache ); - const oauthData = transformSessionData(getCookie('youtube_oauth')); + const cookie = getCookie('youtube_oauth'); + const oauthData = transformSessionData(cookie); if (!session.logged_in && oauthData) { await session.oauth.init(oauthData); @@ -70,6 +71,15 @@ const cloneInnertube = async (customFetch) => { if (session.logged_in) { await session.oauth.refreshIfRequired(); + const oldExpiry = new Date(cookie.values().expires); + const newExpiry = session.oauth.credentials.expires; + + if (oldExpiry.getTime() !== newExpiry.getTime()) { + updateCookieValues(cookie, { + ...session.oauth.credentials, + expires: session.oauth.credentials.expires.toISOString() + }); + } } const yt = new Innertube(session);