66 lines
2.3 KiB
Python
66 lines
2.3 KiB
Python
import json
|
|
import logging
|
|
import pathlib
|
|
|
|
from rec.query import QueryParameters
|
|
from repositories.listing_repository import ListingRepository
|
|
|
|
logger = logging.getLogger("uvicorn.error")
|
|
|
|
|
|
async def export_immoweb(
|
|
repository: ListingRepository,
|
|
output_file: str | None = None,
|
|
query_parameters: QueryParameters | None = None,
|
|
limit: int | None = None,
|
|
):
|
|
listings = await repository.get_listings(
|
|
query_parameters=query_parameters,
|
|
limit=limit,
|
|
)
|
|
logger.info(f"Fetched {len(listings)} listings")
|
|
|
|
# Convert listings to immoweb format
|
|
immoweb_listings = []
|
|
for listing in listings:
|
|
immoweb_listing = {
|
|
"type": "Feature",
|
|
"properties": {
|
|
"city": "London", # change me
|
|
"country": "United Kingdom",
|
|
"qm": listing.square_meters,
|
|
"qmprice": listing.price_per_square_meter,
|
|
"rooms": listing.number_of_bedrooms,
|
|
"total_price": listing.price,
|
|
"url": listing.url,
|
|
"photo_thumbnail": listing.photo_thumbnail,
|
|
"last_seen": listing.last_seen.isoformat(),
|
|
"price_history": [item.to_dict() for item in listing.price_history],
|
|
"agency": listing.agency,
|
|
"available_from": listing.additional_info["property"].get(
|
|
"letDateAvailable", None
|
|
),
|
|
# All other crap can be found in additional_info
|
|
# Prefer pulling out fields here instead of exporting the entire additional_info
|
|
# "info": listing.additional_info,
|
|
},
|
|
"geometry": {
|
|
"coordinates": [
|
|
listing.longtitude,
|
|
listing.latitude,
|
|
],
|
|
"type": "Point",
|
|
},
|
|
}
|
|
immoweb_listings.append(immoweb_listing)
|
|
|
|
prefix = "var data = "
|
|
serialized_data = {"type": "FeatureCollection", "features": immoweb_listings}
|
|
result = prefix + json.dumps(serialized_data, 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
|