Search in sources :

Example 11 with Country

use of com.maxmind.geoip2.record.Country in project druid by druid-io.

the class WikipediaIrcDecoder method decodeMessage.

@Override
public InputRow decodeMessage(final DateTime timestamp, String channel, String msg) {
    final Map<String, String> dimensions = Maps.newHashMap();
    final Map<String, Float> metrics = Maps.newHashMap();
    Matcher m = pattern.matcher(msg);
    if (!m.matches()) {
        throw new IllegalArgumentException("Invalid input format");
    }
    Matcher shortname = shortnamePattern.matcher(channel);
    if (shortname.matches()) {
        dimensions.put("language", shortname.group(1));
    }
    String page = m.group(1);
    String pageUrl = page.replaceAll("\\s", "_");
    dimensions.put("page", pageUrl);
    String user = m.group(4);
    Matcher ipMatch = ipPattern.matcher(user);
    boolean anonymous = ipMatch.matches();
    if (anonymous) {
        try {
            final InetAddress ip = InetAddress.getByName(ipMatch.group());
            final Omni lookup = geoLookup.omni(ip);
            dimensions.put("continent", lookup.getContinent().getName());
            dimensions.put("country", lookup.getCountry().getName());
            dimensions.put("region", lookup.getMostSpecificSubdivision().getName());
            dimensions.put("city", lookup.getCity().getName());
        } catch (UnknownHostException e) {
            log.error(e, "invalid ip [%s]", ipMatch.group());
        } catch (IOException e) {
            log.error(e, "error looking up geo ip");
        } catch (GeoIp2Exception e) {
            log.error(e, "error looking up geo ip");
        }
    }
    dimensions.put("user", user);
    final String flags = m.group(2);
    dimensions.put("unpatrolled", Boolean.toString(flags.contains("!")));
    dimensions.put("newPage", Boolean.toString(flags.contains("N")));
    dimensions.put("robot", Boolean.toString(flags.contains("B")));
    dimensions.put("anonymous", Boolean.toString(anonymous));
    String[] parts = page.split(":");
    if (parts.length > 1 && !parts[1].startsWith(" ")) {
        Map<String, String> channelNamespaces = namespaces.get(channel);
        if (channelNamespaces != null && channelNamespaces.containsKey(parts[0])) {
            dimensions.put("namespace", channelNamespaces.get(parts[0]));
        } else {
            dimensions.put("namespace", "wikipedia");
        }
    } else {
        dimensions.put("namespace", "article");
    }
    float delta = m.group(6) != null ? Float.parseFloat(m.group(6)) : 0;
    metrics.put("delta", delta);
    metrics.put("added", Math.max(delta, 0));
    metrics.put("deleted", Math.min(delta, 0));
    return new InputRow() {

        @Override
        public List<String> getDimensions() {
            return dimensionList;
        }

        @Override
        public long getTimestampFromEpoch() {
            return timestamp.getMillis();
        }

        @Override
        public DateTime getTimestamp() {
            return timestamp;
        }

        @Override
        public List<String> getDimension(String dimension) {
            final String value = dimensions.get(dimension);
            if (value != null) {
                return ImmutableList.of(value);
            } else {
                return ImmutableList.of();
            }
        }

        @Override
        public Object getRaw(String dimension) {
            return dimensions.get(dimension);
        }

        @Override
        public float getFloatMetric(String metric) {
            return metrics.get(metric);
        }

        @Override
        public long getLongMetric(String metric) {
            return new Float(metrics.get(metric)).longValue();
        }

        @Override
        public int compareTo(Row o) {
            return timestamp.compareTo(o.getTimestamp());
        }

        @Override
        public String toString() {
            return "WikipediaRow{" + "timestamp=" + timestamp + ", dimensions=" + dimensions + ", metrics=" + metrics + '}';
        }
    };
}
Also used : UnknownHostException(java.net.UnknownHostException) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) InputRow(io.druid.data.input.InputRow) InputRow(io.druid.data.input.InputRow) Row(io.druid.data.input.Row) Omni(com.maxmind.geoip2.model.Omni) InetAddress(java.net.InetAddress)

Example 12 with Country

use of com.maxmind.geoip2.record.Country in project OpenAM by OpenRock.

the class Adaptive method checkGeoLocation.

protected int checkGeoLocation() {
    int retVal = 0;
    String countryCode;
    if (debug.messageEnabled()) {
        debug.message("{}.checkGeoLocation: GeoLocation database location = {}", ADAPTIVE, geoLocationDatabase);
    }
    DatabaseReader db = getLookupService(geoLocationDatabase);
    if (db == null) {
        debug.error("{}.checkGeoLocation: GeoLocation database lookup returns null", ADAPTIVE);
        return geoLocationScore;
    }
    if (geoLocationValues == null) {
        debug.error("{}.checkGeoLocation: The property '{}' is null", ADAPTIVE, GEO_LOCATION_VALUES);
        return geoLocationScore;
    }
    try {
        countryCode = getCountryCode(db, clientIP);
    } catch (IOException e) {
        if (debug.warningEnabled()) {
            debug.warning("{}.checkGeoLocation: #getCountryCode :: An IO error happened", ADAPTIVE, e);
        }
        return geoLocationScore;
    } catch (GeoIp2Exception e) {
        if (debug.warningEnabled()) {
            debug.warning("{}.checkGeoLocation: #getCountryCode :: An error happened when looking up the IP", ADAPTIVE, e);
        }
        return geoLocationScore;
    }
    if (debug.messageEnabled()) {
        debug.message("{}.checkGeoLocation: {} returns {}", ADAPTIVE, clientIP, countryCode);
    }
    StringTokenizer st = new StringTokenizer(geoLocationValues, "|");
    while (st.hasMoreTokens()) {
        if (countryCode.equalsIgnoreCase(st.nextToken())) {
            if (debug.messageEnabled()) {
                debug.message("{}.checkGeoLocation: Found Country Code : {}", ADAPTIVE, countryCode);
            }
            retVal = geoLocationScore;
            break;
        }
    }
    if (!geoLocationInvert) {
        retVal = geoLocationScore - retVal;
    }
    return retVal;
}
Also used : StringTokenizer(java.util.StringTokenizer) DatabaseReader(com.maxmind.geoip2.DatabaseReader) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception)

Example 13 with Country

use of com.maxmind.geoip2.record.Country in project quickutil by quickutil.

the class GeoUtil method geoCodeyByBaidu.

/**
 * 根据经纬度查询地理信息-百度批量
 *
 * @param points-纬度经度数组,下标偶数位为纬度,奇数位为经度,必须成对,最多20条经纬度
 * @return
 */
public static List<GeoDef> geoCodeyByBaidu(List<GeoPoint> points) {
    try {
        if (points.size() > 20)
            return null;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < points.size(); i++) {
            double[] delta = WGSToGCJPointer(points.get(i).latitude, points.get(i).longitude);
            delta = GCJToBDPointer(delta[0], delta[1]);
            builder.append(delta[0] + "," + delta[1] + "|");
        }
        String queryUrl = String.format("http://api.map.baidu.com/geocoder/v2/?ak=%s&location=%s&output=json&batch=true", baiduKeyIn, URLEncoder.encode(builder.substring(0, builder.length() - 1), "UTF-8"));
        JsonObject object = JsonUtil.toJsonMap(FileUtil.stream2string(HttpUtil.httpGet(queryUrl).getEntity().getContent()));
        JsonArray array = object.getAsJsonArray("areas");
        List<GeoDef> geodefList = new ArrayList<GeoDef>();
        for (int i = 0; i < points.size(); i++) {
            object = array.get(i).getAsJsonObject();
            String country = object.get("country").getAsString();
            if (country.equals("中国"))
                country = "China";
            if (country.equals("England"))
                country = "United Kingdom";
            String countryCode = countryCodeByCountryName(country);
            String countryChinese = countryChineseByCountryCode(countryCode);
            String state = object.get("province").getAsString();
            String stateCode = "";
            if (country.equals("China"))
                stateCode = stateCodeByStateChinese(countryCode, state);
            else
                stateCode = stateCodeByStateName(countryCode, state);
            state = stateNameByStateCode(countryCode, stateCode);
            String stateChinese = stateChineseByStateCode(countryCode, stateCode);
            String city = object.get("city").getAsString();
            String description = object.get("district").getAsString();
            countryCode = (countryCode == null) ? UNKNOWN : countryCode;
            country = (country == null) ? UNKNOWN : country;
            countryChinese = (countryChinese == null) ? UNKNOWN : countryChinese;
            stateCode = (stateCode == null) ? UNKNOWN : stateCode;
            state = (state == null) ? UNKNOWN : state;
            stateChinese = (stateChinese == null) ? UNKNOWN : stateChinese;
            city = (city == null) ? UNKNOWN : city;
            geodefList.add(new GeoDef(points.get(i).latitude, points.get(i).longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, description));
        }
        return geodefList;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) GeoPoint(com.quickutil.platform.def.GeoPoint) GeoDef(com.quickutil.platform.def.GeoDef) UnknownHostException(java.net.UnknownHostException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException)

Example 14 with Country

use of com.maxmind.geoip2.record.Country in project quickutil by quickutil.

the class GeoUtil method geoCodeyByAmap.

/**
 * 根据经纬度查询地理信息-高德中国
 *
 * @param latitude-纬度
 * @param longitude-经度
 * @return
 */
public static GeoDef geoCodeyByAmap(double latitude, double longitude) {
    try {
        double[] delta = WGSToGCJPointer(latitude, longitude);
        String queryUrl = String.format("http://restapi.amap.com/v3/geocode/regeo?output=json&location=%s,%s&key=%s", delta[1], delta[0], amapKeyIn);
        JsonObject object = JsonUtil.toJsonMap(FileUtil.stream2string(HttpUtil.httpGet(queryUrl).getEntity().getContent()));
        String country = object.getAsJsonObject("regeocode").getAsJsonObject("addressComponent").get("country").getAsString();
        if (country.equals("中国"))
            country = "China";
        if (country.equals("England"))
            country = "United Kingdom";
        String city = "";
        if (!object.getAsJsonObject("regeocode").getAsJsonObject("addressComponent").get("city").isJsonArray())
            city = object.getAsJsonObject("regeocode").getAsJsonObject("addressComponent").get("city").getAsString();
        String description = object.getAsJsonObject("regeocode").get("formatted_address").getAsString();
        String countryCode = countryCodeByCountryName(country);
        String countryChinese = countryChineseByCountryCode(countryCode);
        String state = object.get("province").getAsString();
        String stateCode = "";
        if (country.equals("China"))
            stateCode = stateCodeByStateChinese(countryCode, state);
        else
            stateCode = stateCodeByStateName(countryCode, state);
        state = stateNameByStateCode(countryCode, stateCode);
        String stateChinese = stateChineseByStateCode(countryCode, stateCode);
        countryCode = (countryCode == null) ? UNKNOWN : countryCode;
        country = (country == null) ? UNKNOWN : country;
        countryChinese = (countryChinese == null) ? UNKNOWN : countryChinese;
        stateCode = (stateCode == null) ? UNKNOWN : stateCode;
        state = (state == null) ? UNKNOWN : state;
        stateChinese = (stateChinese == null) ? UNKNOWN : stateChinese;
        city = (city == null) ? UNKNOWN : city;
        return new GeoDef(latitude, longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, description);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : JsonObject(com.google.gson.JsonObject) GeoDef(com.quickutil.platform.def.GeoDef) UnknownHostException(java.net.UnknownHostException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException)

Example 15 with Country

use of com.maxmind.geoip2.record.Country in project quickutil by quickutil.

the class GeoUtil method GeoIPByMMDB.

public static GeoDef GeoIPByMMDB(String ip) {
    String countryCode = UNKNOWN;
    String country = UNKNOWN;
    String countryChinese = UNKNOWN;
    String stateCode = UNKNOWN;
    String state = UNKNOWN;
    String stateChinese = UNKNOWN;
    String city = UNKNOWN;
    Double latitude = 0.0;
    Double longitude = 0.0;
    try {
        InetAddress ipAddr = InetAddress.getByName(ip);
        CityResponse result;
        result = databaseReader.city(ipAddr);
        countryCode = result.getCountry().getIsoCode();
        country = result.getCountry().getName();
        countryChinese = countryChineseByCountryCode(countryCode);
        stateCode = result.getMostSpecificSubdivision().getIsoCode();
        state = stateNameByStateCode(countryCode, stateCode);
        stateChinese = stateChineseByStateCode(countryCode, stateCode);
        city = result.getCity().getName();
        if (countryCode != null && countryCode.equals("CN") && city != null && result.getCity().getNames().containsKey("zh-CN")) {
            if (!result.getCity().getNames().get("zh-CN").endsWith("市")) {
                city = result.getCity().getNames().get("zh-CN") + "市";
            } else {
                city = result.getCity().getNames().get("zh-CN");
            }
        }
        latitude = result.getLocation().getLatitude();
        longitude = result.getLocation().getLongitude();
    } catch (AddressNotFoundException ae) {
    } catch (UnknownHostException ue) {
    } catch (Exception e) {
        e.printStackTrace();
    }
    countryCode = (countryCode == null) ? UNKNOWN : countryCode;
    country = (country == null) ? UNKNOWN : country;
    countryChinese = (countryChinese == null) ? UNKNOWN : countryChinese;
    stateCode = (stateCode == null) ? UNKNOWN : stateCode;
    state = (state == null) ? UNKNOWN : state;
    stateChinese = (stateChinese == null) ? UNKNOWN : stateChinese;
    city = (city == null) ? UNKNOWN : city;
    latitude = (latitude == null) ? 0.0 : latitude;
    longitude = (longitude == null) ? 0.0 : longitude;
    return new GeoDef(latitude, longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, "");
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) UnknownHostException(java.net.UnknownHostException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) GeoDef(com.quickutil.platform.def.GeoDef)

Aggregations

CityResponse (com.maxmind.geoip2.model.CityResponse)12 Country (com.maxmind.geoip2.record.Country)11 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)10 Location (com.maxmind.geoip2.record.Location)9 IOException (java.io.IOException)9 InetAddress (java.net.InetAddress)8 CountryResponse (com.maxmind.geoip2.model.CountryResponse)7 City (com.maxmind.geoip2.record.City)7 Continent (com.maxmind.geoip2.record.Continent)7 UnknownHostException (java.net.UnknownHostException)7 HashMap (java.util.HashMap)6 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)5 Postal (com.maxmind.geoip2.record.Postal)5 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)4 Subdivision (com.maxmind.geoip2.record.Subdivision)4 GeoDef (com.quickutil.platform.def.GeoDef)4 JsonObject (com.google.gson.JsonObject)3 Traits (com.maxmind.geoip2.record.Traits)3 User (com.earth2me.essentials.User)2 MaxMind (com.maxmind.geoip2.record.MaxMind)2