From 029f0cb0b9da48d4f8f26d51df35f1397342f9a3 Mon Sep 17 00:00:00 2001 From: absidue <48293849+absidue@users.noreply.github.com> Date: Sat, 18 Mar 2023 21:27:56 +0100 Subject: [PATCH] Fetch the videos tab directly for local API subscriptions (#3315) --- src/renderer/helpers/api/local.js | 36 +++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/renderer/helpers/api/local.js b/src/renderer/helpers/api/local.js index 26d7d787427d9..7b1f4199a2c37 100644 --- a/src/renderer/helpers/api/local.js +++ b/src/renderer/helpers/api/local.js @@ -1,4 +1,4 @@ -import { Innertube, ClientType, Misc, Utils } from 'youtubei.js' +import { Innertube, ClientType, Misc, Utils, YT } from 'youtubei.js' import Autolinker from 'autolinker' import { join } from 'path' @@ -203,19 +203,33 @@ export async function getLocalChannel(id) { } export async function getLocalChannelVideos(id) { - const channel = await getLocalChannel(id) - - if (channel.alert) { - return null - } + const innertube = await createInnertube() - if (!channel.has_videos) { - return [] - } + try { + const response = await innertube.actions.execute('/browse', { + browseId: id, + params: 'EgZ2aWRlb3PyBgQKAjoA' + // protobuf for the videos tab (this is the one that YouTube uses, + // it has some empty fields in the protobuf but it doesn't work if you remove them) + }) - const videosTab = await channel.getVideos() + const videosTab = new YT.Channel(null, response) - return parseLocalChannelVideos(videosTab.videos, channel.header.author) + // if the channel doesn't have a videos tab, YouTube returns the home tab instead + // so we need to check that we got the right tab + if (videosTab.current_tab?.endpoint.metadata.url?.endsWith('/videos')) { + return parseLocalChannelVideos(videosTab.videos, videosTab.header.author) + } else { + return [] + } + } catch (error) { + console.error(error) + if (error instanceof Utils.ChannelError) { + return null + } else { + throw error + } + } } /**