diff --git a/src/main/java/ru/soune/nocopy/service/search/YandexSearchService.java b/src/main/java/ru/soune/nocopy/service/search/YandexSearchService.java index 01e945d..f062098 100644 --- a/src/main/java/ru/soune/nocopy/service/search/YandexSearchService.java +++ b/src/main/java/ru/soune/nocopy/service/search/YandexSearchService.java @@ -102,71 +102,15 @@ public class YandexSearchService { return callReverseImageApiByUrl(publicUrl); } - private String callReverseImageApiByUrl(String imageUrl) throws IOException { - if (searchApiKey == null || searchApiKey.isEmpty()) { - throw new IllegalStateException("SearchAPI key not configured. Set searchapi.api-key property"); - } - - if (imageUrl == null || imageUrl.trim().isEmpty()) { - throw new IllegalArgumentException("Image URL cannot be null or empty"); - } - - HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search") - .newBuilder() - .addQueryParameter("engine", "yandex_reverse_image") - .addQueryParameter("api_key", searchApiKey) - .addQueryParameter("url", imageUrl) - .addQueryParameter("wait", "true") - .addQueryParameter("timeout", "30000") - .build(); - - log.info("Calling Yandex Reverse Image API for URL: {}", imageUrl); - log.debug("Full API URL: {}", url); - - Request request = new Request.Builder() - .url(url) - .header("Accept", "application/json") - .header("User-Agent", "Mozilla/5.0 (compatible; YourApp/1.0)") - .header("Accept-Encoding", "gzip") - .build(); - - try (Response response = httpClient.newCall(request).execute()) { - ResponseBody responseBody = response.body(); - - log.info("Response code: {}", response.code()); - log.debug("Response headers: {}", response.headers()); - - if (!response.isSuccessful()) { - String errorBody = responseBody != null ? responseBody.string() : "No error body"; - log.error("API request failed: {} - {}", response.code(), errorBody); - throw new IOException("API request failed: " + response.code() + " - " + response.message()); - } - - if (responseBody == null) { - log.error("Response body is null for successful request"); - throw new IOException("Response body is null"); - } - - String responseContent = responseBody.string(); - log.info("Response body length: {} characters", responseContent.length()); - - if (log.isDebugEnabled() && responseContent.length() > 0) { - log.debug("Response body (first 500 chars): {}", - responseContent.substring(0, Math.min(responseContent.length(), 500))); - } - - return responseContent; - } catch (IOException e) { - log.error("Failed to call Yandex Reverse Image API for URL: {}", imageUrl, e); - throw e; - } - } - // private String callReverseImageApiByUrl(String imageUrl) throws IOException { // if (searchApiKey == null || searchApiKey.isEmpty()) { // throw new IllegalStateException("SearchAPI key not configured. Set searchapi.api-key property"); // } // +// if (imageUrl == null || imageUrl.trim().isEmpty()) { +// throw new IllegalArgumentException("Image URL cannot be null or empty"); +// } +// // HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search") // .newBuilder() // .addQueryParameter("engine", "yandex_reverse_image") @@ -176,31 +120,87 @@ public class YandexSearchService { // .addQueryParameter("timeout", "30000") // .build(); // -// log.info("Calling Yandex Reverse Image API: {}", url); +// log.info("Calling Yandex Reverse Image API for URL: {}", imageUrl); +// log.debug("Full API URL: {}", url); // // Request request = new Request.Builder() // .url(url) // .header("Accept", "application/json") -// .header("User-Agent", "Mozilla/5.0") +// .header("User-Agent", "Mozilla/5.0 (compatible; YourApp/1.0)") +// .header("Accept-Encoding", "gzip") // .build(); // // try (Response response = httpClient.newCall(request).execute()) { +// ResponseBody responseBody = response.body(); +// // log.info("Response code: {}", response.code()); -// log.info("Response headers: {}", response.headers()); +// log.debug("Response headers: {}", response.headers()); // // if (!response.isSuccessful()) { -// String errorBody = response.body() != null ? response.body().string() : "No error body"; +// String errorBody = responseBody != null ? responseBody.string() : "No error body"; // log.error("API request failed: {} - {}", response.code(), errorBody); // throw new IOException("API request failed: " + response.code() + " - " + response.message()); // } // -// String responseBody = response.body().string(); -// log.info("Response body length: {} characters", responseBody.length()); +// if (responseBody == null) { +// log.error("Response body is null for successful request"); +// throw new IOException("Response body is null"); +// } // -// return responseBody; +// String responseContent = responseBody.string(); +// log.info("Response body length: {} characters", responseContent.length()); +// +// if (log.isDebugEnabled() && responseContent.length() > 0) { +// log.debug("Response body (first 500 chars): {}", +// responseContent.substring(0, Math.min(responseContent.length(), 500))); +// } +// +// return responseContent; +// } catch (IOException e) { +// log.error("Failed to call Yandex Reverse Image API for URL: {}", imageUrl, e); +// throw e; // } // } + private String callReverseImageApiByUrl(String imageUrl) throws IOException { + if (searchApiKey == null || searchApiKey.isEmpty()) { + throw new IllegalStateException("SearchAPI key not configured. Set searchapi.api-key property"); + } + + HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search") + .newBuilder() + .addQueryParameter("engine", "yandex_reverse_image") + .addQueryParameter("api_key", searchApiKey) + .addQueryParameter("url", imageUrl) + .addQueryParameter("wait", "true") + .addQueryParameter("timeout", "30000") + .build(); + + log.info("Calling Yandex Reverse Image API: {}", url); + + Request request = new Request.Builder() + .url(url) + .header("Accept", "application/json") + .header("User-Agent", "Mozilla/5.0") + .build(); + + try (Response response = httpClient.newCall(request).execute()) { + log.info("Response code: {}", response.code()); + log.info("Response headers: {}", response.headers()); + + if (!response.isSuccessful()) { + String errorBody = response.body() != null ? response.body().string() : "No error body"; + log.error("API request failed: {} - {}", response.code(), errorBody); + throw new IOException("API request failed: " + response.code() + " - " + response.message()); + } + + String responseBody = response.body().string(); + log.info("Response body length: {} characters", responseBody.length()); + + return responseBody; + } + } + private boolean isImageFile(FileEntity fileEntity) { String mimeType = fileEntity.getMimeType();