mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Change to freegeoip.io and add a second service as fall-back
This commit is contained in:
parent
b4ddc86304
commit
2e44166854
@ -17,16 +17,35 @@ LocationInfo = collections.namedtuple(
|
|||||||
'city', 'zip_code', 'time_zone', 'latitude', 'longitude',
|
'city', 'zip_code', 'time_zone', 'latitude', 'longitude',
|
||||||
'use_fahrenheit'])
|
'use_fahrenheit'])
|
||||||
|
|
||||||
|
DATA_SOURCE = ['https://freegeoip.io/json/', 'http://ip-api.com/json']
|
||||||
|
|
||||||
|
|
||||||
def detect_location_info():
|
def detect_location_info():
|
||||||
"""Detect location information."""
|
"""Detect location information."""
|
||||||
try:
|
success = None
|
||||||
raw_info = requests.get(
|
|
||||||
'https://freegeoip.net/json/', timeout=5).json()
|
|
||||||
except (requests.RequestException, ValueError):
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
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}
|
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,
|
# From Wikipedia: Fahrenheit is used in the Bahamas, Belize,
|
||||||
# the Cayman Islands, Palau, and the United States and associated
|
# the Cayman Islands, Palau, and the United States and associated
|
||||||
|
Loading…
x
Reference in New Issue
Block a user