From 470d8d94063f0159cd005c9eb15fd1a4a175bea0 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Tue, 28 Mar 2023 20:50:50 -0300 Subject: [PATCH] fix(PlayerCaptionsTracklist): parse props only if they exist in the node Fixes #372 --- src/parser/classes/PlayerCaptionsTracklist.ts | 68 +++++++++++-------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/src/parser/classes/PlayerCaptionsTracklist.ts b/src/parser/classes/PlayerCaptionsTracklist.ts index 4c9a194ac..0aedcaa77 100644 --- a/src/parser/classes/PlayerCaptionsTracklist.ts +++ b/src/parser/classes/PlayerCaptionsTracklist.ts @@ -1,10 +1,11 @@ import Text from './misc/Text.js'; import { YTNode } from '../helpers.js'; +import { RawNode } from '../index.js'; class PlayerCaptionsTracklist extends YTNode { static type = 'PlayerCaptionsTracklist'; - caption_tracks: { + caption_tracks?: { base_url: string; name: Text; vss_id: string; @@ -13,7 +14,7 @@ class PlayerCaptionsTracklist extends YTNode { is_translatable: boolean; }[]; - audio_tracks: { + audio_tracks?: { audio_track_id: string; captions_initial_state: string; default_caption_track_index: number; @@ -22,39 +23,48 @@ class PlayerCaptionsTracklist extends YTNode { caption_track_indices: number; }[]; - default_audio_track_index: number; + default_audio_track_index?: number; - translation_languages: { + translation_languages?: { language_code: string; language_name: Text; }[]; - constructor(data: any) { + constructor(data: RawNode) { super(); - this.caption_tracks = data.captionTracks.map((ct: any) => ({ - base_url: ct.baseUrl, - name: new Text(ct.name), - vss_id: ct.vssId, - language_code: ct.languageCode, - kind: ct.kind, - is_translatable: ct.isTranslatable - })); - - this.audio_tracks = data.audioTracks.map((at: any) => ({ - audio_track_id: at.audioTrackId, - captions_initial_state: at.captionsInitialState, - default_caption_track_index: at.defaultCaptionTrackIndex, - has_default_track: at.hasDefaultTrack, - visibility: at.visibility, - caption_track_indices: at.captionTrackIndices - })); - - this.default_audio_track_index = data.defaultAudioTrackIndex; - - this.translation_languages = data.translationLanguages.map((tl: any) => ({ - language_code: tl.languageCode, - language_name: new Text(tl.languageName) - })); + + if (Reflect.has(data, 'captionTracks')) { + this.caption_tracks = data.captionTracks.map((ct: any) => ({ + base_url: ct.baseUrl, + name: new Text(ct.name), + vss_id: ct.vssId, + language_code: ct.languageCode, + kind: ct.kind, + is_translatable: ct.isTranslatable + })); + } + + if (Reflect.has(data, 'audioTracks')) { + this.audio_tracks = data.audioTracks.map((at: any) => ({ + audio_track_id: at.audioTrackId, + captions_initial_state: at.captionsInitialState, + default_caption_track_index: at.defaultCaptionTrackIndex, + has_default_track: at.hasDefaultTrack, + visibility: at.visibility, + caption_track_indices: at.captionTrackIndices + })); + } + + if (Reflect.has(data, 'defaultAudioTrackIndex')) { + this.default_audio_track_index = data.defaultAudioTrackIndex; + } + + if (Reflect.has(data, 'translationLanguages')) { + this.translation_languages = data.translationLanguages.map((tl: any) => ({ + language_code: tl.languageCode, + language_name: new Text(tl.languageName) + })); + } } }