refactor from disgusting monolith

This commit is contained in:
2026-01-24 17:20:00 -05:00
parent 22c6c01007
commit b4728e3dde
12 changed files with 390 additions and 165 deletions

0
utils/__init__.py Normal file
View File

17
utils/geocode.py Normal file
View File

@@ -0,0 +1,17 @@
import requests
def get_city_name(lat, lon):
try:
r = requests.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", {})
city = address.get("city") or address.get("town") or address.get("village") or address.get("county") or f"{lat:.2f},{lon:.2f}"
return city
except Exception as e:
print("Reverse geocoding error:", e)
return f"{lat:.2f},{lon:.2f}"

43
utils/weather.py Normal file
View File

@@ -0,0 +1,43 @@
import requests
from datetime import datetime
import pytz
from timezonefinder import TimezoneFinder
from config import OPEN_METEO_URL
tf = TimezoneFinder()
def get_today_snowfall(lat, lon):
try:
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"