This commit is contained in:
@@ -104,10 +104,7 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
||||
|
||||
yandexSearchResponse.setImages(results);
|
||||
yandexSearchResponse.setTotalResults(totalSize);
|
||||
log.info("COMBINED RESPONSE" + yandexSearchResponse.toString());
|
||||
log.info("COMBINED RESPONSE" + yandexSearchResponse.toString());
|
||||
log.info("COMBINED RESPONSE" + yandexSearchResponse.toString());
|
||||
log.info("COMBINED RESPONSE" + yandexSearchResponse.toString());
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), yandexSearchResponse);
|
||||
}
|
||||
|
||||
@@ -12,14 +12,13 @@ public class SearchApiToYandexResponseMapper {
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public YandexSearchResponse mapToYandexResponse(String searchApiJson, int page, int pageSize, String findType) throws IOException {
|
||||
|
||||
JsonNode root = objectMapper.readTree(searchApiJson);
|
||||
JsonNode visualMatches = root.path(findType);
|
||||
JsonNode matches = root.path(findType);
|
||||
|
||||
YandexSearchResponse response = new YandexSearchResponse();
|
||||
List<YandexSearchResponse.ImageResult> images = new ArrayList<>();
|
||||
|
||||
if (!visualMatches.isArray() || visualMatches.isEmpty()) {
|
||||
if (!matches.isArray() || matches.isEmpty()) {
|
||||
response.setImages(images);
|
||||
response.setPage(page);
|
||||
response.setPageSize(pageSize);
|
||||
@@ -28,7 +27,7 @@ public class SearchApiToYandexResponseMapper {
|
||||
return response;
|
||||
}
|
||||
|
||||
int totalResults = visualMatches.size();
|
||||
int totalResults = matches.size();
|
||||
int totalPages = (int) Math.ceil((double) totalResults / pageSize);
|
||||
|
||||
if (page > totalPages) {
|
||||
@@ -44,9 +43,24 @@ public class SearchApiToYandexResponseMapper {
|
||||
int endIndex = Math.min(startIndex + pageSize, totalResults);
|
||||
|
||||
for (int i = startIndex; i < endIndex; i++) {
|
||||
JsonNode match = visualMatches.get(i);
|
||||
JsonNode match = matches.get(i);
|
||||
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()) {
|
||||
images.add(result);
|
||||
}
|
||||
@@ -61,6 +75,56 @@ public class SearchApiToYandexResponseMapper {
|
||||
return response;
|
||||
}
|
||||
|
||||
// public YandexSearchResponse mapToYandexResponse(String searchApiJson, int page, int pageSize, String findType) throws IOException {
|
||||
//
|
||||
// JsonNode root = objectMapper.readTree(searchApiJson);
|
||||
// JsonNode visualMatches = root.path(findType);
|
||||
//
|
||||
// YandexSearchResponse response = new YandexSearchResponse();
|
||||
// List<YandexSearchResponse.ImageResult> images = new ArrayList<>();
|
||||
//
|
||||
// if (!visualMatches.isArray() || visualMatches.isEmpty()) {
|
||||
// response.setImages(images);
|
||||
// response.setPage(page);
|
||||
// response.setPageSize(pageSize);
|
||||
// response.setTotalResults(0);
|
||||
// response.setTotalPages(0);
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// int totalResults = visualMatches.size();
|
||||
// int totalPages = (int) Math.ceil((double) totalResults / pageSize);
|
||||
//
|
||||
// if (page > totalPages) {
|
||||
// response.setImages(images);
|
||||
// response.setPage(page);
|
||||
// response.setPageSize(pageSize);
|
||||
// response.setTotalResults(totalResults);
|
||||
// response.setTotalPages(totalPages);
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// int startIndex = (page - 1) * pageSize;
|
||||
// int endIndex = Math.min(startIndex + pageSize, totalResults);
|
||||
//
|
||||
// for (int i = startIndex; i < endIndex; i++) {
|
||||
// JsonNode match = visualMatches.get(i);
|
||||
// YandexSearchResponse.ImageResult result = mapImageResult(match);
|
||||
//
|
||||
// if (result.getUrl() != null && !result.getUrl().isBlank()) {
|
||||
// images.add(result);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// response.setImages(images);
|
||||
// response.setPage(page);
|
||||
// response.setPageSize(pageSize);
|
||||
// response.setTotalResults(totalResults);
|
||||
// response.setTotalPages(totalPages);
|
||||
//
|
||||
// return response;
|
||||
// }
|
||||
|
||||
private YandexSearchResponse.ImageResult mapImageResult(JsonNode match) {
|
||||
|
||||
YandexSearchResponse.ImageResult result =
|
||||
|
||||
Reference in New Issue
Block a user