Skip to content
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

Fetch video count in playlist metadata #801

Merged
merged 6 commits into from
Jul 25, 2024
Merged

Conversation

IcySnex
Copy link
Contributor

@IcySnex IcySnex commented Jul 20, 2024

Closes #356
// Provided solution wasn't actually a solution. Just a workaround.


Added support for "vidoes count" property for playlist fetching:

Knowing the estimate videos count provided by the offical YouTube API without having to reiterate over the entire video collection is needed (especially when there are hundreds of videos in the playlist)

  • Added int VideosCount to Playlist
  • Added int? VideosCount to IPlaylistData, PlaylistBrowseResponse, PlaylistNextResponse
  • Updated PlaylistSpecs-Tests

@Tyrrrz
Copy link
Owner

Tyrrrz commented Jul 20, 2024

Thank you. I'm not sure I like that some playlists will have null count with this approach.

@IcySnex
Copy link
Contributor Author

IcySnex commented Jul 20, 2024

During my testing I didn't find any playlists where YouTube doesn't return the number of videos, but for safety reasons I made it nullable internally and set it to 0 in the PlaylistClient if it is null, similar to the Description property

@Tyrrrz
Copy link
Owner

Tyrrrz commented Jul 23, 2024

It looks like the PlaylistNext response parser is hard-codded to return null, and we use that primarily for dynamic playlists (such as mixes) and other playlists that we can't fetch normally.

What video count does it return for the non-PL playlists from this list?

public const string Normal = "PLI5YfMzCfRtZ8eV576YoY3vIYrHjyVm_e";
public const string Private = "PLYjTMWc3sa4ZKheRwyA1q56xxQrfQEUBr";
public const string NonExisting = "PLYjTMWc3sa4ZKheRwyA1q56xxQrfQEUBx";
public const string Large = "PLWwAypAcFRgKFlxtLbn_u14zddtDJj3mk";
public const string VideoMix = "RDTsYhxMnGYCw";
public const string MusicMix = "RDCLAK5uy_lf8okgl2ygD075nhnJVjlfhwp8NsUgEbs";
public const string MusicAlbum = "OLAK5uy_lLeonUugocG5J0EUAEDmbskX4emejKwcM";
public const string UserUploads = "UUTMt7iMWa7jy0fNXIktwyLA";
public const string Weird = "PL601B2E69B03FAB9D";
public const string ContainsLongVideos = "PLkk2FsMngwGi9FNkWIoNZlfqglcldj_Zs";

@IcySnex
Copy link
Contributor Author

IcySnex commented Jul 24, 2024

I have implemented the parsing of the video count for the PlaylistNextResponse. I have tested the non-PL playlists with this and
most of them work just fine but there are minor issues with PlaylistIds.VideoMix & PlaylistIds.MusicMix.

I have inspected the YouTube API response and I have noticed that the ContentRoot also provides a property called isInfinite. If this property is true (which is the case for these Mix-Playlists) then YouTube won't tell the actual video count.

Thus I have added the IsInfinite-property to the PlaylistNextResponse. But I'm not quite sure what to do with the VideoCount-property in this case. Currently It will be set to int.MaxValue but I'm not quite sure about this approach:

[Lazy]
public bool? IsInfinite => ContentRoot?.GetPropertyOrNull("isInfinite")?.GetBoolean();
public int? VideosCount =>
IsInfinite == true
? int.MaxValue
: ContentRoot
?.GetPropertyOrNull("totalVideosText")
?.GetPropertyOrNull("runs")
?.EnumerateArrayOrNull()
?.FirstOrNull()
?.GetPropertyOrNull("text")
?.GetStringOrNull()
?.ParseIntOrNull()
?? ContentRoot
?.GetPropertyOrNull("videoCountText")
?.GetPropertyOrNull("runs")
?.EnumerateArrayOrNull()
?.ElementAtOrNull(2)
?.GetPropertyOrNull("text")
?.GetStringOrNull()
?.ParseIntOrNull();

@Tyrrrz
Copy link
Owner

Tyrrrz commented Jul 24, 2024

Let's make the VideoCount property nullable and have null indicate an infinite playlist (explain so in the <remarks> block).

Playlist.VideoCount may be null in case of playlists with infinite videos (e.g. mixes).
@IcySnex
Copy link
Contributor Author

IcySnex commented Jul 25, 2024

Alright that sounds good. I have implemented it and updated the tests.

@Tyrrrz Tyrrrz changed the title Introduced videos count for playlist fetching Fetch video count in playlist metadata Jul 25, 2024
@Tyrrrz Tyrrrz merged commit d4b1422 into Tyrrrz:master Jul 25, 2024
7 checks passed
@Tyrrrz
Copy link
Owner

Tyrrrz commented Jul 25, 2024

Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How do you get the number of videos in playlist?
2 participants