mirror of
https://github.com/chubin/wttr.in.git
synced 2025-08-16 00:28:12 +02:00
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:
@ -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(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):
|
||||
"""Convert IP address `ip_addr` to a location name"""
|
||||
|
Reference in New Issue
Block a user