parseAlbumHeader2024 function

JsonMap parseAlbumHeader2024(
  1. JsonMap response
)

Parse the information in an album header in 2025 format.

Implementation

JsonMap parseAlbumHeader2024(JsonMap response) {
  final header =
      nav(response, [
            ...TWO_COLUMN_RENDERER,
            ...TAB_CONTENT,
            ...SECTION_LIST_ITEM,
            ...RESPONSIVE_HEADER,
          ])
          as JsonMap;

  final album = <String, dynamic>{
    'title': nav(header, TITLE_TEXT),
    'type': nav(header, SUBTITLE),
    'thumbnails': nav(header, THUMBNAILS),
    'isExplicit': nav(header, SUBTITLE_BADGE_LABEL, nullIfAbsent: true) != null,
  };

  final (description, descriptionRuns) = parseDescriptionRuns(
    nav(header, [
      'description',
      ...DESCRIPTION_SHELF,
      ...DESCRIPTION_RUN_LIST,
    ], nullIfAbsent: true),
  );

  album['description'] = description;
  album['descriptionRuns'] = descriptionRuns;

  final albumInfo = parseSongRuns(
    ((header['subtitle'] as JsonMap)['runs'] as List).sublist(2),
  );

  final straplineRuns = nav(header, [
    'straplineTextOne',
    'runs',
  ], nullIfAbsent: true);

  albumInfo['artists'] =
      straplineRuns != null ? parseArtistsRuns(straplineRuns as List) : null;

  album.addAll(albumInfo);

  if (((header['secondSubtitle'] as JsonMap)['runs'] as List).length > 1) {
    album['trackCount'] = toInt(
      (((header['secondSubtitle'] as JsonMap)['runs'] as List)[0]
              as JsonMap)['text']
          as String,
    );
    album['duration'] =
        (((header['secondSubtitle'] as JsonMap)['runs'] as List)[2]
            as JsonMap)['text'];
  } else {
    album['duration'] =
        (((header['secondSubtitle'] as JsonMap)['runs'] as List)[0]
            as JsonMap)['text'];
  }

  // add to library/uploaded
  final buttons = header['buttons'];
  album['audioPlaylistId'] = nav(
    findObjectByKey(buttons as List, 'musicPlayButtonRenderer'),
    ['musicPlayButtonRenderer', 'playNavigationEndpoint', ...WATCH_PID],
    nullIfAbsent: true,
  );

  // remove this once A/B testing is finished and it is no longer covered
  if (album['audioPlaylistId'] == null) {
    album['audioPlaylistId'] = nav(
      findObjectByKey(buttons, 'musicPlayButtonRenderer'),
      [
        'musicPlayButtonRenderer',
        'playNavigationEndpoint',
        ...WATCH_PLAYLIST_ID,
      ],
      nullIfAbsent: true,
    );
  }

  final service = nav(findObjectByKey(buttons, 'toggleButtonRenderer'), [
    'toggleButtonRenderer',
    'defaultServiceEndpoint',
  ], nullIfAbsent: true);

  album['likeStatus'] = 'INDIFFERENT';
  if (service != null) {
    album['likeStatus'] = parseLikeStatus(service as JsonMap);
  }

  return album;
}