From 763ad16fec0ec012e4db8fbee087dc887491bf33 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Tue, 19 Dec 2017 09:32:28 +0100 Subject: [PATCH] fix: tries to get coordinate from ISO_CODE if 'location' is not in the geoip.database response --- helpers/geo_helper.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/helpers/geo_helper.py b/helpers/geo_helper.py index 8169fe8..c6d9fec 100644 --- a/helpers/geo_helper.py +++ b/helpers/geo_helper.py @@ -172,8 +172,15 @@ class Geo_helper: def ip_to_coord(self, ip): resp = self.reader.city(ip) - lat = float(resp.location.latitude) - lon = float(resp.location.longitude) + try: + lat = float(resp.location.latitude) + lon = float(resp.location.longitude) + except TypeError: # No location, try to use iso_code instead + self.logger.info('no location in geIP.database response for ip: {}'.format(ip)) + iso_code = resp.registered_country.iso_code #if no iso_code, throws + coord = self.country_code_to_coord[iso_code.lower()] # countrycode is in upper case + lat = float(coord['lat']) + lon = float(coord['long']) # 0.0001 correspond to ~10m # Cast the float so that it has the correct float format lat_corrected = float("{:.4f}".format(lat))