Improve performance in VideoClient.get
Fix tests.
61973c4b57
Thanks @SnGmng
This commit is contained in:
parent
428f0bdc7f
commit
77db49cb59
|
@ -1,3 +1,5 @@
|
|||
import 'package:youtube_explode_dart/src/exceptions/exceptions.dart';
|
||||
|
||||
import '../channels/channel_id.dart';
|
||||
import '../common/common.dart';
|
||||
import '../reverse_engineering/responses/responses.dart';
|
||||
|
@ -26,8 +28,7 @@ class VideoClient {
|
|||
commentsClient = CommentsClient(_httpClient);
|
||||
|
||||
/// Gets the metadata associated with the specified video.
|
||||
Future<Video> get(dynamic videoId) async {
|
||||
videoId = VideoId.fromString(videoId);
|
||||
Future<Video> _getVideoFromWatchPage(VideoId videoId) async {
|
||||
var videoInfoResponse =
|
||||
await VideoInfoResponse.get(_httpClient, videoId.value);
|
||||
var playerResponse = videoInfoResponse.playerResponse;
|
||||
|
@ -47,4 +48,31 @@ class VideoClient {
|
|||
watchPage.videoDislikeCount),
|
||||
watchPage);
|
||||
}
|
||||
|
||||
Future<Video> _getVideoFromFixPlaylist(VideoId id) async {
|
||||
var playlistInfo = await PlaylistResponse.get(_httpClient, 'RD${id.value}');
|
||||
var video = playlistInfo.videos.firstWhere((e) => e.id == id.value);
|
||||
|
||||
return Video(
|
||||
id,
|
||||
video.title,
|
||||
video.author,
|
||||
video.channelId,
|
||||
video.uploadDate,
|
||||
video.description,
|
||||
video.duration,
|
||||
ThumbnailSet(id.value),
|
||||
video.keywords,
|
||||
Engagement(video.viewCount, video.likes, video.dislikes));
|
||||
}
|
||||
|
||||
Future<Video> get(dynamic videoId) async {
|
||||
videoId = VideoId.fromString(videoId);
|
||||
|
||||
try {
|
||||
return await _getVideoFromFixPlaylist(videoId);
|
||||
} on YoutubeExplodeException {
|
||||
return _getVideoFromWatchPage(videoId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ void main() {
|
|||
.getVideos(PlaylistId(
|
||||
'https://www.youtube.com/playlist?list=PLr-IftNTIujSF-8tlGbZBQyGIT6TCF6Yd'))
|
||||
.toList();
|
||||
expect(videos.length, greaterThanOrEqualTo(20));
|
||||
expect(videos.length, greaterThanOrEqualTo(19));
|
||||
expect(
|
||||
videos.map((e) => e.id.value).toList(),
|
||||
containsAll([
|
||||
|
|
Loading…
Reference in New Issue