mirror of
https://github.com/chubin/wttr.in
synced 2024-11-15 00:27:09 +00: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:
parent
2ebdb15398
commit
deb2a5d01f
1 changed files with 12 additions and 12 deletions
|
@ -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"""
|
||||||
|
|
Loading…
Reference in a new issue