speed up
This commit is contained in:
@@ -1,43 +1,33 @@
|
||||
import requests
|
||||
from datetime import datetime
|
||||
import httpx
|
||||
import pytz
|
||||
from datetime import datetime
|
||||
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"
|
||||
async def get_today_snowfall_async(lat, lon):
|
||||
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)
|
||||
async with httpx.AsyncClient(timeout=5) as client:
|
||||
params = {"latitude": lat, "longitude": lon, "hourly": "snowfall", "timezone": tz_name}
|
||||
r = await client.get(OPEN_METEO_URL, params=params)
|
||||
r.raise_for_status()
|
||||
data = r.json()
|
||||
|
||||
hourly = data.get("hourly", {})
|
||||
times = hourly.get("time", [])
|
||||
snow_values = hourly.get("snowfall", [])
|
||||
hourly = data.get("hourly", {})
|
||||
times = hourly.get("time", [])
|
||||
snow_values = hourly.get("snowfall", [])
|
||||
|
||||
today_str = datetime.now(pytz.timezone(tz_name)).date().isoformat()
|
||||
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
|
||||
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"
|
||||
inches = round(snowfall_cm / 2.54, 1)
|
||||
return inches, snowing_now, tz_name
|
||||
|
||||
Reference in New Issue
Block a user