youtube_explode/lib/src/videos/comments/comments_client.dart

72 lines
1.9 KiB
Dart
Raw Normal View History

import '../../channels/channel_id.dart';
2021-07-25 14:47:26 +02:00
import '../../reverse_engineering/clients/comments_client.dart' as re;
import '../../reverse_engineering/youtube_http_client.dart';
import '../videos.dart';
/// Queries related to comments of YouTube videos.
class CommentsClient {
final YoutubeHttpClient _httpClient;
/// Initializes an instance of [CommentsClient]
CommentsClient(this._httpClient);
/// Returns a [List<Comment>] containing the first batch of comments.
/// You can use [CommentsList.nextPage()] to get the next batch of comments.
Future<CommentsList?> getComments(Video video) async {
if (video.watchPage == null) {
return null;
}
2021-06-24 15:23:35 +02:00
final page = await re.CommentsClient.get(_httpClient, video);
if (page == null) {
return null;
}
return CommentsList(
page.comments
.map((e) => Comment(
e.author,
ChannelId(e.channelId),
e.text,
e.likeCount ?? 0,
e.publishTime,
e.repliesCount ?? 0,
2021-08-28 11:47:01 +02:00
e.isHearted,
e.continuation))
.toList(growable: false),
2021-08-28 11:47:01 +02:00
page.getCommentsCount(),
page,
_httpClient);
}
Future<CommentsList?> getReplies(Comment comment) async {
if (comment.continuation == null) {
return null;
}
final page =
await re.CommentsClient.getReplies(_httpClient, comment.continuation!);
if (page == null) {
return null;
}
return CommentsList(
page.comments
.map((e) => Comment(
e.author,
ChannelId(e.channelId),
e.text,
e.likeCount ?? 0,
e.publishTime,
e.repliesCount ?? 0,
2021-08-28 11:47:01 +02:00
e.isHearted,
e.continuation))
.toList(growable: false),
2021-08-28 11:47:01 +02:00
0,
page,
_httpClient);
}
}