This commit is contained in:
@@ -7,4 +7,10 @@ import lombok.Data;
|
|||||||
public class ImageSearchRequest {
|
public class ImageSearchRequest {
|
||||||
@JsonProperty("file_id")
|
@JsonProperty("file_id")
|
||||||
private String fileId;
|
private String fileId;
|
||||||
|
|
||||||
|
@JsonProperty("page")
|
||||||
|
private Integer page = 1;
|
||||||
|
|
||||||
|
@JsonProperty("page_size")
|
||||||
|
private Integer pageSize = 10;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,4 +42,12 @@ public class YandexSearchResponse {
|
|||||||
@JsonProperty("host")
|
@JsonProperty("host")
|
||||||
private String host;
|
private String host;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int page;
|
||||||
|
|
||||||
|
private int pageSize;
|
||||||
|
|
||||||
|
private int totalResults;
|
||||||
|
|
||||||
|
private int totalPages;
|
||||||
}
|
}
|
||||||
@@ -59,12 +59,15 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
|||||||
// GoogleVisionSearchResponse googleVisionSearchResponse = googleVisionSearchService.searchByFileEntity(fileId);
|
// GoogleVisionSearchResponse googleVisionSearchResponse = googleVisionSearchService.searchByFileEntity(fileId);
|
||||||
|
|
||||||
String yandexReverseSearchResponse = yandexSearchService.searchReverseByPublicUrl(fileEntity);
|
String yandexReverseSearchResponse = yandexSearchService.searchReverseByPublicUrl(fileEntity);
|
||||||
|
int page = imageSearchRequest.getPage() != null ? imageSearchRequest.getPage() : 1;
|
||||||
|
int pageSize = imageSearchRequest.getPageSize() != null ? imageSearchRequest.getPageSize() : 10;
|
||||||
|
|
||||||
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
||||||
|
|
||||||
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
||||||
|
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
MessageCode.SUCCESS.getDescription(),mapper.mapToYandexResponse(yandexReverseSearchResponse));
|
MessageCode.SUCCESS.getDescription(),mapper.mapToYandexResponse(yandexReverseSearchResponse, page,
|
||||||
|
pageSize));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import java.util.List;
|
|||||||
public class SearchApiToYandexResponseMapper {
|
public class SearchApiToYandexResponseMapper {
|
||||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
public YandexSearchResponse mapToYandexResponse(String searchApiJson) throws Exception {
|
public YandexSearchResponse mapToYandexResponse(String searchApiJson, int page, int pageSize) throws Exception {
|
||||||
JsonNode root = objectMapper.readTree(searchApiJson);
|
JsonNode root = objectMapper.readTree(searchApiJson);
|
||||||
|
|
||||||
YandexSearchResponse response = new YandexSearchResponse();
|
YandexSearchResponse response = new YandexSearchResponse();
|
||||||
@@ -18,9 +18,12 @@ public class SearchApiToYandexResponseMapper {
|
|||||||
JsonNode visualMatches = root.path("visual_matches");
|
JsonNode visualMatches = root.path("visual_matches");
|
||||||
|
|
||||||
if (visualMatches.isArray()) {
|
if (visualMatches.isArray()) {
|
||||||
for (JsonNode match : visualMatches) {
|
int startIndex = (page - 1) * pageSize;
|
||||||
YandexSearchResponse.ImageResult result = new YandexSearchResponse.ImageResult();
|
int endIndex = Math.min(startIndex + pageSize, visualMatches.size());
|
||||||
|
|
||||||
|
for (int i = startIndex; i < endIndex; i++) {
|
||||||
|
JsonNode match = visualMatches.get(i);
|
||||||
|
YandexSearchResponse.ImageResult result = new YandexSearchResponse.ImageResult();
|
||||||
|
|
||||||
JsonNode imageNode = match.path("image");
|
JsonNode imageNode = match.path("image");
|
||||||
if (!imageNode.isMissingNode()) {
|
if (!imageNode.isMissingNode()) {
|
||||||
@@ -30,7 +33,6 @@ public class SearchApiToYandexResponseMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result.setPageUrl(match.path("link").asText());
|
result.setPageUrl(match.path("link").asText());
|
||||||
|
|
||||||
result.setPageTitle(match.path("title").asText());
|
result.setPageTitle(match.path("title").asText());
|
||||||
|
|
||||||
String source = match.path("source").asText();
|
String source = match.path("source").asText();
|
||||||
@@ -40,6 +42,11 @@ public class SearchApiToYandexResponseMapper {
|
|||||||
imageResults.add(result);
|
imageResults.add(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response.setPage(page);
|
||||||
|
response.setPageSize(pageSize);
|
||||||
|
response.setTotalResults(visualMatches.size());
|
||||||
|
response.setTotalPages((int) Math.ceil((double) visualMatches.size() / pageSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
response.setImages(imageResults);
|
response.setImages(imageResults);
|
||||||
|
|||||||
Reference in New Issue
Block a user