From d5cf61d168bdbcf05032d5a8003e61386ae8deb2 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Mon, 28 Sep 2020 22:05:05 +0300 Subject: Finalize geo parsing from IP --- iss/geo.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 iss/geo.py (limited to 'iss/geo.py') 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 -- cgit v1.3.1