parseTopResult function

JsonMap parseTopResult(
  1. JsonMap data,
  2. List<String> searchResultTypes
)

Parses the top result from data.

Implementation

JsonMap parseTopResult(JsonMap data, List<String> searchResultTypes) {
  final resultType = getSearchResultType(
    nav(data, SUBTITLE) as String?,
    searchResultTypes,
  );
  final category =
      nav(data, CARD_SHELF_TITLE, nullIfAbsent: true) ?? 'Top result';
  final searchResult = <String, dynamic>{
    'category': category,
    'resultType': resultType,
  };

  if (resultType == 'artist') {
    final subscribers = nav(data, SUBTITLE2, nullIfAbsent: true) as String?;
    if (subscribers != null) {
      searchResult['subscribers'] = subscribers.split(' ')[0];
    }

    final artistInfo = parseSongRuns(nav(data, ['title', 'runs']) as List);
    searchResult.addAll(artistInfo);
  }

  if (resultType == 'song' || resultType == 'video') {
    final onTap = data['onTap'];
    if (onTap != null) {
      searchResult['videoId'] = nav(onTap, WATCH_VIDEO_ID);
      searchResult['videoType'] = nav(onTap, NAVIGATION_VIDEO_TYPE);
    }
  }

  if (['song', 'video', 'album'].contains(resultType)) {
    searchResult['videoId'] = nav(data, [
      'onTap',
      ...WATCH_VIDEO_ID,
    ], nullIfAbsent: true);
    searchResult['videoType'] = nav(data, [
      'onTap',
      ...NAVIGATION_VIDEO_TYPE,
    ], nullIfAbsent: true);
    searchResult['title'] = nav(data, TITLE_TEXT);

    final runs = nav(data, ['subtitle', 'runs']) as List;
    final songInfo = parseSongRuns(runs.sublist(2));
    searchResult.addAll(songInfo);
  }

  if (resultType == 'album') {
    searchResult['browseId'] = nav(
      data,
      TITLE + NAVIGATION_BROWSE_ID,
      nullIfAbsent: true,
    );
    final buttonCommand = nav(data, [
      'buttons',
      0,
      'buttonRenderer',
      'command',
    ], nullIfAbsent: true);
    searchResult['playlistId'] = parseAlbumPlaylistIdIfExists(
      buttonCommand as JsonMap?,
    );
  }

  if (resultType == 'playlist') {
    searchResult['playlistId'] = nav(data, MENU_PLAYLIST_ID);
    searchResult['title'] = nav(data, TITLE_TEXT);
    searchResult['author'] = parseSongArtistsRuns(
      (nav(data, ['subtitle', 'runs']) as List).sublist(2),
    );
  }

  if (resultType == 'episode') {
    searchResult['videoId'] = nav(data, [
      ...THUMBNAIL_OVERLAY_NAVIGATION,
      ...WATCH_VIDEO_ID,
    ]);
    searchResult['videoType'] = nav(data, [
      ...THUMBNAIL_OVERLAY_NAVIGATION,
      ...NAVIGATION_VIDEO_TYPE,
    ]);
    final runs = (nav(data, SUBTITLE_RUNS) as List).sublist(2);
    searchResult['date'] = (runs[0] as JsonMap)['text'];
    searchResult['podcast'] = parseIdName(runs[2] as JsonMap?);
  }

  searchResult['thumbnails'] = nav(data, THUMBNAILS, nullIfAbsent: true);
  return searchResult;
}