create_htmls.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import json
  2. import sys
  3. import webbrowser
  4. import requests
  5. import db
  6. import monitor
  7. import ranking
  8. def _test_url(url):
  9. r = requests.head(url)
  10. return r.status_code < 300
  11. def _color(ratio):
  12. # for ratio on scale [0, 1], return a color on the scale [red, white, green]
  13. if ratio < 0.5:
  14. num = 150+200*ratio
  15. num_hex = hex(int(round(num)))[-2:]
  16. return 'FF%s%s' % (num_hex, num_hex)
  17. else:
  18. num = 250-(ratio-0.5)*200
  19. num_hex = hex(int(round(num)))[-2:]
  20. return '%sFF%s' % (num_hex, num_hex)
  21. def run_single(conn, pid, check_bad_urls=False):
  22. rows = conn.select('select raw_json, price, lat, lon, size, ber, address, property_type, bathrooms, bedrooms from properties where id=?', pid)
  23. if not rows:
  24. raise ValueError('Could not find a property with id %', pid)
  25. raw_json, price, lat, lon, size, ber, address, property_type, bathrooms, bedrooms = rows[0]
  26. data = json.loads(raw_json)
  27. search_rows = conn.select('select time, price from search_results where property_id=? order by time asc', pid)
  28. main_photo = None
  29. fname = 'htmls/%i.html' % pid
  30. with open(fname, 'w') as f:
  31. f.write('''
  32. <style>
  33. img {width: 900px}
  34. td {padding-right: 20px}
  35. </style>
  36. ''')
  37. f.write('<h2>%s</h2>\n' % address)
  38. f.write('<h2>')
  39. f.write('<span style="background-color: %s">%sk</span>, ' % (_color(ranking.r_price(int(price)/1000)), int(price/1000)))
  40. f.write('<span style="background-color: %s">%s m<sup>2</sup></span>, ' % (_color(ranking.r_size(int(size))), size))
  41. f.write('<span style="background-color: %s">%s br</span> / ' % (_color(ranking.r_bedrooms(int(bedrooms))), bedrooms))
  42. f.write('<span style="background-color: %s">%s b</span>, ' % (_color(ranking.r_bathrooms(int(bathrooms))), bathrooms))
  43. f.write('<span style="background-color: %s">%s</span>' % (_color(ranking.r_ber(ber)), ber))
  44. f.write('</h2><br>\n')
  45. f.write('<a href="https://www.myhome.ie/_/brochure/_/%i">myhome.ie</a>' % pid)
  46. f.write(', <a href="https://www.google.ie/maps/search/%s">maps.ie</a>' % address)
  47. if search_rows:
  48. f.write('<p><hr><p>')
  49. f.write('<table><tr><td><b>Time</b></td><td><b>Price</b></td></tr>')
  50. for ts, price in search_rows:
  51. f.write('<tr><td>%s</td><td>%s</td></tr>' % (ts, int(int(price)/1000) if price != 'GONE' else price))
  52. f.write('</table>')
  53. f.write('<p><hr><p>')
  54. seen = set()
  55. if data.get('MainPhoto'):
  56. main_photo = data['MainPhoto']
  57. seen.add(main_photo)
  58. f.write('<img src="%s"><br>\n' % main_photo)
  59. for i, img in enumerate(data.get('FloorPlans', [])):
  60. seen.add(img)
  61. f.write('<img src="%s">\n' % img)
  62. if (i+1) % 2 == 0:
  63. f.write('<br>')
  64. f.write('<br>')
  65. i = 0
  66. for img in data.get('Images', []):
  67. if img['Url'] in seen:
  68. continue
  69. f.write('<img src="%s">\n' % img['Url'])
  70. if (i+1) % 2 == 0:
  71. f.write('<br>')
  72. i += 1
  73. if check_bad_urls and main_photo and not _test_url(main_photo):
  74. print('Found bad photo URL. Refetching.')
  75. monitor.fetch_properties(conn, property_ids=[pid])
  76. monitor.process_properties(conn)
  77. return run_single(conn, pid)
  78. return size, ber, address, main_photo, bedrooms, bathrooms, price
  79. def run(conn, retry_bad_pids=True):
  80. rows = conn.select('''
  81. select a.id, a.update_ts, p.price
  82. from annotations a join properties p on a.id = p.id
  83. where a.favourited=1 order by a.update_ts desc
  84. ''')
  85. if not rows:
  86. raise Exception('No favourited properties.')
  87. bad_pids = []
  88. fname = 'htmls/index.html'
  89. with open(fname, 'w') as f:
  90. f.write('''
  91. <style>
  92. img {width: 500px}
  93. </style>
  94. ''')
  95. for pid, ts, price in rows:
  96. row = run_single(conn, pid)
  97. size, ber, address, main_photo, bedrooms, bathrooms, price = row
  98. f.write('<a href="%i.html"><h2>%s</h2></a>' % (pid, address))
  99. f.write('<h2>%sk, %s m<sup>2</sup>, %s br / %s b, %s</h2>\n' % (int(int(price)/1000), size, bedrooms, bathrooms, ber))
  100. if main_photo:
  101. f.write('<a href="%i.html"><img src="%s"></a>' % (pid, main_photo))
  102. if not _test_url(main_photo):
  103. bad_pids.append(pid)
  104. f.write('<hr>')
  105. print('Found %i properties with bad photos' % len(bad_pids))
  106. if bad_pids and retry_bad_pids:
  107. monitor.fetch_properties(conn, property_ids=bad_pids)
  108. monitor.process_properties(conn)
  109. return run(conn, retry_bad_pids=False)
  110. return fname
  111. def _get_pid(conn, possible_pid):
  112. if possible_pid > 100000:
  113. return possible_pid
  114. rows = conn.select('select id from properties where tmp_id=?', possible_pid)
  115. if not rows:
  116. raise ValueError('Could not find a pid for tmp_id=%i' % possible_pid)
  117. return rows[0]
  118. if __name__ == '__main__':
  119. with db.connect() as conn:
  120. if len(sys.argv) > 1:
  121. pid = int(sys.argv[1])
  122. pid = _get_pid(conn, pid)
  123. fname = run_single(conn, pid, check_bad_urls=True)[0]
  124. webbrowser.open('htmls/%i.html' % pid)
  125. else:
  126. fname = run(conn)
  127. webbrowser.open('htmls/index.html')