Skip to content

Commit

Permalink
fixed playlist search and enhanced test to include details
Browse files Browse the repository at this point in the history
  • Loading branch information
h0lg committed Nov 25, 2024
1 parent 4e69a37 commit 678b371
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
11 changes: 11 additions & 0 deletions YoutubeExplode.Tests/SearchSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
Expand Down Expand Up @@ -100,6 +101,16 @@ public async Task I_can_get_playlist_results_from_a_search_query()

// Assert
playlists.Should().NotBeEmpty();

var last = playlists.Last();

last.Title.Should().NotBeNullOrWhiteSpace();
last.Author.Should().NotBeNull();
last.Thumbnails.Should().NotBeEmpty();

var lastThumb = last.Thumbnails.Last();
lastThumb.Url.Should().NotBeNullOrWhiteSpace();
lastThumb.Resolution.Should().NotBeSameAs(default(Resolution));
}

[Fact]
Expand Down
44 changes: 24 additions & 20 deletions YoutubeExplode/Bridge/SearchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal partial class SearchResponse(JsonElement content)
[Lazy]
public IReadOnlyList<PlaylistData> Playlists =>
ContentRoot
?.EnumerateDescendantProperties("playlistRenderer")
?.EnumerateDescendantProperties("lockupViewModel")
.Select(j => new PlaylistData(j))
.ToArray() ?? [];

Expand Down Expand Up @@ -129,44 +129,48 @@ internal partial class SearchResponse
public class PlaylistData(JsonElement content)
{
[Lazy]
public string? Id => content.GetPropertyOrNull("playlistId")?.GetStringOrNull();
public string? Id => content.GetPropertyOrNull("contentId")?.GetStringOrNull();

[Lazy]
public string? Title =>
content.GetPropertyOrNull("title")?.GetPropertyOrNull("simpleText")?.GetStringOrNull()
?? content
.GetPropertyOrNull("title")
?.GetPropertyOrNull("runs")
?.EnumerateArrayOrNull()
?.Select(j => j.GetPropertyOrNull("text")?.GetStringOrNull())
.WhereNotNull()
.ConcatToString();
private JsonElement? Metadata => content.GetPropertyOrNull("metadata")?.GetPropertyOrNull("lockupMetadataViewModel");

[Lazy]
public string? Title => Metadata?.GetPropertyOrNull("title")?.GetPropertyOrNull("content")?.GetStringOrNull();

[Lazy]
private JsonElement? AuthorDetails =>
content
.GetPropertyOrNull("longBylineText")
?.GetPropertyOrNull("runs")
Metadata
?.EnumerateDescendantProperties("metadataParts")
?.ElementAtOrNull(0)
?.EnumerateArrayOrNull()
?.ElementAtOrNull(0);
?.ElementAtOrNull(0)
?.GetPropertyOrNull("text");

[Lazy]
public string? Author => AuthorDetails?.GetPropertyOrNull("text")?.GetStringOrNull();
public string? Author => AuthorDetails?.GetPropertyOrNull("content")?.GetStringOrNull();

[Lazy]
public string? ChannelId =>
AuthorDetails
?.GetPropertyOrNull("navigationEndpoint")
?.GetPropertyOrNull("commandRuns")
?.EnumerateArrayOrNull()
?.ElementAtOrNull(0)
?.GetPropertyOrNull("onTap")
?.GetPropertyOrNull("innertubeCommand")
?.GetPropertyOrNull("browseEndpoint")
?.GetPropertyOrNull("browseId")
?.GetStringOrNull();

[Lazy]
public IReadOnlyList<ThumbnailData> Thumbnails =>
content
.GetPropertyOrNull("thumbnails")
?.EnumerateDescendantProperties("thumbnails")
.SelectMany(j => j.EnumerateArrayOrEmpty())
.GetPropertyOrNull("contentImage")
?.GetPropertyOrNull("collectionThumbnailViewModel")
?.GetPropertyOrNull("primaryThumbnail")
?.GetPropertyOrNull("thumbnailViewModel")
?.GetPropertyOrNull("image")
?.GetPropertyOrNull("sources")
?.EnumerateArrayOrEmpty()
.Select(j => new ThumbnailData(j))
.ToArray() ?? [];
}
Expand Down

0 comments on commit 678b371

Please sign in to comment.