|
| 1 | +package com.jacamars.dsp.rtb.tools; |
| 2 | +import java.io.File; |
| 3 | +import java.net.InetAddress; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.JsonNode; |
| 6 | +import com.fasterxml.jackson.databind.node.DoubleNode; |
| 7 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 8 | +import com.fasterxml.jackson.databind.node.MissingNode; |
| 9 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 10 | +import com.fasterxml.jackson.databind.node.TextNode; |
| 11 | +import com.google.openrtb.OpenRtb.BidRequest; |
| 12 | +import com.jacamars.dsp.rtb.blocks.LookingGlass; |
| 13 | +import com.jacamars.dsp.rtb.exchanges.Atomx; |
| 14 | +import com.jacamars.dsp.rtb.tools.IsoTwo2Iso3; |
| 15 | +import com.maxmind.geoip2.DatabaseReader; |
| 16 | +import com.maxmind.geoip2.model.CityResponse; |
| 17 | +import com.maxmind.geoip2.record.City; |
| 18 | +import com.maxmind.geoip2.record.Country; |
| 19 | +import com.maxmind.geoip2.record.Postal; |
| 20 | +import com.maxmind.geoip2.record.Subdivision; |
| 21 | +import com.maxmind.geoip2.record.Location; |
| 22 | + |
| 23 | +/** |
| 24 | + * A singleton that adds geo information to a device, if IP is available. |
| 25 | + * @author Ben M. Faul |
| 26 | + * |
| 27 | + */ |
| 28 | +public enum GeoPatch { |
| 29 | + GEOPATCH; |
| 30 | + static final JsonNodeFactory factory = JsonNodeFactory.instance; |
| 31 | + static IsoTwo2Iso3 isoMap; |
| 32 | + static DatabaseReader reader = null; |
| 33 | + |
| 34 | + public static void main(String[] args) throws Exception { |
| 35 | + new IsoTwo2Iso3("@ISO2-3", "data/adxgeo.csv"); |
| 36 | + GeoPatch.getInstance("/home/ben/GeoLite2-City.mmdb"); |
| 37 | + Atomx br = new Atomx("SampleBids/nogeo.txt"); |
| 38 | + } |
| 39 | + |
| 40 | + public static GeoPatch getInstance(String fileName) throws Exception { |
| 41 | + reader = new DatabaseReader.Builder(new File(fileName)).build(); |
| 42 | + isoMap = (IsoTwo2Iso3) LookingGlass.symbols.get("@ISO2-3"); |
| 43 | + InetAddress ipAddress = InetAddress.getByName("47.180.117.78"); |
| 44 | + reader.city(ipAddress); |
| 45 | + return GEOPATCH; |
| 46 | + } |
| 47 | + |
| 48 | + public static GeoPatch getInstance() { |
| 49 | + return GEOPATCH; |
| 50 | + } |
| 51 | + |
| 52 | + public void patch(JsonNode idev) throws Exception { |
| 53 | + |
| 54 | + //long timer = System.currentTimeMillis(); |
| 55 | + /** |
| 56 | + * No patch if not initialized |
| 57 | + */ |
| 58 | + if (reader == null || idev == null || idev instanceof MissingNode) |
| 59 | + return; |
| 60 | + |
| 61 | + ObjectNode device = (ObjectNode)idev; |
| 62 | + /** |
| 63 | + * Make sure iso2 to 3 converter is available. |
| 64 | + */ |
| 65 | + if (isoMap == null) |
| 66 | + isoMap = (IsoTwo2Iso3) LookingGlass.symbols.get("@ISO2-3"); |
| 67 | + |
| 68 | + String ip = device.get("ip").asText(""); |
| 69 | + if (ip.equals("")) |
| 70 | + return; |
| 71 | + |
| 72 | + InetAddress ipAddress = InetAddress.getByName(ip); |
| 73 | + CityResponse response = null; |
| 74 | + City city = null; |
| 75 | + Country country = null; |
| 76 | + Subdivision subdivision = null; |
| 77 | + Postal postal = null; |
| 78 | + Location location = null; |
| 79 | + |
| 80 | + /** |
| 81 | + * Make a geo node if necessary |
| 82 | + */ |
| 83 | + ObjectNode geo = (ObjectNode)device.get("geo"); |
| 84 | + if (geo == null) { |
| 85 | + geo = factory.objectNode(); |
| 86 | + device.set("geo", geo); |
| 87 | + response = reader.city(ipAddress); |
| 88 | + city = response.getCity(); |
| 89 | + country = response.getCountry(); |
| 90 | + subdivision = response.getMostSpecificSubdivision(); |
| 91 | + postal = response.getPostal(); |
| 92 | + location = response.getLocation(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Add country, convert iso2 to iso3 |
| 97 | + */ |
| 98 | + JsonNode x = null; |
| 99 | + if ((x = geo.get("country"))==null) { |
| 100 | + if (x == null) { |
| 101 | + if (response == null) |
| 102 | + response = reader.city(ipAddress); |
| 103 | + if (city == null) |
| 104 | + city = response.getCity(); |
| 105 | + if (country == null) |
| 106 | + country = response.getCountry(); |
| 107 | + if (subdivision == null) |
| 108 | + subdivision = response.getMostSpecificSubdivision(); |
| 109 | + if (postal == null) |
| 110 | + postal = response.getPostal(); |
| 111 | + if (location == null) |
| 112 | + location = response.getLocation(); |
| 113 | + } |
| 114 | + String iso = country.getIsoCode(); |
| 115 | + if (iso.length()==2 && isoMap != null) { |
| 116 | + iso = isoMap.query(iso); |
| 117 | + } |
| 118 | + geo.set("country", new TextNode(iso)); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Add city |
| 123 | + */ |
| 124 | + x = null; |
| 125 | + if ((x = geo.get("city"))==null) { |
| 126 | + if (x == null) { |
| 127 | + if (response == null) |
| 128 | + response = reader.city(ipAddress); |
| 129 | + if (city == null) |
| 130 | + city = response.getCity(); |
| 131 | + if (country == null) |
| 132 | + country = response.getCountry(); |
| 133 | + if (subdivision == null) |
| 134 | + subdivision = response.getMostSpecificSubdivision(); |
| 135 | + if (postal == null) |
| 136 | + postal = response.getPostal(); |
| 137 | + if (location == null) |
| 138 | + location = response.getLocation(); |
| 139 | + } |
| 140 | + geo.set("city", new TextNode(city.getName())); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Add state/region |
| 145 | + */ |
| 146 | + x = null;; |
| 147 | + if ((x = geo.get("region"))==null) { |
| 148 | + if (x == null) { |
| 149 | + if (response == null) |
| 150 | + response = reader.city(ipAddress); |
| 151 | + if (city == null) |
| 152 | + city = response.getCity(); |
| 153 | + if (country == null) |
| 154 | + country = response.getCountry(); |
| 155 | + if (subdivision == null) |
| 156 | + subdivision = response.getMostSpecificSubdivision(); |
| 157 | + if (postal == null) |
| 158 | + postal = response.getPostal(); |
| 159 | + if (location == null) |
| 160 | + location = response.getLocation(); |
| 161 | + } |
| 162 | + geo.set("state",new TextNode(subdivision.getIsoCode())); |
| 163 | + } |
| 164 | + x = null;; |
| 165 | + if ((x = geo.get("zipcode"))==null) { |
| 166 | + if (x == null) { |
| 167 | + if (response == null) |
| 168 | + response = reader.city(ipAddress); |
| 169 | + if (city == null) |
| 170 | + city = response.getCity(); |
| 171 | + if (country == null) |
| 172 | + country = response.getCountry(); |
| 173 | + if (subdivision == null) |
| 174 | + subdivision = response.getMostSpecificSubdivision(); |
| 175 | + if (postal == null) |
| 176 | + postal = response.getPostal(); |
| 177 | + if (location == null) |
| 178 | + location = response.getLocation(); |
| 179 | + } |
| 180 | + geo.set("zip", new TextNode(postal.getCode())); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Add latitude and longitude |
| 185 | + */ |
| 186 | + x = null;; |
| 187 | + if ((x = geo.get("lat"))==null) { |
| 188 | + if (x == null) { |
| 189 | + if (response == null) |
| 190 | + response = reader.city(ipAddress); |
| 191 | + if (city == null) |
| 192 | + city = response.getCity(); |
| 193 | + if (country == null) |
| 194 | + country = response.getCountry(); |
| 195 | + if (subdivision == null) |
| 196 | + subdivision = response.getMostSpecificSubdivision(); |
| 197 | + if (postal == null) |
| 198 | + postal = response.getPostal(); |
| 199 | + if (location == null) |
| 200 | + location = response.getLocation(); |
| 201 | + } |
| 202 | + geo.set("lat", new DoubleNode(location.getLatitude())); |
| 203 | + geo.set("lon", new DoubleNode(location.getLatitude())); |
| 204 | + } |
| 205 | + |
| 206 | + //timer = System.currentTimeMillis() - timer; |
| 207 | + //System.out.println(timer); |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments