aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorParker <contact@pkrm.dev>2024-11-03 22:59:47 -0600
committerParker <contact@pkrm.dev>2024-11-03 22:59:47 -0600
commit179c29b279bcab67eb718601694a4927cb5e8078 (patch)
treead771f73ff9dd7082fa1f03141c16e5d10ddf96d
parentdeef0c644383d8e13eed64c0d84896c4ee4ef867 (diff)
Hopefully fix the IP2Location issues
-rw-r--r--app/util/log.py46
1 files changed, 28 insertions, 18 deletions
diff --git a/app/util/log.py b/app/util/log.py
index ac7d99e..46687c2 100644
--- a/app/util/log.py
+++ b/app/util/log.py
@@ -11,6 +11,32 @@ Create a new log record whenever a link is visited
"""
+def ip_to_location(ip):
+ if not config.IP_TO_LOCATION:
+ return "-, -", "-"
+
+ url = f"https://api.ip2location.io/?key={config.API_KEY}&ip={ip}"
+ response = requests.get(url)
+ data = response.json()
+
+ if response.status_code != 200:
+ config.LOG.error(
+ "Error with IP2Location API. Perhaps the API is down."
+ )
+ return "-, -", "-"
+
+ if "error" in data:
+ config.LOG.error(
+ "Error with IP2Location API. Likely wrong API key or insufficient"
+ " funds."
+ )
+ return "-, -", "-"
+
+ location = f'{data["country_name"]}, {data["city_name"]}'
+ isp = data["as"]
+ return location, isp
+
+
def log(link, ip, user_agent):
db = SessionLocal()
@@ -21,24 +47,8 @@ def log(link, ip, user_agent):
.first()
)
- if not config.IP_TO_LOCATION:
- location = "-, -"
- isp = "-"
- # Get IP to GEO via IPGeolocation.io
- else:
- url = f"https://api.ip2location.io/?key={config.API_KEY}&ip={ip}"
- data = requests.get(url).json()
- print(data)
- if "error" in data:
- config.LOG.error(
- "Error with IP2Location API. Likely wrong API key or"
- " insufficient funds."
- )
- location = "-, -"
- isp = "-"
- else:
- location = f'{data["country_name"]}, {data["city"]}'
- isp = data["as"]
+ # Get the location and ISP of the user
+ location, isp = ip_to_location(ip)
timestamp = datetime.datetime.now()
ua_string = user_agent_parser.Parse(user_agent)