wrongmove/services/route_calculator.py
Viktor Barzin eafbc1ac52
Flatten repo structure: move crawler/ to root, remove vqa/ and immoweb/
The crawler subdirectory was the only active project. Moving it to the
repo root simplifies paths and removes the unnecessary nesting. The
vqa/ and immoweb/ directories were legacy/unused and have been removed.

Updated .drone.yml, .gitignore, .claude/ docs, and skills to reflect
the new flat structure.
2026-02-07 23:01:20 +00:00

71 lines
2.4 KiB
Python

"""Route calculator service - calculates transit routes using Google Maps API."""
from models.listing import DestinationMode, Route, RouteLegStep
from repositories.listing_repository import ListingRepository
from tqdm.asyncio import tqdm
from rec import routing
from models import Listing
def _parse_duration(duration_str: str) -> int:
"""Parse a duration string like '123s' to integer seconds."""
return int(duration_str.rstrip("s"))
async def calculate_route(
repository: ListingRepository,
destination_address: str,
travel_mode: routing.TravelMode,
limit: int | None = None,
) -> None:
"""Calculate transit routes for listings to a destination."""
listings = await repository.get_listings()
if limit is not None:
listings = listings[:limit]
destination_mode = DestinationMode(destination_address, travel_mode)
updated_listings = await tqdm.gather(
*[update_routing_info(listing, destination_mode) for listing in listings],
total=len(listings),
desc="Updating routing info",
)
await repository.upsert_listings(
[listing for listing in updated_listings if listing is not None]
)
async def update_routing_info(
listing: Listing, destination_mode: DestinationMode
) -> Listing | None:
"""Update routing information for a single listing."""
if listing.routing_info.get(destination_mode) is not None:
# already calculated, do not recompute to save API calls
return None
routes_data = routing.transit_route(
listing.latitude,
listing.longitude,
destination_mode.destination_address,
destination_mode.travel_mode,
)
routes: list[Route] = []
for route_data in routes_data["routes"]:
duration_s = _parse_duration(route_data["duration"])
route = Route(
legs=[
RouteLegStep(
distance_meters=step_data["distanceMeters"],
duration_s=_parse_duration(step_data["staticDuration"]),
travel_mode=routing.TravelMode(step_data["travelMode"]),
)
for step_data in route_data["legs"][0]["steps"]
],
distance_meters=route_data["distanceMeters"],
duration_s=duration_s,
)
routes.append(route)
listing.routing_info_json = listing.serialize_routing_info(
{**listing.routing_info, **{destination_mode: routes}}
)
return listing