55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
import json
|
|
import pathlib
|
|
|
|
from rec.query import QueryParameters
|
|
from repositories.listing_repository import ListingRepository
|
|
|
|
|
|
async def export_immoweb(
|
|
repository: ListingRepository,
|
|
output_file: str,
|
|
query_parameters: QueryParameters | 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,
|
|
)
|
|
|
|
# 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,
|
|
# Additional info; the above is GeoJSON format
|
|
# Below is all other crap we want in the UI
|
|
"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)
|
|
with open(str(output_file_path), "w") as f:
|
|
f.write(result)
|
|
# json.dump(serialized_data, f, indent=4)
|