mirror of
https://github.com/wukko/cobalt.git
synced 2025-02-02 08:26:22 +01:00
api/youtube: add support for cookies
This commit is contained in:
parent
999fa562e0
commit
9da3ba60a9
2 changed files with 9 additions and 6 deletions
|
@ -12,6 +12,7 @@ const VALID_SERVICES = new Set([
|
||||||
'instagram_bearer',
|
'instagram_bearer',
|
||||||
'reddit',
|
'reddit',
|
||||||
'twitter',
|
'twitter',
|
||||||
|
'youtube',
|
||||||
'youtube_oauth'
|
'youtube_oauth'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
@ -66,10 +66,12 @@ const transformSessionData = (cookie) => {
|
||||||
|
|
||||||
const cloneInnertube = async (customFetch) => {
|
const cloneInnertube = async (customFetch) => {
|
||||||
const shouldRefreshPlayer = lastRefreshedAt + PLAYER_REFRESH_PERIOD < new Date();
|
const shouldRefreshPlayer = lastRefreshedAt + PLAYER_REFRESH_PERIOD < new Date();
|
||||||
|
const cookie = getCookie('youtube')?.toString();
|
||||||
if (!innertube || shouldRefreshPlayer) {
|
if (!innertube || shouldRefreshPlayer) {
|
||||||
innertube = await Innertube.create({
|
innertube = await Innertube.create({
|
||||||
fetch: customFetch,
|
fetch: customFetch,
|
||||||
retrieve_player: false,
|
retrieve_player: false,
|
||||||
|
cookie
|
||||||
});
|
});
|
||||||
lastRefreshedAt = +new Date();
|
lastRefreshedAt = +new Date();
|
||||||
}
|
}
|
||||||
|
@ -80,30 +82,30 @@ const cloneInnertube = async (customFetch) => {
|
||||||
innertube.session.api_version,
|
innertube.session.api_version,
|
||||||
innertube.session.account_index,
|
innertube.session.account_index,
|
||||||
innertube.session.player,
|
innertube.session.player,
|
||||||
undefined,
|
cookie,
|
||||||
customFetch ?? innertube.session.http.fetch,
|
customFetch ?? innertube.session.http.fetch,
|
||||||
innertube.session.cache
|
innertube.session.cache
|
||||||
);
|
);
|
||||||
|
|
||||||
const cookie = getCookie('youtube_oauth');
|
const oauthCookie = getCookie('youtube_oauth');
|
||||||
const oauthData = transformSessionData(cookie);
|
const oauthData = transformSessionData(oauthCookie);
|
||||||
|
|
||||||
if (!session.logged_in && oauthData) {
|
if (!session.logged_in && oauthData) {
|
||||||
await session.oauth.init(oauthData);
|
await session.oauth.init(oauthData);
|
||||||
session.logged_in = true;
|
session.logged_in = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session.logged_in) {
|
if (session.logged_in && oauthData) {
|
||||||
if (session.oauth.shouldRefreshToken()) {
|
if (session.oauth.shouldRefreshToken()) {
|
||||||
await session.oauth.refreshAccessToken();
|
await session.oauth.refreshAccessToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
const cookieValues = cookie.values();
|
const cookieValues = oauthCookie.values();
|
||||||
const oldExpiry = new Date(cookieValues.expiry_date);
|
const oldExpiry = new Date(cookieValues.expiry_date);
|
||||||
const newExpiry = new Date(session.oauth.oauth2_tokens.expiry_date);
|
const newExpiry = new Date(session.oauth.oauth2_tokens.expiry_date);
|
||||||
|
|
||||||
if (oldExpiry.getTime() !== newExpiry.getTime()) {
|
if (oldExpiry.getTime() !== newExpiry.getTime()) {
|
||||||
updateCookieValues(cookie, {
|
updateCookieValues(oauthCookie, {
|
||||||
...session.oauth.client_id,
|
...session.oauth.client_id,
|
||||||
...session.oauth.oauth2_tokens,
|
...session.oauth.oauth2_tokens,
|
||||||
expiry_date: newExpiry.toISOString()
|
expiry_date: newExpiry.toISOString()
|
||||||
|
|
Loading…
Reference in a new issue