Compare commits
12 Commits
26eca38f0a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 2112af7794 | |||
| b559d5660d | |||
| 3e05f6ff74 | |||
| 52400f888e | |||
| 97ca92b945 | |||
| 8047d07b86 | |||
| fffac198a8 | |||
| 4ecb238973 | |||
| 291338c697 | |||
| b4728e3dde | |||
| 22c6c01007 | |||
| 032656f945 |
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
utils/__pycache__/__init__.cpython-313.pyc
|
||||||
|
utils/__pycache__/geocode.cpython-313.pyc
|
||||||
|
utils/__pycache__/weather.cpython-313.pyc
|
||||||
|
__pycache__/config.cpython-313.pyc
|
||||||
|
__pycache__/config.cpython-312.pyc
|
||||||
|
utils/__pycache__/geocode.cpython-312.pyc
|
||||||
|
utils/__pycache__/weather.cpython-312.pyc
|
||||||
|
utils/__pycache__/__init__.cpython-312.pyc
|
||||||
13
Dockerfile
13
Dockerfile
@@ -1,12 +1,23 @@
|
|||||||
|
# Use Python 3.12 slim image
|
||||||
FROM python:3.12-slim
|
FROM python:3.12-slim
|
||||||
|
|
||||||
|
# Set working directory
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy and install dependencies first (cache-friendly)
|
||||||
COPY requirements.txt .
|
COPY requirements.txt .
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
# Copy the rest of the app
|
||||||
COPY app.py .
|
COPY app.py .
|
||||||
|
COPY config.py .
|
||||||
|
COPY utils/ ./utils
|
||||||
|
COPY templates/ ./templates
|
||||||
|
COPY static/ ./static
|
||||||
|
|
||||||
|
# Expose the Flask port
|
||||||
EXPOSE 5000
|
EXPOSE 5000
|
||||||
|
|
||||||
CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:app"]
|
# Run the app with Gunicorn, 4 workers, 2 threads each
|
||||||
|
CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:app", "-w", "4", "--threads", "2"]
|
||||||
|
|
||||||
|
|||||||
11
README.md
Normal file
11
README.md
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# Are We Buried?
|
||||||
|
|
||||||
|
A simple Flask web application that shows daily snowfall at your location and visually represents it with a snow depth animation.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Detects your current location using the browser’s geolocation API.
|
||||||
|
- Fetches snowfall data from Open-Meteo API.
|
||||||
|
- Shows total snowfall in inches for today.
|
||||||
|
- Displays the location name using OpenStreetMap reverse geocoding.
|
||||||
|
- Animates snow depth with a dynamic snow overlay.
|
||||||
202
app.py
202
app.py
@@ -1,192 +1,52 @@
|
|||||||
from flask import Flask, render_template_string, jsonify
|
from flask import Flask, render_template, jsonify, request
|
||||||
from datetime import date, datetime
|
from utils.weather import get_today_snowfall_async
|
||||||
import requests
|
from utils.geocode import get_city_name_async
|
||||||
|
from config import MAX_SNOW_INCHES
|
||||||
|
from flask_caching import Cache
|
||||||
|
import asyncio
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
# ---- CONFIG ----
|
# Cache config: 10-minute default timeout
|
||||||
LOCATION_NAME = "South Bend, IN (46617)"
|
cache = Cache(app, config={"CACHE_TYPE": "SimpleCache", "CACHE_DEFAULT_TIMEOUT": 600})
|
||||||
LAT = 41.6764
|
|
||||||
LON = -86.2520
|
|
||||||
|
|
||||||
MAX_SNOW_INCHES = 24
|
@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))
|
||||||
|
|
||||||
OPEN_METEO_URL = "https://api.open-meteo.com/v1/forecast"
|
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)
|
||||||
def get_today_snowfall():
|
return inches, snowing, tz_name, city_name
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
HTML = """
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Are We Buried?</title>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<style>
|
|
||||||
html, body {
|
|
||||||
margin: 0; padding: 0; height: 100%;
|
|
||||||
background: #0b1320; color: #fff;
|
|
||||||
font-family: system-ui, sans-serif;
|
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
.snowfall { position: fixed; inset: 0; pointer-events: none; z-index: 1; }
|
|
||||||
|
|
||||||
.flake {
|
|
||||||
position: absolute; top: -10px;
|
|
||||||
width: 6px; height: 6px;
|
|
||||||
background: rgba(255,255,255,0.9);
|
|
||||||
border-radius: 50%;
|
|
||||||
animation: fall linear infinite;
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes fall { to { transform: translateY(110vh); } }
|
|
||||||
|
|
||||||
.snow-wrapper {
|
|
||||||
position: fixed; bottom: 0; left: 0;
|
|
||||||
width: 100%; height: 0%;
|
|
||||||
transition: height 2s ease-out;
|
|
||||||
z-index: 2; overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
svg { position: absolute; top: -80px; width: 100%; height: 160px; }
|
|
||||||
|
|
||||||
.snow-body {
|
|
||||||
position: absolute; bottom: 0; width: 100%; height: 100%;
|
|
||||||
background:
|
|
||||||
radial-gradient(circle at 20% 20%, #fff 0 2px, transparent 3px),
|
|
||||||
radial-gradient(circle at 60% 40%, #f2f8ff 0 2px, transparent 3px),
|
|
||||||
radial-gradient(circle at 80% 30%, #fff 0 2px, transparent 3px),
|
|
||||||
linear-gradient(#fff, #e6f2ff);
|
|
||||||
background-size: 40px 40px, 60px 60px, 50px 50px, 100% 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.container {
|
|
||||||
position: relative; z-index: 3; height: 100%;
|
|
||||||
display: flex; flex-direction: column;
|
|
||||||
justify-content: center; align-items: center;
|
|
||||||
gap: 0.75rem; pointer-events: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
h1 { font-size: clamp(2.5rem, 6vw, 4rem); margin: 0; }
|
|
||||||
.amount { font-size: clamp(1.5rem, 4vw, 2.5rem); opacity: 0.9; }
|
|
||||||
.meta { opacity: 0.6; font-size: 0.9rem; }
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<div class="snowfall" id="snowfall"></div>
|
|
||||||
|
|
||||||
<div class="snow-wrapper" id="snowWrapper">
|
|
||||||
<svg viewBox="0 0 100 20" preserveAspectRatio="none">
|
|
||||||
<path d="M0 10 Q 15 2 30 10 T 60 10 T 100 10 L 100 20 L 0 20 Z" fill="#ffffff" />
|
|
||||||
</svg>
|
|
||||||
<div class="snow-body"></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="container">
|
|
||||||
<h1>Are We Buried?</h1>
|
|
||||||
<div class="amount" id="amount">-- in</div>
|
|
||||||
<div class="meta" id="meta">{{ location }} • {{ today }}</div>
|
|
||||||
<div class="meta" id="updated">Last updated: --</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
let flakesStarted = false;
|
|
||||||
|
|
||||||
async function loadSnow() {
|
|
||||||
const res = await fetch('/api/snowfall');
|
|
||||||
const data = await res.json();
|
|
||||||
|
|
||||||
document.getElementById('amount').textContent = data.inches + ' inches today';
|
|
||||||
|
|
||||||
const percent = Math.min(data.inches / data.max_inches, 1);
|
|
||||||
document.getElementById('snowWrapper').style.height = (percent * 100) + '%';
|
|
||||||
|
|
||||||
const now = new Date();
|
|
||||||
document.getElementById('updated').textContent =
|
|
||||||
'Last updated: ' + now.toLocaleTimeString();
|
|
||||||
|
|
||||||
if (data.snowing && !flakesStarted) {
|
|
||||||
spawnSnowflakes();
|
|
||||||
flakesStarted = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function spawnSnowflakes() {
|
|
||||||
const container = document.getElementById('snowfall');
|
|
||||||
const flakes = 90;
|
|
||||||
|
|
||||||
for (let i = 0; i < flakes; i++) {
|
|
||||||
const flake = document.createElement('div');
|
|
||||||
flake.className = 'flake';
|
|
||||||
flake.style.left = Math.random() * 100 + 'vw';
|
|
||||||
flake.style.animationDuration = 5 + Math.random() * 10 + 's';
|
|
||||||
flake.style.opacity = Math.random();
|
|
||||||
flake.style.transform = `scale(${Math.random() + 0.5})`;
|
|
||||||
container.appendChild(flake);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loadSnow();
|
|
||||||
setInterval(loadSnow, 10 * 60 * 1000);
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
"""
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
return render_template_string(
|
return render_template("index.html")
|
||||||
HTML,
|
|
||||||
today=date.today().strftime("%B %d, %Y"),
|
|
||||||
location=LOCATION_NAME
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/snowfall")
|
@app.route("/api/snowfall")
|
||||||
def snowfall_api():
|
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, 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({
|
return jsonify({
|
||||||
"location": LOCATION_NAME,
|
"location": city_name,
|
||||||
"lat": LAT,
|
"lat": lat,
|
||||||
"lon": LON,
|
"lon": lon,
|
||||||
"inches": inches,
|
"inches": inches,
|
||||||
"max_inches": MAX_SNOW_INCHES,
|
"max_inches": MAX_SNOW_INCHES,
|
||||||
"snowing": snowing,
|
"snowing": snowing,
|
||||||
|
"today": today,
|
||||||
"updated": datetime.now().isoformat()
|
"updated": datetime.now().isoformat()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=5000)
|
app.run(host="0.0.0.0", port=5000)
|
||||||
2
config.py
Normal file
2
config.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
MAX_SNOW_INCHES = 24
|
||||||
|
OPEN_METEO_URL = "https://api.open-meteo.com/v1/forecast"
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
flask
|
flask
|
||||||
requests
|
requests
|
||||||
gunicorn
|
gunicorn
|
||||||
|
pytz
|
||||||
|
timezonefinder
|
||||||
|
Flask-Caching
|
||||||
|
httpx
|
||||||
|
|||||||
38
static/main.css
Normal file
38
static/main.css
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
html, body {
|
||||||
|
margin:0; padding:0; height:100%;
|
||||||
|
background:#0b1320; color:#fff;
|
||||||
|
font-family:system-ui, sans-serif;
|
||||||
|
overflow:hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.snowfall { position: fixed; inset:0; pointer-events:none; z-index:1; }
|
||||||
|
.flake {
|
||||||
|
position:absolute; top:-10px; width:6px; height:6px;
|
||||||
|
background:rgba(255,255,255,0.9); border-radius:50%;
|
||||||
|
animation:fall linear infinite;
|
||||||
|
}
|
||||||
|
@keyframes fall { to { transform: translateY(110vh); } }
|
||||||
|
|
||||||
|
.snow-wrapper {
|
||||||
|
position:fixed; bottom:0; left:0; width:100%; height:0%;
|
||||||
|
transition:height 2s ease-out; z-index:2; overflow:hidden;
|
||||||
|
}
|
||||||
|
svg { position:absolute; top:-80px; width:100%; height:160px; }
|
||||||
|
.snow-body {
|
||||||
|
position:absolute; bottom:0; width:100%; height:100%;
|
||||||
|
background:
|
||||||
|
radial-gradient(circle at 20% 20%, #fff 0 2px, transparent 3px),
|
||||||
|
radial-gradient(circle at 60% 40%, #f2f8ff 0 2px, transparent 3px),
|
||||||
|
radial-gradient(circle at 80% 30%, #fff 0 2px, transparent 3px),
|
||||||
|
linear-gradient(#fff, #e6f2ff);
|
||||||
|
background-size:40px 40px,60px 60px,50px 50px,100% 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
position:relative; z-index:3; height:100%;
|
||||||
|
display:flex; flex-direction:column; justify-content:center;
|
||||||
|
align-items:center; gap:.75rem; pointer-events:none;
|
||||||
|
}
|
||||||
|
h1 { font-size:clamp(2.5rem,6vw,4rem); margin:0; }
|
||||||
|
.amount { font-size:clamp(1.5rem,4vw,2.5rem); opacity:0.9; }
|
||||||
|
.meta { opacity:0.6; font-size:0.9rem; }
|
||||||
44
static/main.js
Normal file
44
static/main.js
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
let flakesStarted = false;
|
||||||
|
|
||||||
|
async function loadSnow() {
|
||||||
|
if (!navigator.geolocation) return alert('Geolocation not supported');
|
||||||
|
|
||||||
|
navigator.geolocation.getCurrentPosition(async (pos) => {
|
||||||
|
const lat = pos.coords.latitude;
|
||||||
|
const lon = pos.coords.longitude;
|
||||||
|
|
||||||
|
const res = await fetch(`/api/snowfall?lat=${lat}&lon=${lon}`);
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
|
document.getElementById('amount').textContent = data.inches + ' inches today';
|
||||||
|
document.getElementById('meta').textContent = data.location + ' • ' + data.today;
|
||||||
|
|
||||||
|
const percent = Math.min(data.inches / data.max_inches, 1);
|
||||||
|
document.getElementById('snowWrapper').style.height = (percent * 100) + '%';
|
||||||
|
|
||||||
|
const now = new Date();
|
||||||
|
document.getElementById('updated').textContent =
|
||||||
|
'Last updated: ' + now.toLocaleTimeString();
|
||||||
|
|
||||||
|
if (data.snowing && !flakesStarted) {
|
||||||
|
spawnSnowflakes();
|
||||||
|
flakesStarted = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function spawnSnowflakes() {
|
||||||
|
const container = document.getElementById('snowfall');
|
||||||
|
for (let i = 0; i < 90; i++) {
|
||||||
|
const flake = document.createElement('div');
|
||||||
|
flake.className = 'flake';
|
||||||
|
flake.style.left = Math.random()*100+'vw';
|
||||||
|
flake.style.animationDuration = 5+Math.random()*10+'s';
|
||||||
|
flake.style.opacity = Math.random();
|
||||||
|
flake.style.transform = `scale(${Math.random()+0.5})`;
|
||||||
|
container.appendChild(flake);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadSnow();
|
||||||
|
setInterval(loadSnow, 10*60*1000);
|
||||||
25
templates/index.html
Normal file
25
templates/index.html
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Are We Buried?</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<link rel="stylesheet" href="{{ url_for('static', filename='main.css') }}">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="snowfall" id="snowfall"></div>
|
||||||
|
<div class="snow-wrapper" id="snowWrapper">
|
||||||
|
<svg viewBox="0 0 100 20" preserveAspectRatio="none">
|
||||||
|
<path d="M0 10 Q 15 2 30 10 T 60 10 T 100 10 L 100 20 L 0 20 Z" fill="#ffffff"/>
|
||||||
|
</svg>
|
||||||
|
<div class="snow-body"></div>
|
||||||
|
</div>
|
||||||
|
<div class="container">
|
||||||
|
<h1>Are We Buried?</h1>
|
||||||
|
<div class="amount" id="amount">-- in</div>
|
||||||
|
<div class="meta" id="meta">--</div>
|
||||||
|
<div class="meta" id="updated">Last updated: --</div>
|
||||||
|
</div>
|
||||||
|
<script src="{{ url_for('static', filename='main.js') }}"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
0
utils/__init__.py
Normal file
0
utils/__init__.py
Normal file
13
utils/geocode.py
Normal file
13
utils/geocode.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import httpx
|
||||||
|
|
||||||
|
async def get_city_name_async(lat, lon):
|
||||||
|
async with httpx.AsyncClient(timeout=5) as client:
|
||||||
|
r = await client.get(
|
||||||
|
"https://nominatim.openstreetmap.org/reverse",
|
||||||
|
params={"lat": lat, "lon": lon, "format": "json", "zoom": 10, "addressdetails": 1},
|
||||||
|
headers={"User-Agent": "AreWeBuriedApp/1.0"}
|
||||||
|
)
|
||||||
|
r.raise_for_status()
|
||||||
|
data = r.json()
|
||||||
|
address = data.get("address", {})
|
||||||
|
return address.get("city") or address.get("town") or address.get("village") or address.get("county") or f"{lat:.2f},{lon:.2f}"
|
||||||
33
utils/weather.py
Normal file
33
utils/weather.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import httpx
|
||||||
|
import pytz
|
||||||
|
from datetime import datetime
|
||||||
|
from timezonefinder import TimezoneFinder
|
||||||
|
from config import OPEN_METEO_URL
|
||||||
|
|
||||||
|
tf = TimezoneFinder()
|
||||||
|
|
||||||
|
async def get_today_snowfall_async(lat, lon):
|
||||||
|
tz_name = tf.timezone_at(lat=lat, lng=lon) or "UTC"
|
||||||
|
|
||||||
|
async with httpx.AsyncClient(timeout=5) as client:
|
||||||
|
params = {"latitude": lat, "longitude": lon, "daily": "snowfall_sum", "timezone": tz_name}
|
||||||
|
r = await client.get(OPEN_METEO_URL, params=params)
|
||||||
|
r.raise_for_status()
|
||||||
|
data = r.json()
|
||||||
|
|
||||||
|
daily = data.get("daily", {})
|
||||||
|
days = daily.get("time", [])
|
||||||
|
snow_values = daily.get("snowfall_sum", [])
|
||||||
|
|
||||||
|
today_str = datetime.now(pytz.timezone(tz_name)).date().isoformat()
|
||||||
|
|
||||||
|
snowfall_cm = 0.0
|
||||||
|
snowing_now = False
|
||||||
|
for d, s in zip(days, snow_values):
|
||||||
|
if d.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
|
||||||
Reference in New Issue
Block a user