| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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('''
- <style>
- img {width: 900px}
- td {padding-right: 20px}
- #top {
- position: fixed;
- top: 0;
- left: 0;
- z-index: 999;
- width: 100%;
- height: 23px;
- }
- </style>
- ''')
- f.write('<h2>%s</h2>\n' % address)
- # f.write('<div id="top">')
- f.write('<h2>')
- f.write('<span style="background-color: %s">%sk</span>, ' % (_color(ranking.r_price(price/1000)), int(price/1000)))
- f.write('<span style="background-color: %s">%s m<sup>2</sup></span>, ' % (_color(ranking.r_size(size)), size))
- f.write('<span style="background-color: %s">%s br</span> / ' % (_color(ranking.r_bedrooms(bedrooms)), bedrooms))
- f.write('<span style="background-color: %s">%s b</span>, ' % (_color(ranking.r_bathrooms(bathrooms)), bathrooms))
- f.write('<span style="background-color: %s">%s</span>' % (_color(ranking.r_ber(ber)), ber))
- f.write('</h2><br>\n')
- # f.write('</div>')
- f.write('<a href="https://www.myhome.ie/_/brochure/_/%i">myhome.ie</a>' % pid)
- f.write(', <a href="https://www.google.ie/maps/search/%s">maps.ie</a>' % address)
- if search_rows:
- f.write('<p><hr><p>')
- f.write('<table><tr><td><b>Time</b></td><td><b>Price</b></td></tr>')
- for ts, price in search_rows:
- f.write('<tr><td>%s</td><td>%s</td></tr>' % (ts, int(int(price)/1000) if price and price.isdigit() else price))
- f.write('</table>')
-
- f.write('<p><hr><p>')
- seen = set()
- if data.get('MainPhoto'):
- main_photo = data['MainPhoto']
- seen.add(main_photo)
- f.write('<img src="%s"><br>\n' % main_photo)
- for i, img in enumerate(data.get('FloorPlans', [])):
- seen.add(img)
- f.write('<img src="%s">\n' % img)
- if (i+1) % 2 == 0:
- f.write('<br>')
- f.write('<br>')
- i = 0
- for img in data.get('Images', []):
- if img['Url'] in seen:
- continue
- f.write('<img src="%s">\n' % img['Url'])
- if (i+1) % 2 == 0:
- f.write('<br>')
- 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('''
- <style>
- img {width: 500px}
- </style>
- ''')
- for pid, ts, price in rows:
- row = run_single(conn, pid)
- size, ber, address, main_photo, bedrooms, bathrooms, price = row
- f.write('<a href="%i.html"><h2>%s</h2></a>' % (pid, address))
- f.write('<h2>%sk, %s m<sup>2</sup>, %s br / %s b, %s</h2>\n' % (int(int(price)/1000), size, bedrooms, bathrooms, ber))
- if main_photo:
- f.write('<a href="%i.html"><img src="%s"></a>' % (pid, main_photo))
- if not _test_url(main_photo):
- bad_pids.append(pid)
- f.write('<hr>')
- 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')
|