see https://github.com/karlicoss/HPI/issues/262 * move home to fallback/via_home.py * move via_ip to fallback * add fallback model * add stub via_ip file * add fallback_locations for via_ip * use protocol for locations * estimate_from helper, via_home estimator, all.py * via_home: add accuracy, cache history * add datasources to gpslogger/google_takeout * tz/via_location.py: update import to fallback * denylist docs/installation instructions * tz.via_location: let user customize cachew refresh time * add via_ip.estimate_location using binary search * use estimate_location in via_home.get_location * tests: add gpslogger to location config stub * tests: install tz related libs in test env * tz: add regression test for broken windows dates * vendorize bisect_left from python src doesnt have a 'key' parameter till python3.10
45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
"""
|
|
Provides location/timezone data from IP addresses, using [[https://github.com/seanbreckenridge/ipgeocache][ipgeocache]]
|
|
"""
|
|
|
|
REQUIRES = ["git+https://github.com/seanbreckenridge/ipgeocache"]
|
|
|
|
from my.core import __NOT_HPI_MODULE__
|
|
|
|
import ipaddress
|
|
from typing import NamedTuple, Iterator, Tuple
|
|
from datetime import datetime
|
|
|
|
import ipgeocache
|
|
|
|
from my.core import Json
|
|
|
|
|
|
class IP(NamedTuple):
|
|
dt: datetime
|
|
addr: str # an IP address
|
|
|
|
# TODO: could cache? not sure if it's worth it
|
|
def ipgeocache(self) -> Json:
|
|
return ipgeocache.get(self.addr)
|
|
|
|
@property
|
|
def latlon(self) -> Tuple[float, float]:
|
|
loc: str = self.ipgeocache()["loc"]
|
|
lat, _, lon = loc.partition(",")
|
|
return float(lat), float(lon)
|
|
|
|
@property
|
|
def tzname(self) -> str:
|
|
tz: str = self.ipgeocache()["timezone"]
|
|
return tz
|
|
|
|
|
|
def drop_private(ips: Iterator[IP]) -> Iterator[IP]:
|
|
"""
|
|
Helper function that can be used to filter out private IPs
|
|
"""
|
|
for ip in ips:
|
|
if ipaddress.ip_address(ip.addr).is_private:
|
|
continue
|
|
yield ip
|