From deb2a5d01fca64ffbb6ea384653cf7a6eafff89f Mon Sep 17 00:00:00 2001 From: Gregory Danielson Date: Sun, 1 Nov 2020 16:37:45 -0600 Subject: [PATCH] Adjust ipcache to observe new region THIS WILL INVALIDATE THE EXISTING CACHE! The new cache now pulls data from all three of geoip, ip2location, and ipinfo, and stores it in the relatively simple city;region;country format. The old format would either store city;country or store countrycode;country;region;city directly from ip2location, so it is much more consistent with this change. --- lib/location.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/location.py b/lib/location.py index 337772e..c6ab2a5 100644 --- a/lib/location.py +++ b/lib/location.py @@ -103,22 +103,22 @@ def ipcachewrite(ip_addr, location): # like ip2location format, but reversed def ipcache(ip_addr): - cached = os.path.join(IP2LCACHE, ip_addr) + """ Retrieve a location from cache by ip addr + Returns a triple of (CITY, REGION, COUNTRY) or None + """ + cachefile = os.path.join(IP2LCACHE, ip_addr) if not os.path.exists(IP2LCACHE): os.makedirs(IP2LCACHE) - location = None - - if os.path.exists(cached): - location = open(cached, 'r').read().split(';') - if len(location) > 3: - return location[3], location[1] - elif len(location) > 1: - return location[0], location[1] - else: - return location[0], None + if os.path.exists(cachefile): + try: + city, region, country = open(cachefile, 'r').read().split(';') + return city, region, country + except ValueError: + # cache entry is malformed: should be city;region;country + return None + return None - return None, None def ip2location(ip_addr): """Convert IP address `ip_addr` to a location name"""