From 9afe4371dc9dad6ab39fc1281f856671d1aa1db9 Mon Sep 17 00:00:00 2001 From: d4n3436 Date: Thu, 28 Dec 2023 13:55:22 -0500 Subject: [PATCH] Handle old layout when extracting results on Yandex reverse image search --- src/Apis/Yandex/YandexImageSearch.cs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Apis/Yandex/YandexImageSearch.cs b/src/Apis/Yandex/YandexImageSearch.cs index d3da7d9..749c55a 100644 --- a/src/Apis/Yandex/YandexImageSearch.cs +++ b/src/Apis/Yandex/YandexImageSearch.cs @@ -166,14 +166,24 @@ public async Task> 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()!) - .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()!) + .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()!) + .ToArray() ?? Array.Empty(); } ///