parsePlaylistVideo static method

VideoDetailed? parsePlaylistVideo(
  1. dynamic item
)

Implementation

static VideoDetailed? parsePlaylistVideo(dynamic item) {
  final flexColumns =
      traverseList(item, ['flexColumns', 'runs']).expand((e) => e).toList();
  final fixedColumns =
      traverseList(item, ['fixedColumns', 'runs']).expand((e) => e).toList();

  final title = flexColumns.firstWhere(isTitle,
      orElse: () => flexColumns.isNotEmpty ? flexColumns[0] : null);
  final artist = flexColumns.firstWhere(isArtist,
      orElse: () => flexColumns.length > 1 ? flexColumns[1] : null);
  final duration = fixedColumns.firstWhere(isDuration, orElse: () => null);

  final videoId1 =
      traverseString(item, ["playNavigationEndpoint", "videoId"]);
  final videoId2 = RegExp(r"https:\/\/i\.ytimg\.com\/vi\/(.+)\/")
      .firstMatch(traverseList(item, ["thumbnails"]).firstOrNull?.url ?? '')
      ?.group(1);

  if ((videoId1?.isEmpty ?? true) && videoId2 == null) {
    return null;
  }

  return VideoDetailed(
    type: "VIDEO",
    videoId: videoId1 ?? videoId2!,
    name: traverseString(title, ["text"]) ?? '',
    artist: ArtistBasic(
      name: traverseString(artist, ["text"]) ?? '',
      artistId: traverseString(artist, ["browseId"]),
    ),
    duration: Parser.parseDuration(duration?.text),
    thumbnails: traverseList(item, ["thumbnails"])
        .map((item) => ThumbnailFull.fromMap(item))
        .toList(),
  );
}