properties.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import db
  2. import geo_util
  3. import config
  4. def load_properties(require_active, require_update_since):
  5. ts = db.datetime_to_str(require_update_since)
  6. with db.connect() as conn:
  7. rows = conn.select('select id, lat, lon, size, price, bedrooms, ber, bathrooms, property_type from properties '
  8. 'where price<%i and last_update > "%s"' % (config.MAX_PRICE, ts))
  9. res = {}
  10. for property_id, lat, lon, size, price, bedrooms, ber, bathrooms, property_type in rows:
  11. if not price or not size or not lat or not lon:
  12. continue
  13. if not any(geo_util.inside((lat, lon), bbox) for bbox in config.MONITORED_BBOXES.values()):
  14. continue
  15. price = price/1000
  16. if isinstance(bedrooms, str) and bedrooms.endswith(' bed'):
  17. bedrooms = int(bedrooms[:-4])
  18. res[property_id] = {
  19. 'id': property_id,
  20. 'loc': (lat, lon),
  21. 'size': size,
  22. 'price': price,
  23. 'bedrooms': bedrooms,
  24. 'bathrooms': bathrooms,
  25. 'price_history': [],
  26. 'ber': ber,
  27. 'type': property_type,
  28. }
  29. rows = conn.select('select time, property_id, price from search_results order by time asc')
  30. for time, pid, price in rows:
  31. if pid not in res:
  32. continue
  33. if time < ts:
  34. del res[pid]
  35. continue
  36. if price.isdigit():
  37. price = int(price)
  38. res[pid]['price'] = price/1000
  39. res[pid]['price_history'].append(price)
  40. res[pid]['ts'] = time
  41. if require_active:
  42. res = {k: v for k, v in res.items() if v['price_history'] and v['price_history'][-1] != 'GONE'}
  43. return res