209 lines
7.3 KiB
Python
209 lines
7.3 KiB
Python
"""Service layer for cache-aware fetching of FINN ads and Eiendom.no units."""
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from .ad import fetch_ad_details
|
|
from .analysis import analyze_search as run_analysis_search
|
|
from .cache import (
|
|
get_eiendom_unit as get_cached_eiendom_unit,
|
|
get_finn_ad,
|
|
init_db,
|
|
save_eiendom_unit,
|
|
save_finn_ad,
|
|
)
|
|
from .config import FINN_CACHE_PATH
|
|
from .eiendom_no import (
|
|
build_unit_vector,
|
|
decode_unit_vector,
|
|
get_similar_units,
|
|
get_unit,
|
|
search_unit_from_finn_url,
|
|
)
|
|
from .feedback import save_feedback as save_feedback_impl
|
|
from .models import EiendomUnit, FinnAd, SimilarUnit
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def get_or_fetch_ad(finnkode: str, force_refresh: bool = False) -> FinnAd:
|
|
"""Get FinnAd from cache or fetch fresh. Never returns None."""
|
|
conn = init_db(FINN_CACHE_PATH)
|
|
ad = None if force_refresh else get_finn_ad(conn, finnkode, ttl_hours=24)
|
|
if ad is None:
|
|
ad = await fetch_ad_details(finnkode)
|
|
save_finn_ad(conn, ad)
|
|
return ad
|
|
|
|
|
|
async def get_or_fetch_eiendom_unit(
|
|
unit_code: str, force_refresh: bool = False
|
|
) -> EiendomUnit | None:
|
|
"""Get EiendomUnit from cache or fetch fresh."""
|
|
conn = init_db(FINN_CACHE_PATH)
|
|
unit = None if force_refresh else get_cached_eiendom_unit(conn, unit_code, ttl_hours=24)
|
|
if unit is None:
|
|
unit = await get_unit(unit_code)
|
|
if unit is not None:
|
|
save_eiendom_unit(conn, unit)
|
|
return unit
|
|
|
|
|
|
async def get_or_fetch_similar_units(
|
|
unit_code: str, listing_status: str = "RECENTLY_SOLD", force_refresh: bool = False
|
|
) -> list[SimilarUnit]:
|
|
"""Get similar units (comps) from cache or fetch fresh."""
|
|
# Similar units don't have a separate cache table; fetch fresh each time per PRD
|
|
# (or cache them in search_runs if doing diff detection)
|
|
return await get_similar_units(unit_code, listing_status=listing_status)
|
|
|
|
|
|
async def resolve_eiendom_unit_from_finn_url(finn_url: str) -> EiendomUnit | None:
|
|
"""Resolve an Eiendom.no unit from a FINN listing URL."""
|
|
return await search_unit_from_finn_url(finn_url)
|
|
|
|
|
|
# ============================================================================
|
|
# Orchestration functions — delegate to analysis.py
|
|
# ============================================================================
|
|
|
|
|
|
async def analyze_search(
|
|
search_url: str,
|
|
*,
|
|
max_pages: int = 3,
|
|
detail_limit: int = 20,
|
|
include_details: bool = True,
|
|
include_eiendom_no: bool = True,
|
|
include_similar_units_for_shortlist: bool = False,
|
|
) -> dict[str, Any]:
|
|
"""Analyze a FINN search URL and return a ranked shortlist."""
|
|
return await run_analysis_search(
|
|
search_url,
|
|
max_pages=max_pages,
|
|
fetch_details=include_details,
|
|
detail_limit=detail_limit,
|
|
include_eiendom_no=include_eiendom_no,
|
|
include_similar_units_for_shortlist=include_similar_units_for_shortlist,
|
|
)
|
|
|
|
|
|
async def analyze_ad(
|
|
finnkode: str,
|
|
*,
|
|
include_eiendom_no: bool = True,
|
|
include_similar_units: bool = False,
|
|
) -> dict[str, Any]:
|
|
"""Fetch and enrich a single FINN ad with analysis."""
|
|
ad = await get_or_fetch_ad(finnkode)
|
|
result: dict[str, Any] = {
|
|
"ad": ad.model_dump(),
|
|
}
|
|
if include_eiendom_no and ad.eiendom_unit_code:
|
|
unit = await get_or_fetch_eiendom_unit(ad.eiendom_unit_code)
|
|
if unit:
|
|
result["eiendom_unit"] = unit.model_dump()
|
|
if include_similar_units:
|
|
similar = await get_or_fetch_similar_units(ad.eiendom_unit_code)
|
|
result["similar_units"] = [s.model_dump() for s in similar]
|
|
return result
|
|
|
|
|
|
async def analyze_ad_against_comps(
|
|
finnkode: str, listing_status: str = "RECENTLY_SOLD"
|
|
) -> dict[str, Any]:
|
|
"""Evaluate one listing against recent comparable sales."""
|
|
ad = await get_or_fetch_ad(finnkode)
|
|
result: dict[str, Any] = {
|
|
"ad": ad.model_dump(),
|
|
}
|
|
if ad.eiendom_unit_code:
|
|
unit = await get_or_fetch_eiendom_unit(ad.eiendom_unit_code)
|
|
if unit:
|
|
result["eiendom_unit"] = unit.model_dump()
|
|
comps = await get_or_fetch_similar_units(
|
|
ad.eiendom_unit_code, listing_status=listing_status
|
|
)
|
|
result["comparable_units"] = [c.model_dump() for c in comps]
|
|
return result
|
|
|
|
|
|
async def find_similar_to_liked(
|
|
finnkode: str, *, mode: str = "recommendations", listing_status: str = "FOR_SALE"
|
|
) -> dict[str, Any]:
|
|
"""Find properties similar to a listing the user has liked."""
|
|
# Requires that feedback.verdict = "liked" exists for this finnkode
|
|
ad = await get_or_fetch_ad(finnkode)
|
|
if not ad.eiendom_unit_code:
|
|
raise ValueError(
|
|
f"Finnkode {finnkode} has no Eiendom.no unit_code; cannot find similar properties"
|
|
)
|
|
|
|
# TODO: verify feedback verdict = "liked" exists
|
|
unit = await get_or_fetch_eiendom_unit(ad.eiendom_unit_code)
|
|
if not unit:
|
|
raise ValueError(f"Cannot enrich finnkode {finnkode} with Eiendom.no data")
|
|
|
|
similar = await get_or_fetch_similar_units(ad.eiendom_unit_code, listing_status=listing_status)
|
|
return {
|
|
"base_ad": ad.model_dump(),
|
|
"similar_listings": [s.model_dump() for s in similar],
|
|
"mode": mode,
|
|
}
|
|
|
|
|
|
async def compare_ads(
|
|
finnkoder: list[str], *, include_eiendom_no: bool = True, include_comps: bool = True
|
|
) -> dict[str, Any]:
|
|
"""Compare multiple FINN listings side by side."""
|
|
ads = []
|
|
for finnkode in finnkoder:
|
|
ad = await get_or_fetch_ad(finnkode)
|
|
ad_data = ad.model_dump()
|
|
|
|
if include_eiendom_no and ad.eiendom_unit_code:
|
|
unit = await get_or_fetch_eiendom_unit(ad.eiendom_unit_code)
|
|
if unit:
|
|
ad_data["eiendom_unit"] = unit.model_dump()
|
|
if include_comps:
|
|
comps = await get_or_fetch_similar_units(
|
|
ad.eiendom_unit_code, listing_status="RECENTLY_SOLD"
|
|
)
|
|
ad_data["comps"] = [c.model_dump() for c in comps]
|
|
|
|
ads.append(ad_data)
|
|
|
|
return {"listings": ads}
|
|
|
|
|
|
# ============================================================================
|
|
# Helper functions
|
|
# ============================================================================
|
|
|
|
|
|
def build_unit_vector_for_unit_code(unit_code: str) -> dict[str, Any]:
|
|
"""Build a unit_vector dict for a unit_code (msgpack-encoded)."""
|
|
return build_unit_vector(unit_code)
|
|
|
|
|
|
def decode_unit_vector_to_dict(unit_vector: str) -> dict[str, Any]:
|
|
"""Decode a unit_vector string to a dict."""
|
|
return decode_unit_vector(unit_vector)
|
|
|
|
|
|
def save_feedback(finnkode: str, verdict: str, notes: str | None = None) -> dict[str, Any]:
|
|
"""Store user feedback/verdict for a listing."""
|
|
return save_feedback_impl(finnkode, verdict, notes)
|
|
|
|
|
|
def get_shortlist(run_id: int | None = None, limit: int = 10) -> dict[str, Any]:
|
|
"""Fetch stored shortlist from a search run."""
|
|
# TODO: implement via search_runs table in cache.py
|
|
return {"shortlist": [], "run_id": run_id, "limit": limit}
|
|
|
|
|
|
def get_new_ads_since_last_run(search_url: str) -> dict[str, Any]:
|
|
"""Detect new/removed/changed listings vs the previous run."""
|
|
# TODO: implement via search_runs table in cache.py
|
|
return {"new_ads": [], "removed_ads": [], "changed_ads": [], "search_url": search_url}
|