blob: 55c8f02c3f49cb09bb74ee806412f768ea8e72e4 (
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
|
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
|