Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
add findTab method, find by selected
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkyProgrammer committed Sep 14, 2022
1 parent 5da8f00 commit ac06594
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
6 changes: 3 additions & 3 deletions app/fetchers/playlist.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const YoutubeGrabberHelper = require('../helper')
const helper = require('../helper')

class PlaylistFetcher {
Expand Down Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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)
}
Expand Down
28 changes: 7 additions & 21 deletions app/youtube-grabber.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ac06594

Please sign in to comment.