Skip to content

Commit

Permalink
Add exception handler for fetch_web
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Jan 3, 2024
1 parent db662c5 commit b57b4e6
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions helpers/external/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,32 @@ def fetch_db(ip: str) -> Geolocation | None:
return

def fetch_web(ip: str, is_local: bool = False) -> Geolocation | None:
response = app.session.requests.get(f'http://ip-api.com/line/{ip if not is_local else ""}')
try:
response = app.session.requests.get(f'http://ip-api.com/line/{ip if not is_local else ""}')

if not response.ok:
return None
if not response.ok:
return None

status, *lines = response.text.split('\n')
status, *lines = response.text.split('\n')

if status != 'success':
app.session.logger.error(
f'Failed to get geolocation: {status} ({lines[0]})'
if status != 'success':
app.session.logger.error(
f'Failed to get geolocation: {status} ({lines[0]})'
)
return None

index = list(COUNTRIES.keys()).index(lines[1])

return Geolocation(
ip=lines[12],
latitude=float(lines[6]),
longitude=float(lines[7]),
country_code=lines[1],
country_name=lines[0],
country_index=index,
timezone=lines[8],
city=lines[4]
)
except Exception as e:
app.session.logger.error(f'Failed to get geolocation from web: {e}')
return None

index = list(COUNTRIES.keys()).index(lines[1])

return Geolocation(
ip=lines[12],
latitude=float(lines[6]),
longitude=float(lines[7]),
country_code=lines[1],
country_name=lines[0],
country_index=index,
timezone=lines[8],
city=lines[4]
)

0 comments on commit b57b4e6

Please sign in to comment.