-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Geo Location and improve internet responses
- Loading branch information
Hugo Saporetti Junior
committed
Mar 28, 2024
1 parent
580bd0d
commit 6857592
Showing
10 changed files
with
153 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
Questions: | ||
|
||
en_US | ||
|
||
1. Summarize my markdown files at my HomeSetup docs folder. | ||
2. What are the current weather conditions in San Francisco, U.S today? | ||
3. When is the upcoming Los Angeles Lakers match? | ||
4. Who currently holds the office of President of the United States? | ||
4. Who currently holds the office of President of the United States? | ||
|
||
pt_BR | ||
|
||
1. Qual a previsao do tempo hoje para Belo Horizonte? | ||
2. Quem e o atual presidente do Brasil? | ||
3. Quem e Hugo Saporetti junior? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import json | ||
import logging as log | ||
from datetime import datetime | ||
from json import JSONDecodeError | ||
from textwrap import dedent | ||
|
||
import pytz | ||
from hspylib.core.metaclass.singleton import Singleton | ||
from hspylib.core.namespace import Namespace | ||
from hspylib.modules.fetch import fetch | ||
from requests.exceptions import ConnectionError | ||
|
||
|
||
class GeoLocation(metaclass=Singleton): | ||
"""TODO""" | ||
|
||
INSTANCE: 'GeoLocation' = None | ||
|
||
GEO_LOC_URL: str = "http://ip-api.com/json" | ||
|
||
EMPTY_JSON_RESP: str = dedent(''' | ||
{ | ||
"status": "failure", "country": "", "countryCode": "", "region": "", "regionName": "", | ||
"city": "", "zip": "", "lat": 0.0, "lon": 0.0, "timezone": "", | ||
"isp": "", "org": "", "as": "", "query": "" | ||
} | ||
''') | ||
|
||
# Date format used in prompts, e.g: Fri 22 Mar 19:47 2024. | ||
DATE_FMT: str = "%a %d %b %-H:%M %Y" | ||
|
||
@classmethod | ||
def get_location(cls, ip: str = None) -> Namespace: | ||
"""TODO""" | ||
try: | ||
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: | ||
log.error('Failed to retrieve geo location => %s', str(err)) | ||
geo_req = Namespace(body=cls.EMPTY_JSON_RESP) | ||
geo_json = json.loads(geo_req.body) | ||
geo_location: Namespace = Namespace(**geo_json) | ||
return geo_location | ||
|
||
def __init__(self, ip: str = None): | ||
self._geo_location = self.get_location(ip) | ||
|
||
def __str__(self): | ||
geo_loc = self._geo_location | ||
geo_loc.setattr('zoned_datetime', self.now) | ||
return str(self._geo_location) | ||
|
||
@property | ||
def latitude(self) -> float: | ||
return self._geo_location.lat | ||
|
||
@property | ||
def longitude(self) -> float: | ||
return self._geo_location.lon | ||
|
||
@property | ||
def country(self) -> str: | ||
return self._geo_location.country | ||
|
||
@property | ||
def country_code(self) -> str: | ||
return self._geo_location.country_code | ||
|
||
@property | ||
def region(self) -> str: | ||
return self._geo_location.region | ||
|
||
@property | ||
def region_name(self) -> str: | ||
return self._geo_location.region_name | ||
|
||
@property | ||
def city(self) -> float: | ||
return self._geo_location.city | ||
|
||
@property | ||
def zip(self) -> str: | ||
return self._geo_location.zip | ||
|
||
@property | ||
def timezone(self) -> str: | ||
return self._geo_location.timezone | ||
|
||
@property | ||
def now(self) -> str: | ||
utc_datetime = datetime.utcnow().replace(tzinfo=pytz.utc) | ||
zoned_datetime = utc_datetime.astimezone(pytz.timezone(self.timezone)) | ||
return zoned_datetime.strftime(GeoLocation.DATE_FMT) | ||
|
||
|
||
assert (geo_location := GeoLocation().INSTANCE) is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters