mirror of
https://github.com/chubin/wttr.in.git
synced 2025-08-15 16:22:51 +02: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:
@ -143,20 +143,19 @@ def ip2location(ip_addr):
|
|||||||
|
|
||||||
|
|
||||||
def ipinfo(ip_addr):
|
def ipinfo(ip_addr):
|
||||||
location = ipcache(ip_addr)
|
if not IPINFO_TOKEN:
|
||||||
if location:
|
return None, None, None
|
||||||
return location
|
try:
|
||||||
if IPINFO_TOKEN:
|
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))
|
||||||
if r.status_code == 200:
|
r.raise_for_status()
|
||||||
r_json = r.json()
|
r_json = r.json()
|
||||||
location = r_json["city"], r_json["region"], r_json["country"]
|
city, region, country = r_json["city"], r_json["region"], r_json["country"]
|
||||||
else:
|
except (requests.exceptions.RequestException, ValueError):
|
||||||
location = None, None, None
|
# latter is thrown by failure to parse json in reponse
|
||||||
if location:
|
return None, None, None
|
||||||
ipcachewrite(ip_addr, location)
|
return city, region, country
|
||||||
return location
|
|
||||||
|
|
||||||
|
|
||||||
def geoip(ip_addr):
|
def geoip(ip_addr):
|
||||||
|
Reference in New Issue
Block a user