Work on IP2Location issues

This commit is contained in:
Parker M. 2024-11-03 22:55:13 -06:00
parent 0cd8c99216
commit deef0c6443
Signed by: parker
GPG Key ID: 505ED36FC12B5D5E

View File

@ -21,28 +21,24 @@ def log(link, ip, user_agent):
.first()
)
if config.IP_TO_LOCATION:
# Get IP to GEO via IP2Location.io
try:
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:
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 credits."
" insufficient funds."
)
location = "-, -"
isp = "-"
else:
location = "-, -"
isp = "-"
else:
location = f'{data["country_name"]}, {data["city"]}'
isp = data["as"]
timestamp = datetime.datetime.now()
ua_string = user_agent_parser.Parse(user_agent)