youtube_explode/lib/src/playlists/playlist_client.dart

70 lines
1.9 KiB
Dart
Raw Normal View History

2021-09-28 16:49:38 +02:00
import '../channels/channel_id.dart';
2020-06-03 23:02:21 +02:00
import '../common/common.dart';
2021-09-28 16:49:38 +02:00
import '../reverse_engineering/pages/playlist_page.dart';
2020-06-03 23:02:21 +02:00
import '../reverse_engineering/youtube_http_client.dart';
import '../videos/video.dart';
import '../videos/video_id.dart';
import 'playlist.dart';
import 'playlist_id.dart';
/// Queries related to YouTube playlists.
class PlaylistClient {
final YoutubeHttpClient _httpClient;
/// Initializes an instance of [PlaylistClient]
PlaylistClient(this._httpClient);
/// Gets the metadata associated with the specified playlist.
2020-06-05 20:08:04 +02:00
Future<Playlist> get(dynamic id) async {
id = PlaylistId.fromString(id);
2022-02-03 12:06:09 +01:00
var response = await PlaylistPage.get(_httpClient, (id as PlaylistId).value);
2020-06-03 23:02:21 +02:00
return Playlist(
id,
2021-07-21 02:06:02 +02:00
response.title ?? '',
response.author ?? '',
response.description ?? '',
2021-03-04 10:46:37 +01:00
ThumbnailSet(id.value),
2021-08-29 13:26:21 +02:00
Engagement(response.viewCount ?? 0, null, null),
response.videoCount);
2020-06-03 23:02:21 +02:00
}
/// Enumerates videos included in the specified playlist.
2020-06-05 20:08:04 +02:00
Stream<Video> getVideos(dynamic id) async* {
id = PlaylistId.fromString(id);
2021-07-21 02:06:02 +02:00
final encounteredVideoIds = <String>{};
2021-03-04 10:46:37 +01:00
2021-07-21 02:06:02 +02:00
PlaylistPage? page = await PlaylistPage.get(_httpClient, id.value);
2021-03-04 10:46:37 +01:00
2021-07-21 02:06:02 +02:00
while (page != null) {
for (final video in page.videos) {
2020-06-03 23:02:21 +02:00
var videoId = video.id;
// Already added
2020-06-05 16:17:08 +02:00
if (!encounteredVideoIds.add(videoId)) {
2020-06-03 23:02:21 +02:00
continue;
}
2021-03-04 10:46:37 +01:00
if (video.channelId.isEmpty) {
continue;
}
2020-06-03 23:02:21 +02:00
yield Video(
VideoId(videoId),
video.title,
video.author,
2021-03-04 10:46:37 +01:00
ChannelId(video.channelId),
null,
2021-03-24 06:17:49 +01:00
null,
2020-06-03 23:02:21 +02:00
video.description,
video.duration,
ThumbnailSet(videoId),
2021-03-04 10:46:37 +01:00
null,
Engagement(video.viewCount, null, null),
false);
2020-06-03 23:02:21 +02:00
}
page = await page.nextPage(_httpClient);
2020-06-03 23:02:21 +02:00
}
}
}