HexaWetter v1.3.0: Add precipitation map feature, update README, and enhance UI elements
This commit is contained in:
@@ -98,6 +98,50 @@ async def fetch_all_warnings() -> list[dict[str, Any]]:
|
||||
return warnings
|
||||
|
||||
|
||||
def _warning_identity_key(warning: dict[str, Any]) -> tuple[Any, ...]:
|
||||
return (
|
||||
warning.get("event"),
|
||||
warning.get("headline"),
|
||||
warning.get("level"),
|
||||
warning.get("start"),
|
||||
warning.get("end"),
|
||||
warning.get("description"),
|
||||
warning.get("instruction"),
|
||||
)
|
||||
|
||||
|
||||
def _dedupe_warnings(warnings: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
merged: dict[tuple[Any, ...], dict[str, Any]] = {}
|
||||
for warning in warnings:
|
||||
key = _warning_identity_key(warning)
|
||||
region = warning.get("region_name")
|
||||
if key not in merged:
|
||||
merged[key] = {
|
||||
**warning,
|
||||
"_cell_ids": [warning["cell_id"]],
|
||||
"_regions": [region] if region else [],
|
||||
}
|
||||
continue
|
||||
entry = merged[key]
|
||||
if warning["cell_id"] not in entry["_cell_ids"]:
|
||||
entry["_cell_ids"].append(warning["cell_id"])
|
||||
if region and region not in entry["_regions"]:
|
||||
entry["_regions"].append(region)
|
||||
|
||||
result: list[dict[str, Any]] = []
|
||||
for entry in merged.values():
|
||||
regions = entry.pop("_regions", [])
|
||||
cell_ids = entry.pop("_cell_ids", [entry.get("cell_id")])
|
||||
if len(regions) > 1:
|
||||
entry["region_name"] = " · ".join(regions)
|
||||
elif regions:
|
||||
entry["region_name"] = regions[0]
|
||||
entry["cell_id"] = cell_ids[0] if len(cell_ids) == 1 else ",".join(cell_ids)
|
||||
result.append(entry)
|
||||
result.sort(key=lambda w: (-w["level"], w.get("start") or ""))
|
||||
return result
|
||||
|
||||
|
||||
async def fetch_warnings_for_location(lat: float, lon: float, local_only: bool = True) -> dict[str, Any]:
|
||||
warnings = await fetch_all_warnings()
|
||||
warncells = await resolve_warncells(lat, lon)
|
||||
@@ -111,15 +155,16 @@ async def fetch_warnings_for_location(lat: float, lon: float, local_only: bool =
|
||||
]
|
||||
|
||||
if local_only:
|
||||
result_warnings = local
|
||||
result_warnings = _dedupe_warnings(local)
|
||||
else:
|
||||
result_warnings = warnings[:100]
|
||||
result_warnings = _dedupe_warnings(warnings[:100])
|
||||
|
||||
return {
|
||||
"lat": lat,
|
||||
"lon": lon,
|
||||
"count_total": len(warnings),
|
||||
"count_local": len(local),
|
||||
"count_local": len(result_warnings),
|
||||
"count_matched_raw": len(local),
|
||||
"warnings": result_warnings,
|
||||
"warncells": warncells.get("areas", []),
|
||||
"matched_cell_ids": sorted(cell_ids),
|
||||
|
||||
Reference in New Issue
Block a user