Skip to content

Commit

Permalink
Fetch the videos tab directly for local API subscriptions (#3315)
Browse files Browse the repository at this point in the history
  • Loading branch information
absidue authored Mar 18, 2023
1 parent 304152f commit 029f0cb
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/renderer/helpers/api/local.js
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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
}
}
}

/**
Expand Down

0 comments on commit 029f0cb

Please sign in to comment.