import json import sys import webbrowser import requests import db import monitor import ranking def _test_url(url): r = requests.head(url) return r.status_code < 300 def _color(ratio): # for ratio on scale [0, 1], return a color on the scale [red, white, green] if ratio < 0.5: num = 150+200*ratio num_hex = hex(int(round(num)))[-2:] return 'FF%s%s' % (num_hex, num_hex) else: num = 250-(ratio-0.5)*200 num_hex = hex(int(round(num)))[-2:] return '%sFF%s' % (num_hex, num_hex) def run_single(conn, pid, check_bad_urls=False): rows = conn.select('select raw_json, price, lat, lon, size, ber, address, property_type, bathrooms, bedrooms from properties where id=?', pid) if not rows: raise ValueError('Could not find a property with id %', pid) raw_json, price, lat, lon, size, ber, address, property_type, bathrooms, bedrooms = rows[0] price = int(price) if price is not None else price size = int(size) if size is not None else size bedrooms = int(bedrooms) if bedrooms is not None else bedrooms bathrooms = int(bathrooms) if bathrooms is not None else bathrooms data = json.loads(raw_json) search_rows = conn.select('select time, price from search_results where property_id=? order by time asc', pid) main_photo = None fname = 'htmls/%i.html' % pid with open(fname, 'w') as f: f.write(''' ''') f.write('

%s

\n' % address) # f.write('
') f.write('

') f.write('%sk, ' % (_color(ranking.r_price(price/1000)), int(price/1000))) f.write('%s m2, ' % (_color(ranking.r_size(size)), size)) f.write('%s br / ' % (_color(ranking.r_bedrooms(bedrooms)), bedrooms)) f.write('%s b, ' % (_color(ranking.r_bathrooms(bathrooms)), bathrooms)) f.write('%s' % (_color(ranking.r_ber(ber)), ber)) f.write('


\n') # f.write('
') f.write('myhome.ie' % pid) f.write(', maps.ie' % address) if search_rows: f.write('


') f.write('') for ts, price in search_rows: f.write('' % (ts, int(int(price)/1000) if price and price.isdigit() else price)) f.write('
TimePrice
%s%s
') f.write('


') seen = set() if data.get('MainPhoto'): main_photo = data['MainPhoto'] seen.add(main_photo) f.write('
\n' % main_photo) for i, img in enumerate(data.get('FloorPlans', [])): seen.add(img) f.write('\n' % img) if (i+1) % 2 == 0: f.write('
') f.write('
') i = 0 for img in data.get('Images', []): if img['Url'] in seen: continue f.write('\n' % img['Url']) if (i+1) % 2 == 0: f.write('
') i += 1 if check_bad_urls and main_photo and not _test_url(main_photo): print('Found bad photo URL. Refetching.') monitor.fetch_properties(conn, property_ids=[pid]) monitor.process_properties(conn) return run_single(conn, pid) return size, ber, address, main_photo, bedrooms, bathrooms, price def run(conn, retry_bad_pids=True): rows = conn.select(''' select a.id, a.update_ts, p.price from annotations a join properties p on a.id = p.id where a.favourited=1 order by a.update_ts desc ''') if not rows: raise Exception('No favourited properties.') bad_pids = [] fname = 'htmls/index.html' with open(fname, 'w') as f: f.write(''' ''') for pid, ts, price in rows: row = run_single(conn, pid) size, ber, address, main_photo, bedrooms, bathrooms, price = row price = ('%sk' % int(int(price)/1000)) if price != 'GONE' else price f.write('

%s

' % (pid, address)) f.write('

%s, %s m2, %s br / %s b, %s

\n' % (price, size, bedrooms, bathrooms, ber)) if main_photo: f.write('' % (pid, main_photo)) if not _test_url(main_photo): bad_pids.append(pid) f.write('
') print('Found %i properties with bad photos' % len(bad_pids)) if bad_pids and retry_bad_pids: monitor.fetch_properties(conn, property_ids=bad_pids) monitor.process_properties(conn) return run(conn, retry_bad_pids=False) return fname def _get_pid(conn, possible_pid): if possible_pid > 100000: return possible_pid rows = conn.select('select id from properties where tmp_id=?', possible_pid) if not rows: raise ValueError('Could not find a pid for tmp_id=%i' % possible_pid) return rows[0] if __name__ == '__main__': with db.connect() as conn: if len(sys.argv) > 1: for pid in sys.argv[1:]: pid = int(pid) pid = _get_pid(conn, pid) fname = run_single(conn, pid, check_bad_urls=True)[0] webbrowser.open('htmls/%i.html' % pid) else: fname = run(conn) webbrowser.open('htmls/index.html')