properties.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 lat or not lon:
  12. continue
  13. if not size:
  14. size = 0
  15. if not any(geo_util.inside((lat, lon), bbox) for bbox in config.MONITORED_BBOXES.values()):
  16. continue
  17. price = price/1000
  18. if isinstance(bedrooms, str) and bedrooms.endswith(' bed'):
  19. bedrooms = int(bedrooms[:-4])
  20. res[property_id] = {
  21. 'id': property_id,
  22. 'loc': (lat, lon),
  23. 'size': size,
  24. 'price': price,
  25. 'bedrooms': bedrooms,
  26. 'bathrooms': bathrooms,
  27. 'price_history': [],
  28. 'ber': ber,
  29. 'type': property_type,
  30. }
  31. rows = conn.select('select time, property_id, price from search_results order by time asc')
  32. for time, pid, price in rows:
  33. if pid not in res:
  34. continue
  35. if time < ts:
  36. del res[pid]
  37. continue
  38. if price.isdigit():
  39. price = int(price)
  40. res[pid]['price'] = price/1000
  41. res[pid]['price_history'].append(price)
  42. res[pid]['ts'] = time
  43. if require_active:
  44. res = {k: v for k, v in res.items() if v['price_history'] and v['price_history'][-1] != 'GONE'}
  45. return res