2021-07-22 15:03:07 +02:00
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
2020-06-21 16:23:19 +02:00
|
|
|
|
|
|
|
import '../../channels/channel_id.dart';
|
2021-07-22 15:03:07 +02:00
|
|
|
import '../../reverse_engineering/responses/comments_client.dart' as re;
|
2020-06-21 16:23:19 +02:00
|
|
|
import '../../reverse_engineering/youtube_http_client.dart';
|
|
|
|
import '../videos.dart';
|
|
|
|
import 'comment.dart';
|
|
|
|
|
|
|
|
/// Queries related to comments of YouTube videos.
|
2021-07-22 15:03:07 +02:00
|
|
|
@experimental
|
2020-06-21 16:23:19 +02:00
|
|
|
class CommentsClient {
|
|
|
|
final YoutubeHttpClient _httpClient;
|
|
|
|
|
|
|
|
/// Initializes an instance of [CommentsClient]
|
|
|
|
CommentsClient(this._httpClient);
|
|
|
|
|
|
|
|
/// Returns a stream emitting all the [video]'s comment.
|
|
|
|
/// A request is page for every comment page,
|
|
|
|
/// a page contains at most 20 comments, use .take if you want to limit
|
|
|
|
/// the results.
|
|
|
|
///
|
2020-07-16 19:28:49 +02:00
|
|
|
/// The streams doesn't emit any data if [Video.hasWatchPage] is false.
|
2020-09-21 17:34:03 +02:00
|
|
|
/// Use `videos.get(videoId, forceWatchPage: true)` to assure that the
|
|
|
|
/// WatchPage is fetched.
|
2021-07-22 15:03:07 +02:00
|
|
|
Future<List<Comment>> getComments(Video video) async {
|
2020-06-21 16:23:19 +02:00
|
|
|
if (video.watchPage == null) {
|
2021-07-22 15:03:07 +02:00
|
|
|
return const [];
|
2020-06-21 16:23:19 +02:00
|
|
|
}
|
2021-06-24 15:23:35 +02:00
|
|
|
|
2021-07-22 15:03:07 +02:00
|
|
|
final page = await re.CommentsClient.get(_httpClient, video);
|
|
|
|
|
|
|
|
return page?.comments
|
|
|
|
.map((e) => Comment(
|
|
|
|
e.author,
|
|
|
|
ChannelId(e.channelId),
|
|
|
|
e.text,
|
|
|
|
e.likeCount ?? 0,
|
|
|
|
e.publishTime,
|
|
|
|
e.repliesCount ?? 0,
|
|
|
|
e.continuation))
|
|
|
|
.toList(growable: false) ??
|
|
|
|
const [];
|
2020-06-21 16:23:19 +02:00
|
|
|
}
|
|
|
|
}
|