Change to freegeoip.io and add a second service as fall-back

This commit is contained in:
Fabian Affolter 2016-04-06 19:25:40 +02:00 committed by Paulus Schoutsen
parent b4ddc86304
commit 2e44166854

View file

@ -17,16 +17,35 @@ LocationInfo = collections.namedtuple(
'city', 'zip_code', 'time_zone', 'latitude', 'longitude',
'use_fahrenheit'])
DATA_SOURCE = ['https://freegeoip.io/json/', 'http://ip-api.com/json']
def detect_location_info():
"""Detect location information."""
try:
raw_info = requests.get(
'https://freegeoip.net/json/', timeout=5).json()
except (requests.RequestException, ValueError):
return None
success = None
data = {key: raw_info.get(key) for key in LocationInfo._fields}
for source in DATA_SOURCE:
try:
raw_info = requests.get(source, timeout=5).json()
success = source
break
except (requests.RequestException, ValueError):
success = False
if success is False:
return None
else:
data = {key: raw_info.get(key) for key in LocationInfo._fields}
if success is DATA_SOURCE[1]:
data['ip'] = raw_info.get('query')
data['country_code'] = raw_info.get('countryCode')
data['country_name'] = raw_info.get('country')
data['region_code'] = raw_info.get('region')
data['region_name'] = raw_info.get('regionName')
data['zip_code'] = raw_info.get('zip')
data['time_zone'] = raw_info.get('timezone')
data['latitude'] = raw_info.get('lat')
data['longitude'] = raw_info.get('lon')
# From Wikipedia: Fahrenheit is used in the Bahamas, Belize,
# the Cayman Islands, Palau, and the United States and associated