blob: a191d5a3034fb414374cff774c6a52c8f864dd8a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
from collections import namedtuple
import geoip2.database
import geoip2.errors
from .config import GEOIP_GEOLITE2_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):
if not GEOIP_GEOLITE2_CITY_PATH:
return default_location
with geoip2.database.Reader(GEOIP_GEOLITE2_CITY_PATH) as reader:
try:
res = reader.city(ip)
return res.location
except geoip2.errors.AddressNotFoundError:
return default_location
|