From 88cd84b5d2011244dabf875fa4abfd6fa5f89e6b Mon Sep 17 00:00:00 2001 From: Gregory Danielson Date: Sun, 15 Nov 2020 10:57:22 -0600 Subject: [PATCH] Rework ipinfo to include ccode, lat, long for cache purposes --- lib/location.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/location.py b/lib/location.py index da41dc1..93f6bbd 100644 --- a/lib/location.py +++ b/lib/location.py @@ -144,18 +144,22 @@ def ip2location(ip_addr): def ipinfo(ip_addr): if not IPINFO_TOKEN: - return None, None, None + return None try: r = requests.get( 'https://ipinfo.io/%s/json?token=%s' % (ip_addr, IPINFO_TOKEN)) r.raise_for_status() r_json = r.json() - city, region, country = r_json["city"], r_json["region"], r_json["country"] + # can't do two unpackings on one line + city, region, country, ccode = r_json["city"], r_json["region"], '', r_json["country"], + lat, long = r_json["loc"].split(',') + # NOTE: ipinfo only provides ISO codes for countries + country = pycountry.countries.get(alpha_2=ccode).name except (requests.exceptions.RequestException, ValueError): # latter is thrown by failure to parse json in reponse - return None, None, None - return city, region, country + return None + return city, region, country, ccode, lat, long def geoip(ip_addr):