Skip to content

Commit

Permalink
Handle old layout when extracting results on Yandex reverse image search
Browse files Browse the repository at this point in the history
  • Loading branch information
d4n3436 committed Dec 28, 2023
1 parent 07b848b commit 9afe437
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/Apis/Yandex/YandexImageSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,24 @@ public async Task<IReadOnlyList<IYandexReverseImageSearchResult>> ReverseImageSe

using var data = JsonDocument.Parse(json);

return data.RootElement
.GetProperty("initialState")
.GetProperty("serpList")
.GetProperty("items")
.GetProperty("entities")
.EnumerateObject()
.Select(x => x.Value.GetProperty("viewerData").Deserialize<YandexReverseImageSearchResult>()!)
.ToArray();
if (data.RootElement.TryGetProperty("initialState", out var initialState))
{
return initialState
.GetProperty("serpList")
.GetProperty("items")
.GetProperty("entities")
.EnumerateObject()
.Select(x => x.Value.GetProperty("viewerData").Deserialize<YandexReverseImageSearchResult>()!)
.ToArray();
}

// Old layout
return htmlDocument
.GetElementsByClassName("serp-list")
.FirstOrDefault()?
.GetElementsByClassName("serp-item")
.Select(x => JsonDocument.Parse(x.GetAttribute("data-bem")!).RootElement.GetProperty("serp-item").Deserialize<YandexReverseImageSearchResult>()!)
.ToArray() ?? Array.Empty<YandexReverseImageSearchResult>();
}

/// <inheritdoc/>
Expand Down

0 comments on commit 9afe437

Please sign in to comment.