from flask import Flask, render_template, request import random import datetime import json import os import time from app import app def _all_stops(): with open('lats_lons.txt') as f: towns = json.loads("".join(f.read())) start_date = datetime.datetime(2018, 5, 15) filtered_towns = [] last_town = "" for idx, town in enumerate(towns): town['id'] = town['city'].split(',')[0].replace(' ', '') town['day'] = idx time_diff = datetime.timedelta(idx) date_ = start_date + time_diff town['date_'] = date_.strftime("%b.%d.%Y") if town['city'] != last_town: filtered_towns.append(town) else: if 'visits' in filtered_towns[-1]: filtered_towns[-1]['visits'] += 1 else: filtered_towns[-1]['visits'] = 1 end_date = start_date + time_diff + datetime.timedelta(filtered_towns[-1]['visits'] - 1) filtered_towns[-1]['final_date'] = end_date.strftime("%b.%d.%Y") last_town = town['city'] if 'buck' in town and town['buck'] == "stops here": break return filtered_towns def _get_stops(): current_time = datetime.datetime.now() current_year = current_time.year start_day_anniversairy = datetime.datetime(current_year, 5, 15) historical_days_progressed = (current_time - start_day_anniversairy).days stops = _all_stops() if historical_days_progressed <= len(stops): limited_towns = [town for town in stops if town['day'] <= historical_days_progressed] return list(reversed(limited_towns)) else: return stops def _is_it_that_time_of_year(): (datetime.datetime.now() - datetime.datetime(datetime.datetime.now().year, 5, 15)).days <= 90 @app.route('/') def home(): return render_template('home.html') @app.route('/map') def map(): filtered_towns = _get_stops() try: return render_template('map.html', towns=filtered_towns) except Exception as e: print(e) @app.route('/random') def random_town(): towns = _all_stops() try: random_town = random.choice(towns) return render_template('random.html', town=random_town) except Exception as e: print(e) @app.route('/stops') def stops(): towns = _get_stops() is_historical_period = _is_it_that_time_of_year() try: return render_template('stops.html', args={"towns": towns, "is_historical_period": is_historical_period, "todays_date": time.strftime("%B %d")}) except Exception as e: print(e) @app.route('/faq') def faq(): return render_template('faq.html') @app.route('/glossary') def glossary(): return render_template('glossary.html') @app.route('/highlights') def highlights(): img_highlights = os.listdir('static/photos/highlights') img_highlights.sort() first_img = img_highlights[0] others = img_highlights[1:] return render_template('highlights.html', first_img=first_img, imgs=others) @app.route('/town/') def town(town_name): towns = _get_stops() town, = (t for t in towns if t['id'] == town_name) return render_template('town.html', town=town)