From b995bc228633025cf1d60e5ae8c57b7d5fc6d09e Mon Sep 17 00:00:00 2001 From: Viktor Barzin Date: Sun, 15 Jun 2025 12:42:56 +0000 Subject: [PATCH] add api endpoint for geojson data fetching --- crawler/api/app.py | 8 ++++++++ crawler/ui_exporter.py | 16 ++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crawler/api/app.py b/crawler/api/app.py index ecc24da..da56826 100644 --- a/crawler/api/app.py +++ b/crawler/api/app.py @@ -7,6 +7,7 @@ from repositories.listing_repository import ListingRepository from repositories.listing_repository import ListingRepository from database import engine from fastapi.middleware.cors import CORSMiddleware +from ui_exporter import export_immoweb app = FastAPI() @@ -25,3 +26,10 @@ async def get_listing(user: Annotated[User, Depends(get_current_user)]): repository = ListingRepository(engine) listings = await repository.get_listings(limit=5) return {"listings": listings} + + +@app.get("/api/listing_geojson") +async def get_listing_geojson(user: Annotated[User, Depends(get_current_user)]): + repository = ListingRepository(engine) + geojson_data = await export_immoweb(repository, limit=500) + return geojson_data diff --git a/crawler/ui_exporter.py b/crawler/ui_exporter.py index 1d89ad0..c8afb41 100644 --- a/crawler/ui_exporter.py +++ b/crawler/ui_exporter.py @@ -7,13 +7,13 @@ from repositories.listing_repository import ListingRepository async def export_immoweb( repository: ListingRepository, - output_file: str, + output_file: str | None = None, query_parameters: QueryParameters | None = None, + limit: int | None = None, ): - output_file_path = pathlib.Path(output_file) - output_file_path.touch(exist_ok=True) listings = await repository.get_listings( query_parameters=query_parameters, + limit=limit, ) # Convert listings to immoweb format @@ -53,6 +53,10 @@ async def export_immoweb( prefix = "var data = " serialized_data = {"type": "FeatureCollection", "features": immoweb_listings} result = prefix + json.dumps(serialized_data, indent=4) - with open(str(output_file_path), "w") as f: - f.write(result) - # json.dump(serialized_data, f, indent=4) + + if output_file: + output_file_path = pathlib.Path(output_file) + output_file_path.touch(exist_ok=True) + with open(str(output_file_path), "w") as f: + f.write(result) + return serialized_data