properties.py 1.7 KB

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