feat(api): add multiple days api

This commit is contained in:
2024-07-29 21:42:44 +03:00
parent 22fab7a15a
commit 7f0e19fb5a
16 changed files with 5411 additions and 64 deletions

View File

@@ -9,6 +9,7 @@ from fastapi.templating import Jinja2Templates
from gismeteo import datehelp
from gismeteo.location import LOCATION_BUNDLE
from gismeteo.mock import MOCK_DATA
from weather.api import WeatherApi
from weather.model import WeatherResponse
from .filters import cloudness_icon, wind_direction_icon
@@ -47,20 +48,26 @@ def mount(app: FastAPI):
@app.get("/weather/{location}", response_class=RedirectResponse)
async def get_weather_default(location: str):
return RedirectResponse(f"{location}/{datetime.date.today()}")
return RedirectResponse(f"{location}/day/{datetime.date.today()}")
@app.get("/weather/{location}/mock", response_class=HTMLResponse)
async def get_weather_mock(request: Request):
response = MOCK_DATA.response
@app.get("/weather/{location}/day/mock", response_class=HTMLResponse)
async def get_weather_day_mock(request: Request):
response = MOCK_DATA.get_response("day")
return build_weather_response(request, response)
# @app.get("/weather/{location}/{day}", response_class=HTMLResponse)
async def get_weather_day(request: Request, location: str, day: datehelp.Day):
date = datehelp.parse(day)
response = await request.app.state.weather_api.get_day(location, date)
@app.get("/weather/{location}/days/mock", response_class=HTMLResponse)
async def get_weather_days_mock(request: Request):
response = MOCK_DATA.get_response("days")
return build_weather_response(request, response)
@app.get("/weather/{location}/{date}", response_class=HTMLResponse)
async def get_weather(request: Request, location: str, date: datetime.date):
response = await request.app.state.weather_api.get_day(location, date)
@app.get("/weather/{location}/day/{date}", response_class=HTMLResponse)
async def get_weather_day(request: Request, location: str, date: datetime.date):
weather_api: WeatherApi = request.app.state.weather_api
response = await weather_api.get_day(location, date)
return build_weather_response(request, response)
@app.get("/weather/{location}/days/{days}", response_class=HTMLResponse)
async def get_weather_days(request: Request, location: str, days: int):
weather_api: WeatherApi = request.app.state.weather_api
response = await weather_api.get_days(location, days)
return build_weather_response(request, response)

View File

@@ -50,6 +50,11 @@ td {
background: rgba(0, 128, 255, 0.2);
}
.date .value a {
all: unset;
cursor: pointer;
}
.cloudness {
vertical-align: top;
}
@@ -62,11 +67,19 @@ td {
font-size: 2rem;
}
.temperature.positive .value {
.temperature {
padding: 0;
}
.temperature .value {
padding: 0.1rem 0.4rem;
}
.temperature .value.positive {
color: orangered;
}
.temperature.negative .value {
.temperature .value.negative {
color: blue;
}
@@ -82,7 +95,12 @@ td {
color: blue;
}
.pressure {
padding: 0;
}
.pressure .value {
padding: 0.1rem 0.4rem;
color: blueviolet;
}

View File

@@ -17,21 +17,36 @@
<body>
<h3>
{% if response.period == 'day' %}
<a class="button {{'disabled' if response.date == datetime.date.today() else ''}}"
href="{{response.date - datetime.timedelta(days=1)}}">⬅️</a>
<a class="button"
href="../days/10">⬆️</a>
<span>{{response.location}} | {{response.date.strftime('%a, %d %B %Y')}}</span>
<a class="button"
href="{{response.date + datetime.timedelta(days=1)}}">➡️</a>
{% endif %}
{% if response.period == 'days' %}
<span>{{response.location}} | {{response.date.strftime('%a, %d %B %Y')}}</span>
{% endif %}
</h3>
<table>
<tbody>
<!-- date -->
<tr>
{% for value in response.values %}
{% if response.period == 'day' %}
<td
class="date {{'now' if value.date < datetime.datetime.now() and value.date + datetime.timedelta(hours=3) > datetime.datetime.now() else ''}}">
<span class="value">{{value.date.strftime('%H:%M')}}</span>
</td>
{% endif %}
{% if response.period == 'days' %}
<td
class="date {{'now' if value.date.date() == datetime.date.today() else ''}}">
<span class="value"><a href="../day/{{value.date.date()}}">{{value.date.strftime('%a %d')}}</a></span>
</td>
{% endif %}
{% endfor %}
</tr>
<!-- cloudness -->
@@ -59,9 +74,13 @@
</tr>
<tr>
{% for value in response.values %}
<td class="temperature {{'positive' if value.temperature > 0 else 'negative'}}"
style="background-color: rgba(255, 128, 128, {{(value.temperature - 10) * 0.015}});">
<span class="value">{{value.temperature}}</span>
<td class="temperature">
{% for temperature in value.temperature %}
<div class="value {{'positive' if temperature > 0 else 'negative'}}"
style="background-color: rgba(255, 128, 128, {{(temperature - 10) * 0.015}});">
{{temperature}}
</div>
{% endfor %}
</td>
{% endfor %}
</tr>
@@ -123,9 +142,12 @@
</tr>
<tr>
{% for value in response.values %}
<td class="pressure"
style="background-color: rgba(128, 0, 255, {{(value.pressure - 720) * 0.008}});">
<span class="value">{{value.pressure}}</span>
<td class="pressure">
{% for pressure in value.pressure %}
<div class="value"
style="background-color: rgba(128, 0, 255, {{(pressure - 720) * 0.008}});">
{{pressure}}</div>
{% endfor %}
</td>
{% endfor %}
</tr>