wrongmove/crawler/ui_exporter.py

47 lines
1.6 KiB
Python

import json
import pathlib
from data_access import Listing
async def export_immoweb(ctx, output_file: str):
data_dir = ctx.obj['data_dir']
output_file_path = pathlib.Path(output_file)
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])
# 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(output_file_path, 'w') as f:
f.write(result)
# json.dump(serialized_data, f, indent=4)