import db import geo_util import config def load_properties(require_active, require_update_since): ts = db.datetime_to_str(require_update_since) with db.connect() as conn: favourites = set(conn.select('select id from annotations where favourited=1')) favourites_str = ','.join([str(x) for x in favourites]) rows = conn.select( 'select id, lat, lon, size, price, bedrooms, ber, bathrooms, property_type from properties ' 'where (price<%i and last_update > "%s") or (id in (%s))' % (config.MAX_PRICE, ts, favourites_str) ) res = {} for property_id, lat, lon, size, price, bedrooms, ber, bathrooms, property_type in rows: if not price or not lat or not lon: continue if not size: size = 0 if not any(geo_util.inside((lat, lon), bbox) for bbox in config.MONITORED_BBOXES.values()): continue price = price/1000 if isinstance(bedrooms, str) and bedrooms.endswith(' bed'): bedrooms = int(bedrooms[:-4]) res[property_id] = { 'id': property_id, 'loc': (lat, lon), 'size': size, 'price': price, 'bedrooms': bedrooms, 'bathrooms': bathrooms, 'price_history': [], 'ber': ber, 'type': property_type, 'favourite': property_id in favourites, } rows = conn.select('select time, property_id, price from search_results order by time asc') for time, pid, price in rows: if pid not in res: continue if pid not in favourites and time < ts: del res[pid] continue if price.isdigit(): price = int(price) res[pid]['price'] = price/1000 res[pid]['price_history'].append(price) res[pid]['ts'] = time if require_active: res = {k: v for k, v in res.items() if v['price_history'] and v['price_history'][-1] != 'GONE'} return res