api/youtube: use poToken, visitorData, and web client with cookies

and also decipher media whenever needed, but only if cookies are used
This commit is contained in:
wukko 2024-12-23 22:58:16 +06:00
parent 9da3ba60a9
commit c6d0e0bdd5
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2

View file

@ -66,12 +66,18 @@ 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();
const rawCookie = getCookie('youtube');
const cookieValues = rawCookie?.values();
const cookie = rawCookie?.toString();
if (!innertube || shouldRefreshPlayer) { if (!innertube || shouldRefreshPlayer) {
innertube = await Innertube.create({ innertube = await Innertube.create({
fetch: customFetch, fetch: customFetch,
retrieve_player: false, retrieve_player: !!cookie,
cookie cookie,
po_token: cookieValues?.po_token,
visitor_data: cookieValues?.visitor_data,
}); });
lastRefreshedAt = +new Date(); lastRefreshedAt = +new Date();
} }
@ -134,6 +140,8 @@ export default async function(o) {
} else throw e; } else throw e;
} }
const cookie = getCookie('youtube')?.toString();
let useHLS = o.youtubeHLS; let useHLS = o.youtubeHLS;
// HLS playlists don't contain the av1 video format, at least with the iOS client // HLS playlists don't contain the av1 video format, at least with the iOS client
@ -141,9 +149,20 @@ export default async function(o) {
useHLS = false; useHLS = false;
} }
let innertubeClient = "ANDROID";
if (cookie) {
useHLS = false;
innertubeClient = "WEB";
}
if (useHLS) {
innertubeClient = "IOS";
}
let info; let info;
try { try {
info = await yt.getBasicInfo(o.id, useHLS ? 'IOS' : 'ANDROID'); info = await yt.getBasicInfo(o.id, innertubeClient);
} catch (e) { } catch (e) {
if (e?.info) { if (e?.info) {
const errorInfo = JSON.parse(e?.info); const errorInfo = JSON.parse(e?.info);
@ -438,6 +457,10 @@ export default async function(o) {
urls = audio.uri; urls = audio.uri;
} }
if (innertubeClient === "WEB" && innertube) {
urls = audio.decipher(innertube.session.player);
}
return { return {
type: "audio", type: "audio",
isAudioOnly: true, isAudioOnly: true,
@ -464,11 +487,17 @@ export default async function(o) {
width: video.width, width: video.width,
height: video.height, height: video.height,
}); });
filenameAttributes.resolution = `${video.width}x${video.height}`; filenameAttributes.resolution = `${video.width}x${video.height}`;
filenameAttributes.extension = codecList[codec].container; filenameAttributes.extension = codecList[codec].container;
video = video.url; video = video.url;
audio = audio.url; audio = audio.url;
if (innertubeClient === "WEB" && innertube) {
video = video.decipher(innertube.session.player);
audio = audio.decipher(innertube.session.player);
}
} }
filenameAttributes.qualityLabel = `${resolution}p`; filenameAttributes.qualityLabel = `${resolution}p`;