Skip to content

Commit

Permalink
refactor(Playlist): Ignore ContinuationItem nodes from `SectionList…
Browse files Browse the repository at this point in the history
…#contents` (#579)

* feat(PlaylistVideo): Add `style`

* refactor(Playlist): Ignore `ContinuationItem` nodes in `SectionList#contents`

This should fix some issues regarding the library fetching the wrong continuation or empty continuations (NOTE: This means the solution in 987f506 no longer applies as empty continuations were all in `SectionList#contents`).
  • Loading branch information
LuanRT authored Jan 18, 2024
1 parent 6082b4a commit 04d55d0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/parser/classes/PlaylistVideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class PlaylistVideo extends YTNode {
upcoming?: Date;
video_info: Text;
accessibility_label?: string;
style?: string;

duration: {
text: string;
Expand All @@ -44,6 +45,10 @@ export default class PlaylistVideo extends YTNode {
this.video_info = new Text(data.videoInfo);
this.accessibility_label = data.title.accessibility.accessibilityData.label;

if (Reflect.has(data, 'style')) {
this.style = data.style;
}

const upcoming = data.upcomingEventData && Number(`${data.upcomingEventData.startTime}000`);
if (upcoming) {
this.upcoming = new Date(upcoming);
Expand Down
39 changes: 36 additions & 3 deletions src/parser/youtube/Playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import PlaylistSidebarSecondaryInfo from '../classes/PlaylistSidebarSecondaryInf
import PlaylistVideoThumbnail from '../classes/PlaylistVideoThumbnail.js';
import VideoOwner from '../classes/VideoOwner.js';
import Alert from '../classes/Alert.js';
import ContinuationItem from '../classes/ContinuationItem.js';
import PlaylistVideo from '../classes/PlaylistVideo.js';
import SectionList from '../classes/SectionList.js';

import { InnertubeError } from '../../utils/Utils.js';
import type { ObservedArray } from '../helpers.js';
import { observe, type ObservedArray } from '../helpers.js';

import type Actions from '../../core/Actions.js';
import type { ApiResponse } from '../../core/Actions.js';
Expand Down Expand Up @@ -64,8 +67,38 @@ export default class Playlist extends Feed<IBrowseResponse> {
return primary_info.stats[index]?.toString() || 'N/A';
}

get items() {
return this.videos;
get items(): ObservedArray<PlaylistVideo> {
return observe(this.videos.as(PlaylistVideo).filter((video) => video.style !== 'PLAYLIST_VIDEO_RENDERER_STYLE_RECOMMENDED_VIDEO'));
}

get has_continuation() {
const section_list = this.memo.getType(SectionList).first();

if (!section_list)
return super.has_continuation;

return !!this.memo.getType(ContinuationItem).find((node) => !section_list.contents.includes(node));
}

async getContinuationData(): Promise<IBrowseResponse | undefined> {
const section_list = this.memo.getType(SectionList).first();

/**
* No section list means there can't be additional continuation nodes here,
* so no need to check.
*/
if (!section_list)
return await super.getContinuationData();

const playlist_contents_continuation = this.memo.getType(ContinuationItem)
.find((node) => !section_list.contents.includes(node));

if (!playlist_contents_continuation)
throw new InnertubeError('There are no continuations.');

const response = await playlist_contents_continuation.endpoint.call<IBrowseResponse>(this.actions, { parse: true });

return response;
}

async getContinuation(): Promise<Playlist> {
Expand Down

0 comments on commit 04d55d0

Please sign in to comment.