mirror of
https://github.com/chubin/wttr.in
synced 2024-11-15 00:27:09 +00:00
Remove cache operation from ipinfo; shortcut where possible
This comes with something of a refactor. - IPINFO_TOKEN is now checked with `if not` to reduce indention - ConnectionError is increased to RequestException to catch all requests errors - On that note, now raising for status too - Now catching ValueError in case of json parsing failure
This commit is contained in:
parent
996485adf1
commit
628a860d6d
1 changed files with 13 additions and 14 deletions
|
@ -143,20 +143,19 @@ def ip2location(ip_addr):
|
|||
|
||||
|
||||
def ipinfo(ip_addr):
|
||||
location = ipcache(ip_addr)
|
||||
if location:
|
||||
return location
|
||||
if IPINFO_TOKEN:
|
||||
r = requests.get('https://ipinfo.io/%s/json?token=%s' %
|
||||
(ip_addr, IPINFO_TOKEN))
|
||||
if r.status_code == 200:
|
||||
r_json = r.json()
|
||||
location = r_json["city"], r_json["region"], r_json["country"]
|
||||
else:
|
||||
location = None, None, None
|
||||
if location:
|
||||
ipcachewrite(ip_addr, location)
|
||||
return location
|
||||
if not IPINFO_TOKEN:
|
||||
return None, None, 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"]
|
||||
except (requests.exceptions.RequestException, ValueError):
|
||||
# latter is thrown by failure to parse json in reponse
|
||||
return None, None, None
|
||||
return city, region, country
|
||||
|
||||
|
||||
def geoip(ip_addr):
|
||||
|
|
Loading…
Reference in a new issue