rainview api integration

This commit is contained in:
2025-04-11 14:09:06 -04:00
parent ecd01bd49c
commit d6412625d5
3 changed files with 84 additions and 3 deletions

36
ui/src/lib/rainViewer.ts Normal file
View File

@@ -0,0 +1,36 @@
import { WeatherMaps } from '@lib/rainViewer.types.ts';
const weatherMapsUrl = 'https://api.rainviewer.com/public/weather-maps.json';
async function getWeatherMaps(): Promise<WeatherMaps | undefined> {
const response = await fetch(`${weatherMapsUrl}`, {
method: 'GET',
});
if (response?.status === 200) {
return response.json();
} else {
return undefined;
}
}
// const rainViewerUrl = 'https://tilecache.rainviewer.com/v2/radar/1744386000/256/{z}/{x}/{y}/2/1_1.png';
// const rainViewerUrl = 'https://tilecache.rainviewer.com/v2/radar/1744386000/256/10/290/391/2/1_1.png'
// https://api.rainviewer.com/public/weather-maps.json
export async function getWeatherMapUrl(): Promise<string | null> {
const weatherMaps = await getWeatherMaps();
if (weatherMaps != undefined) {
let url = weatherMaps.host;
let latest = "";
if (weatherMaps.radar.nowcast.length > 0) {
latest = weatherMaps.radar.nowcast[weatherMaps.radar.nowcast.length - 1].path;
} else if (weatherMaps.radar.past.length > 0) {
latest = weatherMaps.radar.past[weatherMaps.radar.past.length - 1].path;
} else {
return null;
}
url += latest + "/256/{z}/{x}/{y}/2/1_1.png";
return url;
} else {
return null;
}
}

View File

@@ -0,0 +1,21 @@
export interface WeatherMaps {
version: string;
generated: number;
host: string;
radar: RadarObject;
satellite: SatelliteObject;
}
export interface RadarObject {
past: FrameObject[];
nowcast: FrameObject[];
}
export interface SatelliteObject {
infrared: FrameObject[];
}
export interface FrameObject {
time: number;
path: string;
}