properties.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import db
  2. import geo_util
  3. import config
  4. def load_properties(require_active=True):
  5. with db.connect() as conn:
  6. rows = conn.select('select id, lat, lon, size, price, bedrooms, ber from properties where price<550000 and property_type != "Site"')
  7. res = {}
  8. for property_id, lat, lon, size, price, bedrooms, ber in rows:
  9. if not price or not size or not lat or not lon:
  10. continue
  11. if not any(geo_util.inside((lat, lon), bbox) for bbox in config.MONITORED_BBOXES):
  12. continue
  13. price = price/1000
  14. res[property_id] = {
  15. 'loc': (lat, lon),
  16. 'size': size,
  17. 'price': price,
  18. 'bedrooms': bedrooms,
  19. 'price_history': [],
  20. 'ber': ber,
  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'] = price/1000
  29. res[pid]['price_history'].append(price)
  30. if require_active:
  31. res = {k: v for k, v in res.items() if v['price_history'] and v['price_history'][-1] != 'GONE'}
  32. return res