This commit is contained in:
@@ -6,7 +6,9 @@ import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SearchApiToYandexResponseMapper {
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
@@ -125,6 +127,108 @@ public class SearchApiToYandexResponseMapper {
|
||||
// return response;
|
||||
// }
|
||||
|
||||
public List<YandexSearchResponse.ImageResult> getAllImagesWithoutPagination(String searchApiJson, String findType)
|
||||
throws IOException {
|
||||
JsonNode root = objectMapper.readTree(searchApiJson);
|
||||
JsonNode matches = root.path(findType);
|
||||
|
||||
List<YandexSearchResponse.ImageResult> allImages = new ArrayList<>();
|
||||
|
||||
if (matches.isArray()) {
|
||||
for (JsonNode match : matches) {
|
||||
YandexSearchResponse.ImageResult result = mapImageResult(match);
|
||||
|
||||
if ("exact_matches".equals(findType)) {
|
||||
JsonNode thumbnail = match.path("thumbnail");
|
||||
if (!thumbnail.isMissingNode() && thumbnail.asText().startsWith("data:image")) {
|
||||
result.setUrl(thumbnail.asText());
|
||||
}
|
||||
|
||||
JsonNode imageNode = match.path("image");
|
||||
if (!imageNode.isMissingNode()) {
|
||||
String directUrl = imageNode.path("link").asText(null);
|
||||
if (directUrl != null && !directUrl.isBlank()) {
|
||||
result.setUrl(directUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result.getUrl() != null && !result.getUrl().isBlank()) {
|
||||
allImages.add(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return allImages;
|
||||
}
|
||||
|
||||
public List<YandexSearchResponse.ImageResult> removeDuplicateUrls(
|
||||
List<YandexSearchResponse.ImageResult> yandexImages,
|
||||
List<YandexSearchResponse.ImageResult> googleImages) {
|
||||
|
||||
Map<String, YandexSearchResponse.ImageResult> uniqueByUrl = new LinkedHashMap<>();
|
||||
|
||||
for (YandexSearchResponse.ImageResult image : googleImages) {
|
||||
String normalizedUrl = normalizeUrl(image.getUrl());
|
||||
uniqueByUrl.putIfAbsent(normalizedUrl, image);
|
||||
}
|
||||
|
||||
for (YandexSearchResponse.ImageResult image : yandexImages) {
|
||||
String normalizedUrl = normalizeUrl(image.getUrl());
|
||||
uniqueByUrl.putIfAbsent(normalizedUrl, image);
|
||||
}
|
||||
|
||||
return new ArrayList<>(uniqueByUrl.values());
|
||||
}
|
||||
|
||||
public List<YandexSearchResponse.ImageResult> paginateResults(
|
||||
List<YandexSearchResponse.ImageResult> allResults,
|
||||
int page,
|
||||
int pageSize) {
|
||||
|
||||
if (allResults.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
int totalPages = (int) Math.ceil((double) allResults.size() / pageSize);
|
||||
|
||||
if (page > totalPages) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
int startIndex = (page - 1) * pageSize;
|
||||
int endIndex = Math.min(startIndex + pageSize, allResults.size());
|
||||
|
||||
return new ArrayList<>(allResults.subList(startIndex, endIndex));
|
||||
}
|
||||
|
||||
private String normalizeUrl(String url) {
|
||||
if (url == null) return null;
|
||||
|
||||
try {
|
||||
if (url.startsWith("data:image")) {
|
||||
return url.length() > 100 ? url.substring(0, 100) : url;
|
||||
}
|
||||
|
||||
java.net.URI uri = new java.net.URI(url);
|
||||
String host = uri.getHost();
|
||||
if (host != null) {
|
||||
host = host.toLowerCase().replaceFirst("^www\\.", "");
|
||||
}
|
||||
|
||||
String path = uri.getPath();
|
||||
if (path != null && path.length() > 1 && path.endsWith("/")) {
|
||||
path = path.substring(0, path.length() - 1);
|
||||
}
|
||||
|
||||
return (host != null ? host : "") + (path != null ? path : "");
|
||||
} catch (Exception e) {
|
||||
return url.toLowerCase()
|
||||
.replaceFirst("^(https?://)?(www\\.)?", "")
|
||||
.replaceFirst("/$", "");
|
||||
}
|
||||
}
|
||||
|
||||
private YandexSearchResponse.ImageResult mapImageResult(JsonNode match) {
|
||||
|
||||
YandexSearchResponse.ImageResult result =
|
||||
|
||||
Reference in New Issue
Block a user