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('
') f.write('
| Time | Price |
| %s | %s |
')
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
f.write('%s
' % (pid, address))
f.write('