Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow checking whether a channel has optional tabs #296

Merged
merged 2 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/TabbedFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
20 changes: 20 additions & 0 deletions src/parser/youtube/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@ 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_playlists(): boolean {
return this.hasTabWithURL('playlists');
}

get has_community(): boolean {
return this.hasTabWithURL('community');
}

/**
* Retrives list continuation.
*/
Expand Down
4 changes: 4 additions & 0 deletions test/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@ export const CHANNELS = [
{
ID: 'UCpbpfcZfo-hoDAx2m1blFhg',
NAME: 'Learning Blocks'
},
{
ID: 'UCt-oJR5teQIjOAxCmIQvcgA',
NAME: "They're Just Movies"
}
];
9 changes: 9 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ 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_playlists).toBe(true);
expect(channel.has_community).toBe(true);
})

it('should retrieve home feed', async () => {
const homefeed = await yt.getHomeFeed();
expect(homefeed.header).toBeDefined();
Expand Down