parseAlbumHeader function

JsonMap parseAlbumHeader(
  1. JsonMap response
)

Parse the information in an album header.

Implementation

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

  if (header.containsKey('description')) {
    album['description'] =
        (((header['description'] as JsonMap)['runs'] as List)[0]
            as JsonMap)['text'];
  }

  final albumInfo = parseSongRuns(
    ((header['subtitle'] as JsonMap)['runs'] as List).sublist(2),
  );
  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 menu = nav(header, MENU) as JsonMap;
  final toplevel = menu['topLevelButtons'];
  album['audioPlaylistId'] = nav(toplevel, [
    0,
    'buttonRenderer',
    ...NAVIGATION_WATCH_PLAYLIST_ID,
  ], nullIfAbsent: true);
  if (album['audioPlaylistId'] == null) {
    album['audioPlaylistId'] = nav(toplevel, [
      0,
      'buttonRenderer',
      ...NAVIGATION_PLAYLIST_ID,
    ], nullIfAbsent: true);
  }
  final service = nav(toplevel, [
    1,
    'buttonRenderer',
    'defaultServiceEndpoint',
  ], nullIfAbsent: true);
  if (service != null) {
    album['likeStatus'] = parseLikeStatus(service as JsonMap);
  }

  return album;
}