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
gregdan3/master
Gregory Danielson 4 years ago
parent 996485adf1
commit 628a860d6d
No known key found for this signature in database
GPG Key ID: 88D4EF22F6C14CA7

@ -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):

Loading…
Cancel
Save