create_htmls.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. price = int(price) if price is not None else price
  27. size = int(size) if size is not None else size
  28. bedrooms = int(bedrooms) if bedrooms is not None else bedrooms
  29. bathrooms = int(bathrooms) if bathrooms is not None else bathrooms
  30. data = json.loads(raw_json)
  31. search_rows = conn.select('select time, price from search_results where property_id=? order by time asc', pid)
  32. main_photo = None
  33. fname = 'htmls/%i.html' % pid
  34. with open(fname, 'w') as f:
  35. f.write('''
  36. <style>
  37. img {width: 900px}
  38. td {padding-right: 20px}
  39. #top {
  40. position: fixed;
  41. top: 0;
  42. left: 0;
  43. z-index: 999;
  44. width: 100%;
  45. height: 23px;
  46. }
  47. </style>
  48. ''')
  49. f.write('<h2>%s</h2>\n' % address)
  50. # f.write('<div id="top">')
  51. f.write('<h2>')
  52. f.write('<span style="background-color: %s">%sk</span>, ' % (_color(ranking.r_price(price/1000)), int(price/1000)))
  53. f.write('<span style="background-color: %s">%s m<sup>2</sup></span>, ' % (_color(ranking.r_size(size)), size))
  54. f.write('<span style="background-color: %s">%s br</span> / ' % (_color(ranking.r_bedrooms(bedrooms)), bedrooms))
  55. f.write('<span style="background-color: %s">%s b</span>, ' % (_color(ranking.r_bathrooms(bathrooms)), bathrooms))
  56. f.write('<span style="background-color: %s">%s</span>' % (_color(ranking.r_ber(ber)), ber))
  57. f.write('</h2><br>\n')
  58. # f.write('</div>')
  59. f.write('<a href="https://www.myhome.ie/_/brochure/_/%i">myhome.ie</a>' % pid)
  60. f.write(', <a href="https://www.google.ie/maps/search/%s">maps.ie</a>' % address)
  61. if search_rows:
  62. f.write('<p><hr><p>')
  63. f.write('<table><tr><td><b>Time</b></td><td><b>Price</b></td></tr>')
  64. for ts, price in search_rows:
  65. f.write('<tr><td>%s</td><td>%s</td></tr>' % (ts, int(int(price)/1000) if price and price.isdigit() else price))
  66. f.write('</table>')
  67. f.write('<p><hr><p>')
  68. seen = set()
  69. if data.get('MainPhoto'):
  70. main_photo = data['MainPhoto']
  71. seen.add(main_photo)
  72. f.write('<img src="%s"><br>\n' % main_photo)
  73. for i, img in enumerate(data.get('FloorPlans', [])):
  74. seen.add(img)
  75. f.write('<img src="%s">\n' % img)
  76. if (i+1) % 2 == 0:
  77. f.write('<br>')
  78. f.write('<br>')
  79. i = 0
  80. for img in data.get('Images', []):
  81. if img['Url'] in seen:
  82. continue
  83. f.write('<img src="%s">\n' % img['Url'])
  84. if (i+1) % 2 == 0:
  85. f.write('<br>')
  86. i += 1
  87. if check_bad_urls and main_photo and not _test_url(main_photo):
  88. print('Found bad photo URL. Refetching.')
  89. monitor.fetch_properties(conn, property_ids=[pid])
  90. monitor.process_properties(conn)
  91. return run_single(conn, pid)
  92. return size, ber, address, main_photo, bedrooms, bathrooms, price
  93. def run(conn, retry_bad_pids=True):
  94. rows = conn.select('''
  95. select a.id, a.update_ts, p.price
  96. from annotations a join properties p on a.id = p.id
  97. where a.favourited=1 order by a.update_ts desc
  98. ''')
  99. if not rows:
  100. raise Exception('No favourited properties.')
  101. bad_pids = []
  102. fname = 'htmls/index.html'
  103. with open(fname, 'w') as f:
  104. f.write('''
  105. <style>
  106. img {width: 500px}
  107. </style>
  108. ''')
  109. for pid, ts, price in rows:
  110. row = run_single(conn, pid)
  111. size, ber, address, main_photo, bedrooms, bathrooms, price = row
  112. f.write('<a href="%i.html"><h2>%s</h2></a>' % (pid, address))
  113. f.write('<h2>%sk, %s m<sup>2</sup>, %s br / %s b, %s</h2>\n' % (int(int(price)/1000), size, bedrooms, bathrooms, ber))
  114. if main_photo:
  115. f.write('<a href="%i.html"><img src="%s"></a>' % (pid, main_photo))
  116. if not _test_url(main_photo):
  117. bad_pids.append(pid)
  118. f.write('<hr>')
  119. print('Found %i properties with bad photos' % len(bad_pids))
  120. if bad_pids and retry_bad_pids:
  121. monitor.fetch_properties(conn, property_ids=bad_pids)
  122. monitor.process_properties(conn)
  123. return run(conn, retry_bad_pids=False)
  124. return fname
  125. def _get_pid(conn, possible_pid):
  126. if possible_pid > 100000:
  127. return possible_pid
  128. rows = conn.select('select id from properties where tmp_id=?', possible_pid)
  129. if not rows:
  130. raise ValueError('Could not find a pid for tmp_id=%i' % possible_pid)
  131. return rows[0]
  132. if __name__ == '__main__':
  133. with db.connect() as conn:
  134. if len(sys.argv) > 1:
  135. for pid in sys.argv[1:]:
  136. pid = int(pid)
  137. pid = _get_pid(conn, pid)
  138. fname = run_single(conn, pid, check_bad_urls=True)[0]
  139. webbrowser.open('htmls/%i.html' % pid)
  140. else:
  141. fname = run(conn)
  142. webbrowser.open('htmls/index.html')