54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
import json
|
|
import pathlib
|
|
|
|
from data_access import Listing
|
|
from rec.query import QueryParameters, filter_listings
|
|
|
|
|
|
async def export_immoweb(
|
|
ctx,
|
|
output_file: str,
|
|
query_parameters: QueryParameters | None = None,
|
|
):
|
|
data_dir = ctx.obj["data_dir"]
|
|
output_file_path = pathlib.Path(output_file)
|
|
output_file_path.touch(exist_ok=True)
|
|
listing_paths = sorted(list(pathlib.Path(data_dir).glob("*/listing.json")))
|
|
# listing_paths = listing_paths[:10]
|
|
listings = Listing.get_all_listings([str(path) for path in listing_paths])
|
|
if query_parameters is not None:
|
|
listings = await filter_listings(listings, 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": await listing.sqm_ocr(),
|
|
"qmprice": round(await listing.price_per_sqm(), 2),
|
|
"rooms": listing.bedrooms,
|
|
"total_price": listing.price,
|
|
"url": listing.url,
|
|
# Additional info; the above is GeoJSON format
|
|
# Below is all other crap we want in the UI
|
|
"info": await listing.dict_nicely(),
|
|
},
|
|
"geometry": {
|
|
"coordinates": [
|
|
listing.longitude,
|
|
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)
|