properties.py 2.1 KB

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