Skip to content

Commit

Permalink
youtube: don't block api startup waiting for innertube to activate (#532
Browse files Browse the repository at this point in the history
)

cobalt api has been getting blocked for several seconds
during startup, and also crashing when unable to connect
to youtube (e.g. when it's blocked); this should fix both
those things
  • Loading branch information
dumbmoron committed May 29, 2024
1 parent 35ba3dc commit 44ecfee
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/modules/processing/services/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { env } from '../../config.js';
import { cleanString } from '../../sub/utils.js';
import { fetch } from 'undici'

const ytBase = await Innertube.create();
const ytBase = Innertube.create().catch(e => e);

const codecMatch = {
h264: {
Expand All @@ -23,24 +23,29 @@ const codecMatch = {
}
}

const cloneInnertube = (customFetch) => {
const cloneInnertube = async (customFetch) => {
const innertube = await ytBase;
if (innertube instanceof Error) {
throw innertube;
}

const session = new Session(
ytBase.session.context,
ytBase.session.key,
ytBase.session.api_version,
ytBase.session.account_index,
ytBase.session.player,
innertube.session.context,
innertube.session.key,
innertube.session.api_version,
innertube.session.account_index,
innertube.session.player,
undefined,
customFetch ?? ytBase.session.http.fetch,
ytBase.session.cache
customFetch ?? innertube.session.http.fetch,
innertube.session.cache
);

const yt = new Innertube(session);
return yt;
}

export default async function(o) {
const yt = cloneInnertube(
const yt = await cloneInnertube(
(input, init) => fetch(input, { ...init, dispatcher: o.dispatcher })
);

Expand Down

0 comments on commit 44ecfee

Please sign in to comment.