Updated geoip to check history before doing lookup

This commit is contained in:
Thomas Williams 2024-08-21 11:54:10 +01:00
parent 39c5196349
commit 512f94d7f6
Signed by: thomas
GPG key ID: EB8F975CF60BCBFF

76
log.py
View file

@ -108,44 +108,68 @@ class logsManager:
try:
if 'delayUntil' in locals() and datetime.now() < delayUntil:
conn = pyodbc.connect(self.conn_str)
cursor = conn.cursor()
timeThreshold = datetime.now() - timedelta(days=1)
print("Rate limit exceeded. Please wait before trying again.")
return None
cursor.execute("SELECT blockedipaddress, hostname, city, region, country, loc, org, postal, timezone FROM monutil_geoip WHERE logtime >= ?", timeThreshold)
url = f"https://ipinfo.io/{ip}?token={token}"
rows = cursor.fetchall()
response = requests.get(url)
response.raise_for_status()
data = response.json()
geoinfo = []
if response.status_code == 429:
for row in rows:
geoinfo = {
"ip": row.blockedipaddress,
"hostname": row.hostname,
"city": row.city,
"region": row.region,
"country": row.country,
"loc": row.loc,
"org": row.org,
"postal": row.postal,
"timezone": row.timezone,
}
print("Rate limit exceeded. Please wait before trying again.")
today = datetime.now()
if not geoinfo:
if today.month == 12:
if 'delayUntil' in locals() and datetime.now() < delayUntil:
delayUntil = datetime(today.year + 1, 1, 1)
print("Rate limit exceeded. Please wait before trying again.")
return None
else:
url = f"https://ipinfo.io/{ip}?token={token}"
delayUntil = datetime(today.year, today.month + 1, 1)
response = requests.get(url)
response.raise_for_status()
data = response.json()
return None
if response.status_code == 429:
geoinfo = {
"ip": data.get("ip"),
"hostname": data.get("hostname"),
"city": data.get("city"),
"region": data.get("region"),
"country": data.get("country"),
"loc": data.get("loc"),
"org": data.get("org"),
"postal": data.get("postal"),
"timezone": data.get("timezone"),
}
print("Rate limit exceeded. Please wait before trying again.")
today = datetime.now()
if today.month == 12:
delayUntil = datetime(today.year + 1, 1, 1)
else:
delayUntil = datetime(today.year, today.month + 1, 1)
return None
geoinfo = {
"ip": data.get("ip"),
"hostname": data.get("hostname"),
"city": data.get("city"),
"region": data.get("region"),
"country": data.get("country"),
"loc": data.get("loc"),
"org": data.get("org"),
"postal": data.get("postal"),
"timezone": data.get("timezone"),
}
return geoinfo