53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
from flask import Flask, render_template, jsonify, request
|
|
from utils.weather import get_today_snowfall_async
|
|
from utils.geocode import get_city_name_async
|
|
from config import MAX_SNOW_INCHES
|
|
from flask_caching import Cache
|
|
import asyncio
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Cache config: 10-minute default timeout
|
|
cache = Cache(app, config={"CACHE_TYPE": "SimpleCache", "CACHE_DEFAULT_TIMEOUT": 600})
|
|
|
|
@cache.memoize()
|
|
def get_snowfall_for_location(lat, lon):
|
|
# Run async functions in an event loop
|
|
return asyncio.run(fetch_snow_and_city(lat, lon))
|
|
|
|
async def fetch_snow_and_city(lat, lon):
|
|
inches, snowing, tz_name = await get_today_snowfall_async(lat, lon)
|
|
city_name = await get_city_name_async(lat, lon)
|
|
return inches, snowing, tz_name, city_name
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return render_template("index.html")
|
|
|
|
@app.route("/api/snowfall")
|
|
def snowfall_api():
|
|
lat = request.args.get("lat", type=float)
|
|
lon = request.args.get("lon", type=float)
|
|
if lat is None or lon is None:
|
|
return jsonify({"error": "Missing lat/lon"}), 400
|
|
|
|
inches, snowing, tz_name, city_name = get_snowfall_for_location(lat, lon)
|
|
|
|
from datetime import datetime
|
|
import pytz
|
|
today = datetime.now(pytz.timezone(tz_name)).strftime("%B %d, %Y")
|
|
|
|
return jsonify({
|
|
"location": city_name,
|
|
"lat": lat,
|
|
"lon": lon,
|
|
"inches": inches,
|
|
"max_inches": MAX_SNOW_INCHES,
|
|
"snowing": snowing,
|
|
"today": today,
|
|
"updated": datetime.now().isoformat()
|
|
})
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=5000)
|