assume IP info is the same for any /24 subnet

This commit is contained in:
Connor Olding 2020-09-03 12:57:05 +02:00
parent 0603081c49
commit 3a3c1340a9

View file

@ -1,5 +1,4 @@
from collections import namedtuple
from sys import stderr
from time import time
CacheLine = namedtuple("CacheLine", ("time", "code"))
@ -7,6 +6,11 @@ CacheLine = namedtuple("CacheLine", ("time", "code"))
one_month = 365.25 / 12 * 24 * 60 * 60 # in seconds
def mask_ip(ip):
# this assumes IP info is the same for a /24 subnet.
return ".".join(ip.split(".")[:3]) + ".0"
class IpInfoBase:
pass
@ -172,7 +176,8 @@ class IpInfoByIpApi(IpInfoBase):
continue
ip, time, code = row[0], float(row[1]), row[2]
info = CacheLine(time, code)
self.stored[ip] = info
masked = mask_ip(ip)
self.stored[masked] = info
def flush(self):
from csv import writer
@ -183,7 +188,8 @@ class IpInfoByIpApi(IpInfoBase):
handle.writerow(self.csv_header)
for ip, info in self.stored.items():
timestr = "{:.2f}".format(info.time)
handle.writerow([ip, timestr, info.code])
masked = mask_ip(ip)
handle.writerow([masked, timestr, info.code])
def cache(self, ip, info=None, timestamp=None):
if self.stored is None:
@ -191,8 +197,10 @@ class IpInfoByIpApi(IpInfoBase):
now = time() if timestamp is None else timestamp
masked = mask_ip(ip)
if info is None:
cached = self.stored.get(ip, None)
cached = self.stored.get(masked, None)
if cached is None:
return None
if now > cached.time + self.expiry:
@ -200,7 +208,7 @@ class IpInfoByIpApi(IpInfoBase):
return cached
else:
assert isinstance(info, CacheLine), type(info)
self.stored[ip] = info
self.stored[masked] = info
async def find_country(self, ip, db=None):
now = time()