dev add utf encoding
Test Workflow / test (push) Successful in 3s

This commit is contained in:
vladp
2026-02-10 12:52:36 +07:00
parent d50c565405
commit 9692fe48b7
@@ -102,71 +102,15 @@ public class YandexSearchService {
return callReverseImageApiByUrl(publicUrl); 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 { // private String callReverseImageApiByUrl(String imageUrl) throws IOException {
// if (searchApiKey == null || searchApiKey.isEmpty()) { // if (searchApiKey == null || searchApiKey.isEmpty()) {
// throw new IllegalStateException("SearchAPI key not configured. Set searchapi.api-key property"); // 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") // HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search")
// .newBuilder() // .newBuilder()
// .addQueryParameter("engine", "yandex_reverse_image") // .addQueryParameter("engine", "yandex_reverse_image")
@@ -176,31 +120,87 @@ public class YandexSearchService {
// .addQueryParameter("timeout", "30000") // .addQueryParameter("timeout", "30000")
// .build(); // .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() // Request request = new Request.Builder()
// .url(url) // .url(url)
// .header("Accept", "application/json") // .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(); // .build();
// //
// try (Response response = httpClient.newCall(request).execute()) { // try (Response response = httpClient.newCall(request).execute()) {
// ResponseBody responseBody = response.body();
//
// log.info("Response code: {}", response.code()); // log.info("Response code: {}", response.code());
// log.info("Response headers: {}", response.headers()); // log.debug("Response headers: {}", response.headers());
// //
// if (!response.isSuccessful()) { // 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); // log.error("API request failed: {} - {}", response.code(), errorBody);
// throw new IOException("API request failed: " + response.code() + " - " + response.message()); // throw new IOException("API request failed: " + response.code() + " - " + response.message());
// } // }
// //
// String responseBody = response.body().string(); // if (responseBody == null) {
// log.info("Response body length: {} characters", responseBody.length()); // 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) { private boolean isImageFile(FileEntity fileEntity) {
String mimeType = fileEntity.getMimeType(); String mimeType = fileEntity.getMimeType();