Add weather endpoint
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
76d355b047
commit
2cc1fcd918
|
@ -6,3 +6,5 @@ images:
|
|||
endpoint: s3.clortox.com
|
||||
bucket: backgrounds
|
||||
secure: True
|
||||
weather:
|
||||
period: 15
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
from datetime import datetime, date
|
||||
from fastapi import FastAPI, HTTPException
|
||||
|
||||
from routers import recipe, images
|
||||
from routers import recipe, images, weather
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(recipe.router)
|
||||
app.include_router(images.router)
|
||||
app.include_router(weather.router)
|
||||
|
|
|
@ -77,7 +77,6 @@ async def background():
|
|||
return file_response
|
||||
|
||||
|
||||
|
||||
except Exception as err:
|
||||
traceback.print_exc()
|
||||
raise HTTPException(status_code=500, detail=f"Unknown error. Check logs {err}")
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
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;
|
||||
|
||||
@router.get("/now")
|
||||
def get_weather_now_in_stow():
|
||||
"""
|
||||
Obtain the current weather in Stow, Ohio
|
||||
"""
|
||||
|
||||
return getWeather()
|
Loading…
Reference in New Issue