dev add geo country service
Test Workflow / test (push) Successful in 3s

This commit is contained in:
vladp
2026-04-15 10:25:25 +07:00
parent 0bb952c4e6
commit c60aeec6f5
3 changed files with 27 additions and 7 deletions
@@ -64,6 +64,9 @@ public class ViolationResponse {
@JsonProperty("country") @JsonProperty("country")
private String country; private String country;
@JsonProperty("country_code")
private String countryCode;
@JsonProperty("file_id") @JsonProperty("file_id")
private String fileId; private String fileId;
@@ -121,7 +121,8 @@ public class ViolationHandler implements RequestHandler {
for (Violation violation : content) { for (Violation violation : content) {
ViolationResponse.ViolationDto violationDto = ViolationResponse.ViolationDto.fromEntity(violation); ViolationResponse.ViolationDto violationDto = ViolationResponse.ViolationDto.fromEntity(violation);
violationDto.setCountry(geoCountryService.getCountry(violation.getPageUrl())); violationDto.setCountry(geoCountryService.getCountryName(violation.getPageUrl()));
violationDto.setCountryCode(geoCountryService.getCountryCode(violation.getPageUrl()));
violationDtos.add(violationDto); violationDtos.add(violationDto);
} }
@@ -1,6 +1,7 @@
package ru.soune.nocopy.service.geo; package ru.soune.nocopy.service.geo;
import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CountryResponse; import com.maxmind.geoip2.model.CountryResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
@@ -21,13 +22,9 @@ public class GeoCountryService {
this.dbReader = new DatabaseReader.Builder(resource.getInputStream()).build(); this.dbReader = new DatabaseReader.Builder(resource.getInputStream()).build();
} }
public String getCountry(String input) { public String getCountryName(String input) {
try { try {
String domain = extractDomain(input); CountryResponse response = extractCountry(input);
InetAddress ip = InetAddress.getByName(domain);
CountryResponse response = dbReader.country(ip);
return response.getCountry().getName(); return response.getCountry().getName();
} catch (Exception e) { } catch (Exception e) {
@@ -36,6 +33,25 @@ public class GeoCountryService {
} }
} }
public String getCountryCode(String input) {
try {
CountryResponse response = extractCountry(input);
return response.getCountry().getIsoCode();
} catch (Exception e) {
log.error("Failed for input: {}", input, e);
return "UNKNOWN";
}
}
private CountryResponse extractCountry(String input) throws IOException, GeoIp2Exception {
String domain = extractDomain(input);
InetAddress ip = InetAddress.getByName(domain);
return dbReader.country(ip);
}
private String extractDomain(String input) { private String extractDomain(String input) {
if (input == null || input.isEmpty()) { if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input is empty"); throw new IllegalArgumentException("Input is empty");