Fix #1799 locale not found in GeoIP2 database. Optimize code. Clean up old files. (#1817)

This commit is contained in:
k-jiang 2018-02-11 18:44:59 -05:00 committed by Ali 'SupaHam' M
parent e57d0e96e2
commit d92884800b
2 changed files with 47 additions and 41 deletions

View File

@ -23,6 +23,8 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Date;
import java.util.zip.GZIPInputStream;
import java.util.Arrays;
import java.util.List;
import com.ice.tar.TarInputStream;
import com.ice.tar.TarEntry;
@ -62,46 +64,37 @@ public class EssentialsGeoIPPlayerListener implements Listener, IConf {
}
InetAddress address = player.getAddress().getAddress();
StringBuilder sb = new StringBuilder();
String locale = ess.getI18n().getCurrentLocale().toString().replace('_', '-'); // get locale setting from Essentials
try {
if (config.getBoolean("database.show-cities", false)) {
CityResponse response = mmreader.city(address);
if (response == null) {
return;
}
String city;
String region;
String country;
if (config.getBoolean("enable-locale")) {
// Get geolocation based on locale. If not avaliable in specific language, get the default one.
city = ((city=response.getCity().getNames().get(locale))!=null) ? city : response.getCity().getName();
region = ((region=response.getMostSpecificSubdivision().getNames().get(locale))!=null) ? region : response.getMostSpecificSubdivision().getName();
country = ((country=response.getCountry().getNames().get(locale))!=null) ? country : response.getCountry().getName();
} else {
// Get geolocation regarding locale setting.
city = response.getCity().getName();
region = response.getMostSpecificSubdivision().getName();
country = response.getCountry().getName();
}
if (city != null) {
sb.append(city).append(", ");
}
if (region != null) {
sb.append(region).append(", ");
}
sb.append(country);
} else {
CountryResponse response = mmreader.country(address);
sb.append(response.getCountry().getNames().get(locale));
try {
if (config.getBoolean("database.show-cities", false)) {
CityResponse response = mmreader.city(address);
if (response == null) {
return;
}
} catch (AddressNotFoundException ex) {
// GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
// TODO: Maybe, we can set a new custom msg about addr-not-found in messages.properties.
logger.log(Level.INFO, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
} catch (IOException | GeoIp2Exception ex) {
// GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
logger.log(Level.SEVERE, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
String city;
String region;
String country;
city = response.getCity().getName();
region = response.getMostSpecificSubdivision().getName();
country = response.getCountry().getName();
if (city != null) {
sb.append(city).append(", ");
}
if (region != null) {
sb.append(region).append(", ");
}
sb.append(country);
} else {
CountryResponse response = mmreader.country(address);
sb.append(response.getCountry().getName());
}
} catch (AddressNotFoundException ex) {
// GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
// TODO: Maybe, we can set a new custom msg about addr-not-found in messages.properties.
logger.log(Level.INFO, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
} catch (IOException | GeoIp2Exception ex) {
// GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
logger.log(Level.SEVERE, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
}
if (config.getBoolean("show-on-whois", true)) {
u.setGeoLocation(sb.toString());
}
@ -127,6 +120,11 @@ public class EssentialsGeoIPPlayerListener implements Listener, IConf {
config.set("database.update.by-every-x-days", 30);
config.set("enable-locale", true);
config.save();
// delete old GeoIP.dat fiiles
File oldDatFile = new File(dataFolder, "GeoIP.dat");
File oldDatFileCity = new File(dataFolder, "GeoIP-City.dat");
oldDatFile.delete();
oldDatFileCity.delete();
}
if (config.getBoolean("database.show-cities", false)) {
@ -149,7 +147,14 @@ public class EssentialsGeoIPPlayerListener implements Listener, IConf {
}
}
try {
mmreader = new DatabaseReader.Builder(databaseFile).build();
// locale setting
if (config.getBoolean("enable-locale")) {
// Get geolocation based on Essentials' locale. If the locale is not avaliable, use "en".
String locale = ess.getI18n().getCurrentLocale().toString().replace('_', '-');
mmreader = new DatabaseReader.Builder(databaseFile).locales(Arrays.asList(locale,"en")).build();
} else {
mmreader = new DatabaseReader.Builder(databaseFile).build();
}
} catch (IOException ex) {
logger.log(Level.SEVERE, tl("cantReadGeoIpDB"), ex);
}

View File

@ -10,6 +10,7 @@ database:
by-every-x-days: 30
show-on-login: true
show-on-whois: true
# "enable-locale" enables locale on geolocation display. Not all languages are supported.
# Check your Essentials/config.yml "locale" section for details.
# "enable-locale" enables locale on geolocation display.
# Language is determined by Essentials/config.yml "locale" section.
# Not all languages are supported. See https://dev.maxmind.com/geoip/geoip2/web-services/#Languages
enable-locale: true