diff options
author | Parker <contact@pkrm.dev> | 2024-11-03 22:42:22 -0600 |
---|---|---|
committer | Parker <contact@pkrm.dev> | 2024-11-03 22:42:22 -0600 |
commit | 850c3c7c406e885077095be8363eae97b0f53e28 (patch) | |
tree | bd04b282f5a800edccdf890ed2bc9fb72484f862 /app/util | |
parent | e22ef86f0750bdf3940694cc6d9d6c006735ddee (diff) |
Hopefully fix IP2Location issues
Diffstat (limited to 'app/util')
-rw-r--r-- | app/util/log.py | 25 |
1 files 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 = "-" |