diff options
Diffstat (limited to 'iss/geo.py')
| -rw-r--r-- | iss/geo.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/iss/geo.py b/iss/geo.py new file mode 100644 index 0000000..55c8f02 --- /dev/null +++ b/iss/geo.py @@ -0,0 +1,24 @@ +from collections import namedtuple +import geoip2.database +import geoip2.errors + +from .config import GEOIP_CITY_PATH + +DEFAULT_LOCATION = { + "latitude": 34.7641, + "longitude": 32.0669, + "time_zone": "Asia/Jerusalem", +} + +default_location = namedtuple("Location", DEFAULT_LOCATION.keys())( + *DEFAULT_LOCATION.values() +) + + +def get_location(ip): + with geoip2.database.Reader(GEOIP_CITY_PATH) as reader: + try: + res = reader.city(ip) + return res.location + except geoip2.errors.AddressNotFoundError: + return default_location |
