atci90d.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from flask import Flask, render_template, request
  2. import random
  3. import datetime
  4. import json
  5. import os
  6. import time
  7. from app import app
  8. def _all_stops():
  9. with open('lats_lons.txt') as f:
  10. towns = json.loads("".join(f.read()))
  11. start_date = datetime.datetime(2018, 5, 15)
  12. filtered_towns = []
  13. last_town = ""
  14. for idx, town in enumerate(towns):
  15. town['id'] = town['city'].split(',')[0].replace(' ', '')
  16. town['day'] = idx
  17. time_diff = datetime.timedelta(idx)
  18. date_ = start_date + time_diff
  19. town['date_'] = date_.strftime("%b.%d.%Y")
  20. if town['city'] != last_town:
  21. filtered_towns.append(town)
  22. else:
  23. if 'visits' in filtered_towns[-1]:
  24. filtered_towns[-1]['visits'] += 1
  25. else:
  26. filtered_towns[-1]['visits'] = 1
  27. end_date = start_date + time_diff + datetime.timedelta(filtered_towns[-1]['visits'] - 1)
  28. filtered_towns[-1]['final_date'] = end_date.strftime("%b.%d.%Y")
  29. last_town = town['city']
  30. if 'buck' in town and town['buck'] == "stops here":
  31. break
  32. return filtered_towns
  33. def _get_stops():
  34. current_time = datetime.datetime.now()
  35. current_year = current_time.year
  36. start_day_anniversairy = datetime.datetime(current_year, 5, 15)
  37. historical_days_progressed = (current_time - start_day_anniversairy).days
  38. stops = _all_stops()
  39. if historical_days_progressed <= len(stops):
  40. limited_towns = [town for town in stops if town['day'] <= historical_days_progressed]
  41. return list(reversed(limited_towns))
  42. else:
  43. return stops
  44. def _is_it_that_time_of_year():
  45. (datetime.datetime.now() - datetime.datetime(datetime.datetime.now().year, 5, 15)).days <= 90
  46. @app.route('/')
  47. def home():
  48. return render_template('home.html')
  49. @app.route('/map')
  50. def map():
  51. filtered_towns = _get_stops()
  52. try:
  53. return render_template('map.html', towns=filtered_towns)
  54. except Exception as e:
  55. print(e)
  56. @app.route('/random')
  57. def random_town():
  58. towns = _all_stops()
  59. try:
  60. random_town = random.choice(towns)
  61. return render_template('random.html', town=random_town)
  62. except Exception as e:
  63. print(e)
  64. @app.route('/stops')
  65. def stops():
  66. towns = _get_stops()
  67. is_historical_period = _is_it_that_time_of_year()
  68. try:
  69. return render_template('stops.html', args={"towns": towns, "is_historical_period": is_historical_period, "todays_date": time.strftime("%B %d")})
  70. except Exception as e:
  71. print(e)
  72. @app.route('/faq')
  73. def faq():
  74. return render_template('faq.html')
  75. @app.route('/glossary')
  76. def glossary():
  77. return render_template('glossary.html')
  78. @app.route('/highlights')
  79. def highlights():
  80. img_highlights = os.listdir('static/photos/highlights')
  81. img_highlights.sort()
  82. first_img = img_highlights[0]
  83. others = img_highlights[1:]
  84. return render_template('highlights.html', first_img=first_img, imgs=others)
  85. @app.route('/town/<town_name>')
  86. def town(town_name):
  87. towns = _get_stops()
  88. town, = (t for t in towns if t['id'] == town_name)
  89. return render_template('town.html', town=town)