mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 12:50:01 +00:00
youtube: periodically refresh innertube player
This commit is contained in:
parent
dd30973601
commit
3fdf266ad0
1 changed files with 22 additions and 13 deletions
|
@ -6,7 +6,9 @@ import { env } from "../../config.js";
|
||||||
import { cleanString } from "../../sub/utils.js";
|
import { cleanString } from "../../sub/utils.js";
|
||||||
import { getCookie, updateCookieValues } from "../cookie/manager.js";
|
import { getCookie, updateCookieValues } from "../cookie/manager.js";
|
||||||
|
|
||||||
const ytBase = Innertube.create().catch(e => e);
|
const PLAYER_REFRESH_PERIOD = 1000 * 60 * 15; // ms
|
||||||
|
|
||||||
|
let innertube, lastRefreshedAt;
|
||||||
|
|
||||||
const codecMatch = {
|
const codecMatch = {
|
||||||
h264: {
|
h264: {
|
||||||
|
@ -48,11 +50,12 @@ const transformSessionData = (cookie) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const cloneInnertube = async (customFetch) => {
|
const cloneInnertube = async (customFetch) => {
|
||||||
const innertube = await ytBase;
|
const shouldRefreshPlayer = lastRefreshedAt + PLAYER_REFRESH_PERIOD < new Date();
|
||||||
if (innertube instanceof Error) {
|
if (!innertube || shouldRefreshPlayer) {
|
||||||
if (innertube?.message?.endsWith("decipher algorithm")) {
|
innertube = await Innertube.create({
|
||||||
return { error: "ErrorYoutubeDecipher" }
|
fetch: customFetch
|
||||||
} else throw innertube;
|
});
|
||||||
|
lastRefreshedAt = +new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
const session = new Session(
|
const session = new Session(
|
||||||
|
@ -97,13 +100,19 @@ const cloneInnertube = async (customFetch) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function(o) {
|
export default async function(o) {
|
||||||
const yt = await cloneInnertube(
|
let yt;
|
||||||
|
try {
|
||||||
|
yt = await cloneInnertube(
|
||||||
(input, init) => fetch(input, {
|
(input, init) => fetch(input, {
|
||||||
...init,
|
...init,
|
||||||
dispatcher: o.dispatcher
|
dispatcher: o.dispatcher
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
if (yt.error) return yt;
|
} catch(e) {
|
||||||
|
if (e.message?.endsWith("decipher algorithm")) {
|
||||||
|
return { error: "ErrorYoutubeDecipher" }
|
||||||
|
} else throw e;
|
||||||
|
}
|
||||||
|
|
||||||
const quality = o.quality === "max" ? "9000" : o.quality;
|
const quality = o.quality === "max" ? "9000" : o.quality;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue