-
-
Notifications
You must be signed in to change notification settings - Fork 504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GetVideosAsync fix #821
GetVideosAsync fix #821
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,4 +189,18 @@ public async Task I_can_get_a_subset_of_videos_included_in_a_playlist() | |
// Assert | ||
videos.Should().HaveCount(10); | ||
} | ||
|
||
[Fact] | ||
public async Task I_can_get_videos_included_in_a_playlist_with_a_lot_of_duplicate() | ||
{ | ||
// Arrange | ||
var youtube = new YoutubeClient(); | ||
|
||
// Act | ||
var videos = await youtube.Playlists.GetVideosAsync(PlaylistIds.EnormousDuplicates); | ||
|
||
// Assert | ||
videos.Should().HaveCountGreaterOrEqualTo(3_900); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also worth asserting that there are no duplicates |
||
videos.Should().OnlyHaveUniqueItems(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using YoutubeExplode.Bridge; | ||
using YoutubeExplode.Common; | ||
using YoutubeExplode.Exceptions; | ||
using YoutubeExplode.Videos; | ||
|
@@ -92,8 +93,20 @@ public async IAsyncEnumerable<Batch<PlaylistVideo>> GetVideoBatchesAsync( | |
); | ||
|
||
var videos = new List<PlaylistVideo>(); | ||
IEnumerable<PlaylistVideoData> originalVideos; | ||
if (!encounteredIds.Any()) | ||
{ | ||
originalVideos = response.Videos.DistinctBy(v => v.Id); | ||
} | ||
else | ||
{ | ||
originalVideos = response.Videos.Where(v => | ||
{ | ||
return !encounteredIds.Any(e => string.Equals(e.Value, v.Id)); | ||
}); | ||
} | ||
Comment on lines
+96
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think the old code and the new code work the same 🤔 Previously, we used a if (!encounteredIds.Add(videoId))
continue; // this will execute if the `videoId` already exists And now we use this instead: if (!encounteredIds.Any())
{
originalVideos = response.Videos.DistinctBy(v => v.Id);
}
else
{
originalVideos = response.Videos.Where(v =>
{
return !encounteredIds.Any(e => string.Equals(e.Value, v.Id));
});
} Which seems to do the same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can run it by yourself and see that my changes allow to download more videos. I can't explain by words, why this changes works better than code on master tree. My observation just showed that for some reasons, on some point (probably when last item is duplicate), it starts to return videos, that were already added to HashSet. |
||
|
||
foreach (var videoData in response.Videos) | ||
foreach (var videoData in originalVideos) | ||
{ | ||
var videoId = | ||
videoData.Id | ||
|
@@ -105,9 +118,7 @@ public async IAsyncEnumerable<Batch<PlaylistVideo>> GetVideoBatchesAsync( | |
videoData.Index | ||
?? throw new YoutubeExplodeException("Failed to extract the video index."); | ||
|
||
// Don't yield the same video twice | ||
if (!encounteredIds.Add(videoId)) | ||
continue; | ||
encounteredIds.Add(videoId); | ||
|
||
var videoTitle = | ||
videoData.Title | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.