Skip to content

Commit

Permalink
Fix playlist search response extraction (#843)
Browse files Browse the repository at this point in the history
  • Loading branch information
h0lg authored Dec 12, 2024
1 parent 5062dc3 commit 0abc2a4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 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
59 changes: 53 additions & 6 deletions YoutubeExplode/Bridge/SearchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ internal partial class SearchResponse(JsonElement content)
[Lazy]
public IReadOnlyList<PlaylistData> Playlists =>
ContentRoot
?.EnumerateDescendantProperties("lockupViewModel")
.Select(j => new PlaylistData(j))
.ToArray()
// fall back to older working paths
?? ContentRoot
?.EnumerateDescendantProperties("playlistRenderer")
.Select(j => new PlaylistData(j))
.ToArray() ?? [];
.ToArray()
?? [];

[Lazy]
public IReadOnlyList<ChannelData> Channels =>
Expand Down Expand Up @@ -129,11 +135,20 @@ internal partial class SearchResponse
public class PlaylistData(JsonElement content)
{
[Lazy]
public string? Id => content.GetPropertyOrNull("playlistId")?.GetStringOrNull();
public string? Id =>
content.GetPropertyOrNull("contentId")?.GetStringOrNull()
// fall back to older working paths
?? content.GetPropertyOrNull("playlistId")?.GetStringOrNull();

[Lazy]
private JsonElement? Metadata =>
content.GetPropertyOrNull("metadata")?.GetPropertyOrNull("lockupMetadataViewModel");

[Lazy]
public string? Title =>
content.GetPropertyOrNull("title")?.GetPropertyOrNull("simpleText")?.GetStringOrNull()
Metadata?.GetPropertyOrNull("title")?.GetPropertyOrNull("content")?.GetStringOrNull()
// fall back to older working paths
?? content.GetPropertyOrNull("title")?.GetPropertyOrNull("simpleText")?.GetStringOrNull()
?? content
.GetPropertyOrNull("title")
?.GetPropertyOrNull("runs")
Expand All @@ -144,18 +159,38 @@ public class PlaylistData(JsonElement content)

[Lazy]
private JsonElement? AuthorDetails =>
content
Metadata
?.EnumerateDescendantProperties("metadataParts")
?.ElementAtOrNull(0)
?.EnumerateArrayOrNull()
?.ElementAtOrNull(0)
?.GetPropertyOrNull("text")
// fall back to older working paths
?? content
.GetPropertyOrNull("longBylineText")
?.GetPropertyOrNull("runs")
?.EnumerateArrayOrNull()
?.ElementAtOrNull(0);

[Lazy]
public string? Author => AuthorDetails?.GetPropertyOrNull("text")?.GetStringOrNull();
public string? Author =>
AuthorDetails?.GetPropertyOrNull("content")?.GetStringOrNull()
// fall back to older working paths
?? AuthorDetails?.GetPropertyOrNull("text")?.GetStringOrNull();

[Lazy]
public string? ChannelId =>
AuthorDetails
?.GetPropertyOrNull("commandRuns")
?.EnumerateArrayOrNull()
?.ElementAtOrNull(0)
?.GetPropertyOrNull("onTap")
?.GetPropertyOrNull("innertubeCommand")
?.GetPropertyOrNull("browseEndpoint")
?.GetPropertyOrNull("browseId")
?.GetStringOrNull()
// fall back to older working paths
?? AuthorDetails
?.GetPropertyOrNull("navigationEndpoint")
?.GetPropertyOrNull("browseEndpoint")
?.GetPropertyOrNull("browseId")
Expand All @@ -164,11 +199,23 @@ public class PlaylistData(JsonElement content)
[Lazy]
public IReadOnlyList<ThumbnailData> Thumbnails =>
content
.GetPropertyOrNull("contentImage")
?.GetPropertyOrNull("collectionThumbnailViewModel")
?.GetPropertyOrNull("primaryThumbnail")
?.GetPropertyOrNull("thumbnailViewModel")
?.GetPropertyOrNull("image")
?.GetPropertyOrNull("sources")
?.EnumerateArrayOrEmpty()
.Select(j => new ThumbnailData(j))
.ToArray()
// fall back to older working paths
?? content
.GetPropertyOrNull("thumbnails")
?.EnumerateDescendantProperties("thumbnails")
.SelectMany(j => j.EnumerateArrayOrEmpty())
.Select(j => new ThumbnailData(j))
.ToArray() ?? [];
.ToArray()
?? [];
}
}

Expand Down

0 comments on commit 0abc2a4

Please sign in to comment.