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

Add ViewCount to VideoSearchResult #832

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions YoutubeExplode/Bridge/SearchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ internal class VideoData(JsonElement content)
.ConcatToString()
.ParseTimeSpanOrNull([@"m\:ss", @"mm\:ss", @"h\:mm\:ss", @"hh\:mm\:ss"]);

[Lazy]
public long? ViewCount =>
content
.GetPropertyOrNull("viewCountText")
?.GetPropertyOrNull("simpleText")
?.GetStringOrNull()
?.StripNonDigit()
?.ParseLongOrNull();

[Lazy]
public IReadOnlyList<ThumbnailData> Thumbnails =>
content
Expand Down
3 changes: 3 additions & 0 deletions YoutubeExplode/Search/SearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(
videoData.ChannelId
?? throw new YoutubeExplodeException("Failed to extract the video channel ID.");

var viewCount = videoData.ViewCount ?? 0;

var videoThumbnails = videoData
.Thumbnails.Select(t =>
{
Expand Down Expand Up @@ -103,6 +105,7 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(
videoTitle,
new Author(videoChannelId, videoChannelTitle),
videoData.Duration,
viewCount,
videoThumbnails
);

Expand Down
9 changes: 9 additions & 0 deletions YoutubeExplode/Search/VideoSearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class VideoSearchResult(
string title,
Author author,
TimeSpan? duration,
long viewCount,
IReadOnlyList<Thumbnail> thumbnails
) : ISearchResult, IVideo
{
Expand All @@ -32,6 +33,14 @@ IReadOnlyList<Thumbnail> thumbnails
/// <inheritdoc />
public TimeSpan? Duration { get; } = duration;

/// <summary>
/// Video view count.
/// </summary>
/// <remarks>
/// May be little bit inaccurate due to YouTube's view count caching.
/// </remarks>
public long ViewCount { get; } = viewCount;

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

Expand Down