getVideoComments method

Future<YoutubeVideoComments> getVideoComments({
  1. required String video_id,
})

Implementation

Future<googleapis_client_scheme.YoutubeVideoComments> getVideoComments({
  required String video_id,
}) async {
  googleapis_client_scheme.YoutubeSchemaText youtubeSchemaText =
      GoogleApisClientUtils.parseTextToYoutube(text: video_id);
  if (youtubeSchemaText["@type"] == "error") {
    return googleapis_client_scheme.YoutubeVideoComments(
        youtubeSchemaText.rawData);
  }

  if (youtubeSchemaText["type"] == "channel_username") {
    return googleapis_client_scheme.YoutubeVideoComments(
        youtubeSchemaText.rawData);
  }

  Video video = await youtubeExplode.videos.get(youtubeSchemaText.data);

  CommentsList? commentsList =
      await youtubeExplode.videos.comments.getComments(video);
  if (commentsList == null) {
    Map jsonDataVideo = {
      "@type": "youtubeVideoComments",
      "count": 0,
      "comments": []
    };
    return googleapis_client_scheme.YoutubeVideoComments(jsonDataVideo);
  }
  Map jsonDataVideo = {
    "@type": "youtubeVideoComments",
    "count": commentsList.totalLength,
    "comments": commentsList.toList().map((Comment comment) {
      Map jsonDataVideoComment = {
        "@type": "youtubeVideoComment",
        "author": comment.author,
        "channel_id": comment.channelId.value,
        "date": comment.publishedTime,
        "is_hearted": comment.isHearted,
        "like_count": comment.likeCount,
        "reply_count": comment.replyCount,
        "text": comment.text,
      };

      return jsonDataVideoComment;
    }).toList(),
  };

  return googleapis_client_scheme.YoutubeVideoComments(jsonDataVideo);
}