diff options
| author | Yuval Adam <_@yuv.al> | 2020-09-28 22:05:05 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2020-09-28 22:05:05 +0300 |
| commit | d5cf61d168bdbcf05032d5a8003e61386ae8deb2 (patch) | |
| tree | aeaf99cde2a147511f06d9d53a652c7b8374e86f /iss/geo.py | |
| parent | c81aeba984b08957614f7167cdbdea8780e76a11 (diff) | |
Finalize geo parsing from IP
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 |
