Add services layer, tests, streaming UI, and cleanup legacy code
This commit is contained in:
parent
5514fa6381
commit
d205d15c74
62 changed files with 3729 additions and 1024 deletions
92
crawler/services/export_service.py
Normal file
92
crawler/services/export_service.py
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
"""Unified export service - shared between CLI and HTTP API.
|
||||
|
||||
This module provides export functionality for listings in various formats.
|
||||
"""
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from models.listing import QueryParameters
|
||||
from repositories.listing_repository import ListingRepository
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExportResult:
|
||||
"""Result of an export operation."""
|
||||
success: bool
|
||||
output_path: str | None # For file exports
|
||||
data: Any | None # For in-memory exports (GeoJSON)
|
||||
record_count: int
|
||||
message: str
|
||||
|
||||
|
||||
async def export_to_csv(
|
||||
repository: ListingRepository,
|
||||
output_path: Path,
|
||||
query_parameters: QueryParameters | None = None,
|
||||
) -> ExportResult:
|
||||
"""Export listings to CSV file.
|
||||
|
||||
Used by:
|
||||
- CLI: export-csv
|
||||
- API: (could be added as download endpoint)
|
||||
"""
|
||||
from csv_exporter import export_to_csv as _export_csv
|
||||
|
||||
await _export_csv(repository, output_path, query_parameters)
|
||||
|
||||
listings = await repository.get_listings(query_parameters=query_parameters)
|
||||
return ExportResult(
|
||||
success=True,
|
||||
output_path=str(output_path),
|
||||
data=None,
|
||||
record_count=len(listings),
|
||||
message=f"Exported {len(listings)} listings to {output_path}",
|
||||
)
|
||||
|
||||
|
||||
async def export_to_geojson(
|
||||
repository: ListingRepository,
|
||||
query_parameters: QueryParameters | None = None,
|
||||
output_path: Path | None = None,
|
||||
limit: int | None = None,
|
||||
) -> ExportResult:
|
||||
"""Export listings to GeoJSON format.
|
||||
|
||||
Args:
|
||||
repository: Database repository
|
||||
query_parameters: Filtering parameters
|
||||
output_path: If provided, write to file. Otherwise return data.
|
||||
limit: Maximum number of listings to export
|
||||
|
||||
Used by:
|
||||
- CLI: export-immoweb
|
||||
- API: GET /api/listing_geojson
|
||||
"""
|
||||
from ui_exporter import export_immoweb
|
||||
|
||||
geojson_data = await export_immoweb(
|
||||
repository,
|
||||
output_file=str(output_path) if output_path else None,
|
||||
query_parameters=query_parameters,
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
feature_count = len(geojson_data.get("features", [])) if geojson_data else 0
|
||||
|
||||
if output_path:
|
||||
return ExportResult(
|
||||
success=True,
|
||||
output_path=str(output_path),
|
||||
data=None,
|
||||
record_count=feature_count,
|
||||
message=f"Exported {feature_count} listings to {output_path}",
|
||||
)
|
||||
|
||||
return ExportResult(
|
||||
success=True,
|
||||
output_path=None,
|
||||
data=geojson_data,
|
||||
record_count=feature_count,
|
||||
message=f"Generated GeoJSON with {feature_count} features",
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue