Skip to content

Commit

Permalink
Fetch video count in playlist metadata (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
IcySnex authored Jul 25, 2024
1 parent 9f1bffd commit d4b1422
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions YoutubeExplode.Tests/PlaylistSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public async Task I_can_get_the_metadata_of_a_playlist()
playlist
.Description.Should()
.Contain("Digital Analytics Fundamentals course on Analytics Academy");
playlist.VideosCount.Should().Be(22);
playlist.Thumbnails.Should().NotBeEmpty();
}

Expand Down
2 changes: 2 additions & 0 deletions YoutubeExplode/Bridge/IPlaylistData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ internal interface IPlaylistData

string? Description { get; }

int? VideosCount { get; }

IReadOnlyList<ThumbnailData> Thumbnails { get; }
}
22 changes: 22 additions & 0 deletions YoutubeExplode/Bridge/PlaylistBrowseResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ internal partial class PlaylistBrowseResponse(JsonElement content) : IPlaylistDa
?.GetPropertyOrNull("value")
?.GetStringOrNull();

[Lazy]
public int? VideosCount =>
SidebarPrimary
?.GetPropertyOrNull("stats")
?.EnumerateArrayOrNull()
?.FirstOrNull()
?.GetPropertyOrNull("runs")
?.EnumerateArrayOrNull()
?.FirstOrNull()
?.GetPropertyOrNull("text")
?.GetStringOrNull()
?.ParseIntOrNull()
?? SidebarPrimary
?.GetPropertyOrNull("stats")
?.EnumerateArrayOrNull()
?.FirstOrNull()
?.GetPropertyOrNull("simpleText")
?.GetStringOrNull()
?.Split(' ')
?.FirstOrDefault()
?.ParseIntOrNull();

[Lazy]
public IReadOnlyList<ThumbnailData> Thumbnails =>
SidebarPrimary
Expand Down
19 changes: 19 additions & 0 deletions YoutubeExplode/Bridge/PlaylistNextResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ internal partial class PlaylistNextResponse(JsonElement content) : IPlaylistData

public string? Description => null;

[Lazy]
public int? VideosCount =>
ContentRoot
?.GetPropertyOrNull("totalVideosText")
?.GetPropertyOrNull("runs")
?.EnumerateArrayOrNull()
?.FirstOrNull()
?.GetPropertyOrNull("text")
?.GetStringOrNull()
?.ParseIntOrNull()
?? ContentRoot
?.GetPropertyOrNull("videoCountText")
?.GetPropertyOrNull("runs")
?.EnumerateArrayOrNull()
?.ElementAtOrNull(2)
?.GetPropertyOrNull("text")
?.GetStringOrNull()
?.ParseIntOrNull();

[Lazy]
public IReadOnlyList<ThumbnailData> Thumbnails => Videos.FirstOrDefault()?.Thumbnails ?? [];

Expand Down
9 changes: 9 additions & 0 deletions YoutubeExplode/Playlists/Playlist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Playlist(
string title,
Author? author,
string description,
int? videosCount,
IReadOnlyList<Thumbnail> thumbnails
) : IPlaylist
{
Expand All @@ -32,6 +33,14 @@ IReadOnlyList<Thumbnail> thumbnails
/// </summary>
public string Description { get; } = description;

/// <summary>
/// Count of total videos.
/// </summary>
/// <remarks>
/// May be null in case of playlists with infinite videos (e.g. mixes).
/// </remarks>
public int? VideosCount { get; } = videosCount;

/// <inheritdoc />
public IReadOnlyList<Thumbnail> Thumbnails { get; } = thumbnails;

Expand Down
4 changes: 3 additions & 1 deletion YoutubeExplode/Playlists/PlaylistClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ channelId is not null && channelTitle is not null
// System playlists have no description
var description = response.Description ?? "";

var videosCount = response.VideosCount;

var thumbnails = response
.Thumbnails.Select(t =>
{
Expand All @@ -63,7 +65,7 @@ channelId is not null && channelTitle is not null
})
.ToArray();

return new Playlist(playlistId, title, author, description, thumbnails);
return new Playlist(playlistId, title, author, description, videosCount, thumbnails);
}

/// <summary>
Expand Down

0 comments on commit d4b1422

Please sign in to comment.