feat(easel): implement multiple weather providers

This commit is contained in:
2026-06-23 16:22:17 +03:00
parent e37ca6eeac
commit 9192ca3e4a
10 changed files with 321 additions and 19 deletions

View File

@@ -50,7 +50,13 @@ def build_weather_response(request: AppRequest, response: WeatherResponse):
router = APIRouter(prefix="/weather")
@router.get("/", response_class=HTMLResponse)
@router.get("/", response_class=RedirectResponse)
async def get_weather(request: AppRequest):
default_provider = request.app.state.api.get_api_providers(WeatherApi.TYPE)[0]
return RedirectResponse(default_provider)
@router.get("/{provider}", response_class=HTMLResponse)
async def get_weather_index(request: AppRequest, weather_api: WeatherApiDepends, query: str | None = None):
locations = (await weather_api.find_locations(query)) if query else []
return templates.TemplateResponse(
@@ -62,12 +68,12 @@ async def get_weather_index(request: AppRequest, weather_api: WeatherApiDepends,
)
@router.get("/{location}", response_class=RedirectResponse)
@router.get("/{provider}/{location}", response_class=RedirectResponse)
async def get_weather_default(location: str):
return RedirectResponse(f"{location}/tag/today")
@router.get("/{location}/day/{date}", response_class=HTMLResponse)
@router.get("/{provider}/{location}/day/{date}", response_class=HTMLResponse)
async def get_weather_day(
request: AppRequest,
weather_api: WeatherApiDepends,
@@ -78,13 +84,13 @@ async def get_weather_day(
return build_weather_response(request, response)
@router.get("/{location}/days/{days}", response_class=HTMLResponse)
@router.get("/{provider}/{location}/days/{days}", response_class=HTMLResponse)
async def get_weather_days(request: AppRequest, weather_api: WeatherApiDepends, location: str, days: int):
response = await weather_api.get_days(location, days)
return build_weather_response(request, response)
@router.get("/{location}/tag/{tag}", response_class=HTMLResponse)
@router.get("/{provider}/{location}/tag/{tag}", response_class=HTMLResponse)
async def get_weather_tag(request: AppRequest, weather_api: WeatherApiDepends, location: str, tag: str):
tag_value = TagUtil.parse_tag(tag)
if tag_value.type == TagType.DAY:

View File

@@ -36,7 +36,7 @@
<ul id="locations"
class="list-group mb-5">
{% for location in locations %}
<a href="{{location.id}}"
<a href="{{ url_for('get_weather_default', provider=request.path_params.provider, location=location.id) }}"
class="list-group-item list-group-item-action px-4"
onclick="saveLocation({id:'{{location.id}}', name:'{{location.name}}'});">
<span class="fi fi-{{location.country_code}} me-1"></span>
@@ -50,13 +50,20 @@
</ul>
<script>
(function () {
const getStorageKey = () => {
const provider = window.location.pathname.split('/').pop();
return `locations:${provider}`;
}
document.loadLocations = () => {
const locations = JSON.parse(window.localStorage.getItem('locations') || '{}');
const provider = window.location.pathname.split('/').pop();
console.log('!', provider);
const locations = JSON.parse(window.localStorage.getItem(getStorageKey()) || '{}');
const container = document.querySelector('#locations');
container.innerHTML = '';
for (const [id, name] of Object.entries(locations)) {
const element = document.createElement('a');
element.href = `${id}`;
element.href = `${window.location.pathname}/${id}`;
element.className = 'list-group-item list-group-item-action px-4 d-flex justify-content-between align-items-start';
element.innerHTML = `
<span class="text-primary me-auto">${name}</span>
@@ -67,15 +74,15 @@
}
document.saveLocation = (location) => {
const locations = JSON.parse(window.localStorage.getItem('locations') || '{}');
const locations = JSON.parse(window.localStorage.getItem(getStorageKey()) || '{}');
locations[location.id] = location.name;
window.localStorage.setItem('locations', JSON.stringify(locations));
window.localStorage.setItem(getStorageKey(), JSON.stringify(locations));
}
document.removeLocation = (id) => {
const locations = JSON.parse(window.localStorage.getItem('locations') || '{}');
const locations = JSON.parse(window.localStorage.getItem(getStorageKey()) || '{}');
delete locations[id];
window.localStorage.setItem('locations', JSON.stringify(locations));
window.localStorage.setItem(getStorageKey(), JSON.stringify(locations));
document.loadLocations();
}