From 9529d6c48601f46ccfecafb471e1f3506470468c Mon Sep 17 00:00:00 2001 From: absidue <48293849+absidue@users.noreply.github.com> Date: Sat, 28 Jan 2023 19:41:17 +0100 Subject: [PATCH 1/2] Allow checking whether a channel has optional tabs --- src/core/TabbedFeed.ts | 4 ++++ src/parser/youtube/Channel.ts | 16 ++++++++++++++++ test/constants.ts | 4 ++++ test/main.test.ts | 8 ++++++++ 4 files changed, 32 insertions(+) diff --git a/src/core/TabbedFeed.ts b/src/core/TabbedFeed.ts index 80a59fff2..78cf62940 100644 --- a/src/core/TabbedFeed.ts +++ b/src/core/TabbedFeed.ts @@ -47,6 +47,10 @@ class TabbedFeed extends Feed { return new TabbedFeed(this.#actions, response.data, false); } + hasTabWithURL(url: string): boolean { + return this.#tabs.some((tab) => tab.endpoint.metadata.url?.split('/').pop() === url); + } + get title(): string | undefined { return this.page.contents_memo.getType(Tab)?.find((tab) => tab.selected)?.title.toString(); } diff --git a/src/parser/youtube/Channel.ts b/src/parser/youtube/Channel.ts index 7d1b3ab5a..16516e63e 100644 --- a/src/parser/youtube/Channel.ts +++ b/src/parser/youtube/Channel.ts @@ -196,6 +196,22 @@ export default class Channel extends TabbedFeed { return new Channel(this.actions, page, true); } + get has_videos(): boolean { + return this.hasTabWithURL('videos'); + } + + get has_shorts(): boolean { + return this.hasTabWithURL('shorts'); + } + + get has_live_streams(): boolean { + return this.hasTabWithURL('streams'); + } + + get has_community(): boolean { + return this.hasTabWithURL('community'); + } + /** * Retrives list continuation. */ diff --git a/test/constants.ts b/test/constants.ts index 4e138b622..c7706265a 100644 --- a/test/constants.ts +++ b/test/constants.ts @@ -37,5 +37,9 @@ export const CHANNELS = [ { ID: 'UCpbpfcZfo-hoDAx2m1blFhg', NAME: 'Learning Blocks' + }, + { + ID: 'UCt-oJR5teQIjOAxCmIQvcgA', + NAME: "They're Just Movies" } ]; \ No newline at end of file diff --git a/test/main.test.ts b/test/main.test.ts index bc1d41e58..f347bbcab 100644 --- a/test/main.test.ts +++ b/test/main.test.ts @@ -163,6 +163,14 @@ describe('YouTube.js Tests', () => { expect(filtered_list.videos.length).toBeGreaterThan(0); }); + it('should detect missing channel tabs', async () => { + const channel = await yt.getChannel(CHANNELS[2].ID); + expect(channel.has_videos).toBe(true); + expect(channel.has_shorts).toBe(false); + expect(channel.has_live_streams).toBe(false); + expect(channel.has_community).toBe(true); + }) + it('should retrieve home feed', async () => { const homefeed = await yt.getHomeFeed(); expect(homefeed.header).toBeDefined(); From f4d6bf41255004e863cd68e3dbe50823aa9a5a32 Mon Sep 17 00:00:00 2001 From: absidue <48293849+absidue@users.noreply.github.com> Date: Sun, 29 Jan 2023 12:03:14 +0100 Subject: [PATCH 2/2] Add getter for the playlists tab --- src/parser/youtube/Channel.ts | 4 ++++ test/main.test.ts | 1 + 2 files changed, 5 insertions(+) diff --git a/src/parser/youtube/Channel.ts b/src/parser/youtube/Channel.ts index 16516e63e..9057b0bdb 100644 --- a/src/parser/youtube/Channel.ts +++ b/src/parser/youtube/Channel.ts @@ -208,6 +208,10 @@ export default class Channel extends TabbedFeed { return this.hasTabWithURL('streams'); } + get has_playlists(): boolean { + return this.hasTabWithURL('playlists'); + } + get has_community(): boolean { return this.hasTabWithURL('community'); } diff --git a/test/main.test.ts b/test/main.test.ts index f347bbcab..b25e8edf3 100644 --- a/test/main.test.ts +++ b/test/main.test.ts @@ -168,6 +168,7 @@ describe('YouTube.js Tests', () => { expect(channel.has_videos).toBe(true); expect(channel.has_shorts).toBe(false); expect(channel.has_live_streams).toBe(false); + expect(channel.has_playlists).toBe(true); expect(channel.has_community).toBe(true); })