Skip to content

Commit 05ba1f0

Browse files
committed
youtube: periodically refresh innertube player
1 parent 7f12f4c commit 05ba1f0

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/modules/processing/services/youtube.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { env } from "../../config.js";
66
import { cleanString } from "../../sub/utils.js";
77
import { getCookie, updateCookieValues } from "../cookie/manager.js";
88

9-
const ytBase = Innertube.create().catch(e => e);
9+
const PLAYER_REFRESH_PERIOD = 1000 * 60 * 15; // ms
10+
11+
let innertube, lastRefreshedAt;
1012

1113
const codecMatch = {
1214
h264: {
@@ -48,11 +50,12 @@ const transformSessionData = (cookie) => {
4850
}
4951

5052
const cloneInnertube = async (customFetch) => {
51-
const innertube = await ytBase;
52-
if (innertube instanceof Error) {
53-
if (innertube?.message?.endsWith("decipher algorithm")) {
54-
return { error: "ErrorYoutubeDecipher" }
55-
} else throw innertube;
53+
const shouldRefreshPlayer = lastRefreshedAt + PLAYER_REFRESH_PERIOD < new Date();
54+
if (!innertube || shouldRefreshPlayer) {
55+
innertube = await Innertube.create({
56+
fetch: customFetch
57+
});
58+
lastRefreshedAt = +new Date();
5659
}
5760

5861
const session = new Session(
@@ -97,13 +100,19 @@ const cloneInnertube = async (customFetch) => {
97100
}
98101

99102
export default async function(o) {
100-
const yt = await cloneInnertube(
101-
(input, init) => fetch(input, {
102-
...init,
103-
dispatcher: o.dispatcher
104-
})
105-
);
106-
if (yt.error) return yt;
103+
let yt;
104+
try {
105+
yt = await cloneInnertube(
106+
(input, init) => fetch(input, {
107+
...init,
108+
dispatcher: o.dispatcher
109+
})
110+
);
111+
} catch(e) {
112+
if (e.message?.endsWith("decipher algorithm")) {
113+
return { error: "ErrorYoutubeDecipher" }
114+
} else throw e;
115+
}
107116

108117
const quality = o.quality === "max" ? "9000" : o.quality;
109118

0 commit comments

Comments
 (0)