From ac065948eb817aa51eb4cbfcf1493a9142554eae Mon Sep 17 00:00:00 2001 From: onion chunky <78101139+ChunkyProgrammer@users.noreply.github.com> Date: Wed, 14 Sep 2022 06:53:17 -0400 Subject: [PATCH] add findTab method, find by selected --- app/fetchers/playlist.js | 6 +++--- app/helper.js | 16 ++++++++++------ app/youtube-grabber.js | 28 +++++++--------------------- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/app/fetchers/playlist.js b/app/fetchers/playlist.js index 7002947..cfa1e61 100644 --- a/app/fetchers/playlist.js +++ b/app/fetchers/playlist.js @@ -1,3 +1,4 @@ +const YoutubeGrabberHelper = require('../helper') const helper = require('../helper') class PlaylistFetcher { @@ -45,9 +46,8 @@ class PlaylistFetcher { channelUrl: `https://www.youtube.com/channel/${channelId}` } let playlistData - const playlistTab = channelPageDataResponse.contents.twoColumnBrowseResultsRenderer.tabs.filter(e => { - return e.tabRenderer !== undefined && e.tabRenderer.title === 'Playlists' - })[0] + const playlistTab = YoutubeGrabberHelper.findTab(channelPageDataResponse.contents.twoColumnBrowseResultsRenderer.tabs) + if (playlistTab !== undefined) { const tabRenderer = playlistTab.tabRenderer playlistData = tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer.contents[0].gridRenderer diff --git a/app/helper.js b/app/helper.js index 40ea062..df800cc 100644 --- a/app/helper.js +++ b/app/helper.js @@ -70,9 +70,8 @@ class YoutubeGrabberHelper { channelMetaData = channelPageDataResponse.metadata.channelMetadataRenderer channelName = channelMetaData.title } - const videoTab = channelPageDataResponse.contents.twoColumnBrowseResultsRenderer.tabs.filter(e => { - return e.tabRenderer !== undefined && e.tabRenderer.title === 'Videos' - })[0] + const videoTab = YoutubeGrabberHelper.findTab(channelPageDataResponse.contents.twoColumnBrowseResultsRenderer.tabs) + let channelVideoData if (videoTab !== undefined) { channelVideoData = channelPageDataResponse.contents.twoColumnBrowseResultsRenderer.tabs[1].tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer.contents[0].gridRenderer @@ -307,9 +306,8 @@ class YoutubeGrabberHelper { alertMessage: contentDataJSON.alerts[0].alertRenderer.text.simpleText } } - const communityTab = contentDataJSON.contents.twoColumnBrowseResultsRenderer.tabs.filter(e => { - return e.tabRenderer !== undefined && e.tabRenderer.title === 'Community' - })[0] + const communityTab = YoutubeGrabberHelper.findTab(contentDataJSON.contents.twoColumnBrowseResultsRenderer.tabs) + if (communityTab) { contentDataJSON = contentDataJSON.contents.twoColumnBrowseResultsRenderer.tabs[3].tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer if ('continuationItemRenderer' in contentDataJSON.contents[contentDataJSON.contents.length - 1]) { @@ -689,6 +687,12 @@ class YoutubeGrabberHelper { return { response: channelPageResponse, channelIdType: 3 } } + static findTab(tab) { + return tab.find((data) => + data?.tabRenderer?.selected === true + ) + } + static create(httpsAgent) { return new YoutubeGrabberHelper(httpsAgent) } diff --git a/app/youtube-grabber.js b/app/youtube-grabber.js index 34d6a95..59aa5df 100644 --- a/app/youtube-grabber.js +++ b/app/youtube-grabber.js @@ -65,16 +65,10 @@ class YoutubeGrabber { .filter(tab => tab.tabRenderer !== undefined && tab.tabRenderer !== null) .map(tab => tab.tabRenderer.title) - const channelsTab = headerTabs.filter((data) => { - if (typeof data.tabRenderer !== 'undefined') { - return data.tabRenderer.title === 'Channels' - } - - return false - }) + const channelsTab = YoutubeGrabberHelper.findTab(headerTabs) let featuredChannels = {} - if (channelsTab.length > 0) { - featuredChannels = channelsTab[0].tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer.contents[0] + if (channelsTab !== undefined) { + featuredChannels = channelsTab.tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer.contents[0] } let relatedChannels = [] let relatedChannelsContinuation = null @@ -477,12 +471,8 @@ class YoutubeGrabber { } } const headerTabs = channelPageDataResponse.contents.twoColumnBrowseResultsRenderer.tabs - const aboutTab = headerTabs.filter((data) => { - if (typeof data.tabRenderer !== 'undefined') { - return data.tabRenderer.title === 'About' - } - return false - })[0] + const aboutTab = YoutubeGrabberHelper.findTab(headerTabs) + let views = '0' let location = 'unknown' let joined = null @@ -539,13 +529,9 @@ class YoutubeGrabber { channelName: channelName, channelUrl: channelUrl } - const homeTab = headerTabs.filter((data) => { - if (typeof data.tabRenderer !== 'undefined') { - return data.tabRenderer.title === 'Home' - } - return false - })[0] + const homeTab = YoutubeGrabberHelper.findTab(headerTabs) + let featuredVideo = null let homeItems = [] if (homeTab !== undefined) {