fix: tries to get coordinate from ISO_CODE if 'location' is not in the geoip.database response

pull/25/head
Sami Mokaddem 2017-12-19 09:32:28 +01:00
parent c25e4f1718
commit 763ad16fec
1 changed files with 9 additions and 2 deletions

View File

@ -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))