Rework ip2location to include ccode, lat, long for cache purposes

gregdan3/master
Gregory Danielson 4 years ago
parent 66802657a5
commit b7517bce65
No known key found for this signature in database
GPG Key ID: 88D4EF22F6C14CA7

@ -128,19 +128,18 @@ def ip2location(ip_addr):
# if IP2LOCATION_KEY is not set, do not query, # if IP2LOCATION_KEY is not set, do not query,
# because the query wont be processed anyway # because the query wont be processed anyway
if not IP2LOCATION_KEY: if not IP2LOCATION_KEY:
return None, None, None return None
try: try:
r = requests.get( r = requests.get(
'http://api.ip2location.com/?ip=%s&key=%s&package=WS3' 'http://api.ip2location.com/?ip=%s&key=%s&package=WS5'
% (ip_addr, IP2LOCATION_KEY)) % (ip_addr, IP2LOCATION_KEY))
r.raise_for_status() r.raise_for_status()
location = r.text location = r.text
if location and ';' in location: if location and ';' in location:
_, country, region, city = location.split(';') ccode, country, region, city, lat, long, *_ = location.split(';')
location = city, region, country
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
return None, None, None return None
return city, region, country return city, region, country, ccode, lat, long
def ipinfo(ip_addr): def ipinfo(ip_addr):

Loading…
Cancel
Save