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.
This commit is contained in:
Gregory Danielson 2020-11-01 16:37:45 -06:00
parent 2ebdb15398
commit deb2a5d01f
No known key found for this signature in database
GPG key ID: 88D4EF22F6C14CA7

View file

@ -103,22 +103,22 @@ def ipcachewrite(ip_addr, location):
# like ip2location format, but reversed # like ip2location format, but reversed
def ipcache(ip_addr): 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): if not os.path.exists(IP2LCACHE):
os.makedirs(IP2LCACHE) os.makedirs(IP2LCACHE)
location = 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
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
return None, None
def ip2location(ip_addr): def ip2location(ip_addr):
"""Convert IP address `ip_addr` to a location name""" """Convert IP address `ip_addr` to a location name"""