diff --git a/app.py b/app.py index 874c938..58ad6ff 100644 --- a/app.py +++ b/app.py @@ -1,133 +1,70 @@ -from flask import Flask, render_template_string, jsonify -from datetime import date, datetime +from flask import Flask, render_template_string, jsonify, request +from datetime import datetime, date import requests +import pytz +from timezonefinder import TimezoneFinder app = Flask(__name__) -# ---- CONFIG ---- -LOCATION_NAME = "South Bend, IN (46617)" -LAT = 41.6764 -LON = -86.2520 - MAX_SNOW_INCHES = 24 - OPEN_METEO_URL = "https://api.open-meteo.com/v1/forecast" - - -def get_today_snowfall(): - params = { - "latitude": LAT, - "longitude": LON, - "hourly": "snowfall", - "timezone": "America/Indiana/Indianapolis" - } - - try: - r = requests.get(OPEN_METEO_URL, params=params, timeout=5) - r.raise_for_status() - data = r.json() - - today = date.today().isoformat() - snowfall_cm = 0.0 - snowing_now = False - - for t, s in zip(data["hourly"]["time"], data["hourly"]["snowfall"]): - if t.startswith(today): - snowfall_cm += s - if s > 0: - snowing_now = True - - inches = round(snowfall_cm / 2.54, 1) - return inches, snowing_now - - except Exception: - return 0.0, False - +tf = TimezoneFinder() HTML = """ - - Are We Buried? - - + +Are We Buried? + + - -
- -
- - - -
-
- -
-

Are We Buried?

-
-- in
-
{{ location }} • {{ today }}
-
Last updated: --
-
- +
+
+ + + +
+
+
+

Are We Buried?

+
-- in
+
--
+
Last updated: --
+
""" +def get_today_snowfall(lat, lon): + try: + # Determine timezone for location + tz_name = tf.timezone_at(lat=lat, lng=lon) or "UTC" + + params = { + "latitude": lat, + "longitude": lon, + "hourly": "snowfall", + "timezone": tz_name + } + + r = requests.get(OPEN_METEO_URL, params=params, timeout=5) + r.raise_for_status() + data = r.json() + + hourly = data.get("hourly", {}) + times = hourly.get("time", []) + snow_values = hourly.get("snowfall", []) + + today_str = datetime.now(pytz.timezone(tz_name)).date().isoformat() + + snowfall_cm = 0.0 + snowing_now = False + for t, s in zip(times, snow_values): + if t.startswith(today_str): + snowfall_cm += s + if s > 0: + snowing_now = True + + inches = round(snowfall_cm / 2.54, 1) + return inches, snowing_now, tz_name + + except Exception as e: + print("Error fetching snowfall:", e) + return 0.0, False, "UTC" + @app.route("/") def index(): - return render_template_string( - HTML, - today=date.today().strftime("%B %d, %Y"), - location=LOCATION_NAME - ) - + return render_template_string(HTML) @app.route("/api/snowfall") def snowfall_api(): - inches, snowing = get_today_snowfall() + 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 = get_today_snowfall(lat, lon) + today = datetime.now(pytz.timezone(tz_name)).strftime("%B %d, %Y") return jsonify({ - "location": LOCATION_NAME, - "lat": LAT, - "lon": LON, + "location": f"{lat:.2f}, {lon:.2f}", + "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) \ No newline at end of file + app.run(host="0.0.0.0", port=5000)