mirror of
https://github.com/chubin/wttr.in.git
synced 2025-08-15 08:12:48 +02:00
Rework ipinfo to include ccode, lat, long for cache purposes
This commit is contained in:
@ -144,18 +144,22 @@ def ip2location(ip_addr):
|
|||||||
|
|
||||||
def ipinfo(ip_addr):
|
def ipinfo(ip_addr):
|
||||||
if not IPINFO_TOKEN:
|
if not IPINFO_TOKEN:
|
||||||
return None, None, None
|
return None
|
||||||
try:
|
try:
|
||||||
r = requests.get(
|
r = requests.get(
|
||||||
'https://ipinfo.io/%s/json?token=%s'
|
'https://ipinfo.io/%s/json?token=%s'
|
||||||
% (ip_addr, IPINFO_TOKEN))
|
% (ip_addr, IPINFO_TOKEN))
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
r_json = r.json()
|
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):
|
except (requests.exceptions.RequestException, ValueError):
|
||||||
# latter is thrown by failure to parse json in reponse
|
# latter is thrown by failure to parse json in reponse
|
||||||
return None, None, None
|
return None
|
||||||
return city, region, country
|
return city, region, country, ccode, lat, long
|
||||||
|
|
||||||
|
|
||||||
def geoip(ip_addr):
|
def geoip(ip_addr):
|
||||||
|
Reference in New Issue
Block a user