Skip to content

Commit

Permalink
Add a fallback for when ip-api is offline
Browse files Browse the repository at this point in the history
  • Loading branch information
yorevs committed Nov 13, 2024
1 parent ca17833 commit c589995
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/main/askai/core/component/geo_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
Copyright (c) 2024, HomeSetup
"""
import datetime

from askai.core.askai_configs import configs
from datetime import datetime
from hspylib.core.metaclass.singleton import Singleton
from hspylib.core.namespace import Namespace
from hspylib.modules.fetch import fetch
from json import JSONDecodeError
from requests.exceptions import ConnectionError
from requests.exceptions import ConnectionError, ReadTimeout
from textwrap import dedent

import json
Expand All @@ -38,7 +38,7 @@ class GeoLocation(metaclass=Singleton):
"""
{
"status": "failure", "country": "", "countryCode": "", "region": "", "regionName": "",
"city": "", "zip": "", "lat": 0.0, "lon": 0.0, "timezone": "",
"city": "", "zip": "", "lat": 0.0, "lon": 0.0, "timezone": "UTC",
"isp": "", "org": "", "as": "", "query": ""
}
"""
Expand All @@ -57,7 +57,7 @@ def get_location(cls, ip: str = None) -> Namespace:
url = f"{cls.GEO_LOC_URL}{'/' + ip if ip else ''}"
log.debug("Fetching the Geo Position from: %s", url)
geo_req = fetch.get(url)
except (JSONDecodeError, ConnectionError) as err:
except (JSONDecodeError, ConnectionError, ReadTimeout) as err:
log.error("Failed to retrieve geo location => %s", str(err))
geo_req = Namespace(body=cls.EMPTY_JSON_RESP)
geo_loc: Namespace = Namespace(**(json.loads(geo_req.body)))
Expand Down Expand Up @@ -114,7 +114,7 @@ def location(self) -> str:

@property
def datetime(self) -> str:
utc_datetime = datetime.utcnow().replace(tzinfo=pytz.utc)
utc_datetime = datetime.datetime.now(datetime.UTC).replace(tzinfo=pytz.utc)
zoned_datetime = utc_datetime.astimezone(pytz.timezone(self.timezone))
return zoned_datetime.strftime(self.DATE_FMT)

Expand Down

0 comments on commit c589995

Please sign in to comment.