From 850c3c7c406e885077095be8363eae97b0f53e28 Mon Sep 17 00:00:00 2001 From: Parker Date: Sun, 3 Nov 2024 22:42:22 -0600 Subject: [PATCH] Hopefully fix IP2Location issues --- app/util/log.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/util/log.py b/app/util/log.py index 6be82e7..f25641a 100644 --- a/app/util/log.py +++ b/app/util/log.py @@ -1,15 +1,11 @@ -import ip2locationio +import requests import datetime from ua_parser import user_agent_parser -from ip2locationio.ipgeolocation import IP2LocationIOAPIError from database import SessionLocal import config from models import Link, Record -configuration = ip2locationio.Configuration(config.API_KEY) -ipgeolocation = ip2locationio.IPGeolocation(configuration) - """ Create a new log record whenever a link is visited """ @@ -28,14 +24,19 @@ def log(link, ip, user_agent): if config.IP_TO_LOCATION: # Get IP to GEO via IP2Location.io try: - data = ipgeolocation.lookup(ip) - location = f'{data["country_name"]}, {data["city_name"]}' - isp = data["as"] - # Fatal error, API key is invalid or out of requests, quit - except IP2LocationIOAPIError: + url = f"https://api.ip2location.io/?key={config.API_KEY}&ip={ip}" + data = requests.get(url).json() + if "error" in data: + raise Exception("Error") + else: + location = f'{data["country_name"]}, {data["city"]}' + isp = data["as"] + + # Fatal error, maybe API is down? + except: config.LOG.error( - "Invalid API key or insufficient credits. Change the" - " `config.ini` file if you no longer need IP geolocation." + "Error with IP2Location API. Likely wrong API key or" + " insufficient credits." ) location = "-, -" isp = "-"