parseChannelContents method

JsonMap parseChannelContents(
  1. List<JsonMap> results
)

Parses content from a channel.

Implementation

JsonMap parseChannelContents(List<JsonMap> results) {
  final categories = [
    ['albums', t('albums'), parseAlbum, 'MTRIR'],
    ['singles', t('singles & eps'), parseSingle, 'MTRIR'],
    ['shows', t('shows'), parseAlbum, 'MTRIR'],
    ['videos', t('videos'), parseVideo, 'MTRIR'],
    ['playlists', t('playlists'), parsePlaylist, 'MTRIR'],
    ['related', t('related'), parseRelatedArtist, 'MTRIR'],
    ['episodes', t('episodes'), parseEpisode, 'MMRIR'],
    ['podcasts', t('podcasts'), parsePodcast, 'MTRIR'],
  ];

  final artist = <String, dynamic>{};

  for (final category in categories) {
    final categoryKey = category[0] as String;
    final categoryLocal = category[1] as String;
    final categoryParser = category[2] as JsonMap Function(JsonMap);
    final categoryNavKey = category[3] as String;

    final data =
        List<JsonMap>.from(
          results.where(
            (r) =>
                r.containsKey('musicCarouselShelfRenderer') &&
                ((nav(r, CAROUSEL + CAROUSEL_TITLE) as JsonMap)['text']
                            as String)
                        .toLowerCase() ==
                    categoryLocal.toLowerCase(),
          ),
        ).map((r) => r['musicCarouselShelfRenderer'] as JsonMap).toList();

    if (data.isNotEmpty) {
      artist[categoryKey] = JsonMap.from({'browseId': null, 'results': []});

      final navTitle = nav(data[0], CAROUSEL_TITLE) as JsonMap;
      if (navTitle.containsKey('navigationEndpoint')) {
        (artist[categoryKey] as JsonMap)['browseId'] = nav(
          data[0],
          CAROUSEL_TITLE + NAVIGATION_BROWSE_ID,
        );
        (artist[categoryKey] as JsonMap)['params'] = nav(
          data[0],
          CAROUSEL_TITLE + NAVIGATION_BROWSE + ['params'],
          nullIfAbsent: true,
        );
      }

      (artist[categoryKey] as JsonMap)['results'] = parseContentList(
        data[0]['contents'],
        categoryParser,
        key: categoryNavKey,
      );
    }
  }

  return artist;
}