properties.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import db
  2. import geo_util
  3. import config
  4. import ranking
  5. def load_properties(require_active=True):
  6. with db.connect() as conn:
  7. rows = conn.select('select id, lat, lon, size, price, bedrooms from properties where price<450000')
  8. res = {}
  9. for property_id, lat, lon, size, price, bedrooms in rows:
  10. if not price or not size or not lat or not lon:
  11. continue
  12. if not any(geo_util.inside((lat, lon), bbox) for bbox in config.MONITORED_BBOXES):
  13. continue
  14. price = price/1000
  15. res[property_id] = {
  16. 'loc': (lat, lon),
  17. 'size': size,
  18. 'price': price,
  19. 'bedrooms': bedrooms,
  20. 'price_history': [],
  21. }
  22. rows = conn.select('select property_id, price from search_results order by time asc')
  23. for pid, price in rows:
  24. if pid not in res:
  25. continue
  26. if price.isdigit():
  27. price = int(price)
  28. res[pid]['price_history'].append(price)
  29. if require_active:
  30. res = {k: v for k, v in res.items() if v['price_history'] and v['price_history'][-1] != 'GONE'}
  31. return res