Skip to content

Commit

Permalink
fix(PlayerCaptionsTracklist): parse props only if they exist in the node
Browse files Browse the repository at this point in the history
Fixes #372
  • Loading branch information
LuanRT committed Mar 28, 2023
1 parent 2c5907f commit 470d8d9
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions src/parser/classes/PlayerCaptionsTracklist.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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)
}));
}
}
}

Expand Down

0 comments on commit 470d8d9

Please sign in to comment.