From c7d3b32d53ba66443908c555c288df9b41396f6c Mon Sep 17 00:00:00 2001 From: Gregory Danielson Date: Sun, 1 Nov 2020 16:35:17 -0600 Subject: [PATCH] Remove cache operation from ip2location; shortcut where possible This comes with something of a refactor. - IP2LOCATION_KEY 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 --- lib/location.py | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/location.py b/lib/location.py index 888346d..2dd190b 100644 --- a/lib/location.py +++ b/lib/location.py @@ -116,30 +116,23 @@ def ipcache(ip_addr): return None, None def ip2location(ip_addr): - "Convert IP address `ip_addr` to a location name" - - location = ipcache(ip_addr) - if location: - return location - - # if IP2LOCATION_KEY is not set, do not the query, + """Convert IP address `ip_addr` to a location name""" + # if IP2LOCATION_KEY is not set, do not query, # because the query wont be processed anyway - if IP2LOCATION_KEY: - try: - location = requests\ - .get('http://api.ip2location.com/?ip=%s&key=%s&package=WS3' \ - % (ip_addr, IP2LOCATION_KEY)).text - except requests.exceptions.ConnectionError: - pass - - if location and ';' in location: - ipcachewrite(ip_addr, location) - _, country, region, city = location.split(';') - location = city, region, country - else: - location = location, None, None - - return location + if not IP2LOCATION_KEY: + return None, None, None + try: + r = requests.get( + 'http://api.ip2location.com/?ip=%s&key=%s&package=WS3' + % (ip_addr, IP2LOCATION_KEY)) + r.raise_for_status() + location = r.text + if location and ';' in location: + _, country, region, city = location.split(';') + location = city, region, country + except requests.exceptions.RequestException: + return None, None, None + return city, region, country def ipinfo(ip_addr):