General-API/src/routers/weather.py

46 lines
1.1 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, Header, BackgroundTasks
from fastapi.responses import FileResponse, Response
from typing import List, Optional
from pydantic import BaseModel
import logging
from config import getConfig
import os
from datetime import datetime, timedelta
import requests
router = APIRouter(
prefix="/weather",
tags=["Weather"],
responses={404: {"description": "Not found"}}
)
weather = None
last_checked_time = None
interval = timedelta(minutes=getConfig()["weather.period"])
def getWeather(location: str = "Stow") -> str:
global last_checked_time
global weather
if last_checked_time is None:
last_checked_time = datetime(1970, 1, 1)
if datetime.now() - last_checked_time >= timedelta(seconds=5):
print("Making request for weather")
weather = requests.get(f"https://wttr.in/{location}?format=1")
last_checked_time = datetime.now()
return weather.content.replace(b"\n",b"");
@router.get("/now")
def get_weather_now_in_stow():
"""
Obtain the current weather in Stow, Ohio
"""
return getWeather()