Do cache read and write in get_location instead of duplicated in each IPLOCATION_ORDER method

This commit is contained in:
Gregory Danielson 2020-11-01 16:31:15 -06:00
parent 7af4aaa8ec
commit e4ac3f266f
No known key found for this signature in database
GPG key ID: 88D4EF22F6C14CA7

View file

@ -187,6 +187,10 @@ def get_location(ip_addr):
""" """
Return location triple (CITY, REGION, COUNTRY) for `ip_addr` Return location triple (CITY, REGION, COUNTRY) for `ip_addr`
""" """
location = ipcache(ip_addr)
if location:
return location
for method in IPLOCATION_ORDER: for method in IPLOCATION_ORDER:
if method == 'geoip': if method == 'geoip':
city, region, country = geoip(ip_addr) city, region, country = geoip(ip_addr)
@ -195,10 +199,13 @@ def get_location(ip_addr):
elif method == 'ipinfo': elif method == 'ipinfo':
city, region, country = ipinfo(ip_addr) city, region, country = ipinfo(ip_addr)
else: else:
print("ERROR: invalid iplocation method speficied: %s" % method) print("ERROR: invalid iplocation method specified: %s" % method)
if city is not None:
city, region, country = workaround(city, region, country) if all((city, region, country)):
return city, region, country ipcachewrite(ip_addr, (city, region, country))
# cache write used to happen before workaround, preserve that
city, region, country = workaround(city, region, country)
return city, region, country
# #
# temporary disabled it because of geoip services capcacity # temporary disabled it because of geoip services capcacity
# #