-
Notifications
You must be signed in to change notification settings - Fork 30
/
rainviewer-forecast-api-example.html
190 lines (162 loc) · 10.7 KB
/
rainviewer-forecast-api-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html>
<head>
<title>Rain Viewer Forecast API Example</title>
<meta charset="utf-8" />
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<style type="text/css">
body,
ul {
margin: 0;
}
li {
list-style: none;
display: block;
margin: 0;
padding: 12px 20px;
}
.with-api-key {
display: none;
}
</style>
</head>
<body>
<ul>
<li>
API Key (Mandatory): <input type="text" id="apikey" value="" /><input type="button"
onclick="applyApiKey(document.getElementById('apikey').value); return;" value="Apply" />
</li>
<li>
Latitude: <input type="text" id="latitude" value="45.46" />
Longitude: <input type="text" id="longitude" value="9.18" />
<input type="button"
onclick="displayForecast([document.getElementById('latitude').value, document.getElementById('longitude').value]); return;"
value="Display Forecast" />
</li>
</ul>
<div id="forecast"></div>
<script>
var apiKey = localStorage.getItem('apiKey');
if (apiKey) {
document.getElementById('apikey').value = apiKey;
}
function applyApiKey(key) {
apiKey = key;
localStorage.setItem('apiKey', apiKey);
}
function displayForecast(latitude, longitude) {
if (apiKey == null) {
alert('Please provide an API key');
return;
}
var url = 'https://api.rainviewer.com/private/forecast/' + latitude + ',' + longitude + '?full=1';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.setRequestHeader('x-api-key', apiKey);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var data = JSON.parse(xhr.responseText);
displayForecastData(data.data);
} else if (xhr.status == 401) {
alert('Unauthorized: Please check your API key.');
} else if (xhr.status == 403) {
alert('Forbidden: You do not have access to this resource.');
} else if (xhr.status == 404) {
alert('Not Found: The requested resource could not be found.');
} else {
alert('Error: ' + xhr.status + ' - ' + xhr.statusText);
}
}
};
xhr.onerror = function () {
alert('Request failed. Please check your network connection.');
};
xhr.send();
}
function displayForecastData(data) {
var forecast = document.getElementById('forecast');
forecast.innerHTML = '';
console.log(data);
var location = data.location ? `Location: ${data.location.latitude}, ${data.location.longitude}` : '';
var timezone = data.timezone ? `Timezone: ${data.timezone}` : '';
var sunrise = data.sunrise ? `Sunrise: ${new Date(data.sunrise * 1000).toLocaleTimeString()}` : '';
var sunset = data.sunset ? `Sunset: ${new Date(data.sunset * 1000).toLocaleTimeString()}` : '';
var coverage = data.radars && data.radars.coverage ? `Radar Coverage: ${data.radars.coverage}%` : '';
forecast.innerHTML += `<div>${location}</div>`;
forecast.innerHTML += `<div>${timezone}</div>`;
forecast.innerHTML += `<div>${sunrise}</div>`;
forecast.innerHTML += `<div>${sunset}</div>`;
forecast.innerHTML += `<div>${coverage}</div>`;
if (data.currently) {
var currently = data.currently;
forecast.innerHTML += `<h3>Currently</h3>`;
forecast.innerHTML += `<div>Time: ${new Date(currently.time * 1000).toLocaleTimeString()}</div>`;
forecast.innerHTML += `<div>Icon: ${currently.icon}</div>`;
forecast.innerHTML += `<div>Temperature: ${currently.temperature}°C</div>`;
forecast.innerHTML += `<div>Precipitation Probability: ${currently.precipitation.probability}%</div>`;
forecast.innerHTML += `<div>Precipitation Rate: ${currently.precipitation.rate} mm/h</div>`;
forecast.innerHTML += `<div>Humidity: ${currently.humidity}%</div>`;
forecast.innerHTML += `<div>Pressure: ${currently.pressure} hPa</div>`;
forecast.innerHTML += `<div>Wind: ${currently.wind.speed} km/h, Direction: ${currently.wind.direction}°</div>`;
forecast.innerHTML += `<div>Feels Like: ${currently.temp.feels_like}°C</div>`;
forecast.innerHTML += `<div>Dew Point: ${currently.temp.dew_point}°C</div>`;
forecast.innerHTML += `<div>Sky Coverage: ${currently.sky.coverage}%</div>`;
forecast.innerHTML += `<div>Visibility: ${currently.sky.visibility} km</div>`;
forecast.innerHTML += `<div>UV Index: ${currently.sun.uvi}</div>`;
forecast.innerHTML += `<div>Solar Radiation: ${currently.sun.radiation} W/m²</div>`;
}
if (data.aqi) {
var aqi = data.aqi;
forecast.innerHTML += `<h3>Air Quality Index</h3>`;
forecast.innerHTML += `<div>Time: ${new Date(aqi.time * 1000).toLocaleTimeString()}</div>`;
forecast.innerHTML += `<div>AQI: ${aqi.aqi}</div>`;
forecast.innerHTML += `<div>Category: ${aqi.category}</div>`;
forecast.innerHTML += `<div>Dominant Pollutant: ${aqi.dominant}</div>`;
forecast.innerHTML += `<div>Pollutants:</div>`;
aqi.pollutants.forEach(pollutant => {
forecast.innerHTML += `<div>Type: ${pollutant.type}, Name: ${pollutant.name}, Value (PPB): ${pollutant.valuePPB}, Value (µg/m³): ${pollutant.valueUGM3}, AQI: ${pollutant.aqi}, Category: ${pollutant.category}</div>`;
});
}
if (data.alerts && data.alerts.length > 0) {
forecast.innerHTML += `<h3>Alerts</h3>`;
data.alerts.forEach(alert => {
forecast.innerHTML += `<div>Severity: ${alert.severity}, Category: ${alert.category}, Type: ${alert.type}, Starts: ${new Date(alert.starts * 1000).toLocaleTimeString()}, Ends: ${new Date(alert.ends * 1000).toLocaleTimeString()}, ID: ${alert.id}</div>`;
});
}
if (data.nowcast) {
var nowcast = data.nowcast;
forecast.innerHTML += `<h3>Nowcast</h3>`;
forecast.innerHTML += `<div>Icon: ${nowcast.icon}</div>`;
forecast.innerHTML += `<div>Precipitation Starts: ${new Date(nowcast.precipitation.starts * 1000).toLocaleTimeString()}</div>`;
forecast.innerHTML += `<div>Precipitation Ends: ${new Date(nowcast.precipitation.ends * 1000).toLocaleTimeString()}</div>`;
forecast.innerHTML += `<div>Max Rainrate: ${nowcast.max_rainrate} mm/h</div>`;
nowcast.data.forEach(item => {
forecast.innerHTML += `<div>Time: ${new Date(item.time * 1000).toLocaleTimeString()}, Precipitation: ${item.precipitation} mm/h</div>`;
});
}
if (data.hourly && data.hourly.data.length > 0) {
forecast.innerHTML += `<h3>Hourly Forecast</h3>`;
data.hourly.data.forEach(hour => {
forecast.innerHTML += `<h4>Time: ${new Date(hour.time * 1000).toLocaleTimeString()}</h4>`;
forecast.innerHTML += `<div>Icon: ${hour.icon}, Temperature: ${hour.temperature}°C, Precipitation Probability: ${hour.precipitation.probability}%, Precipitation Rate: ${hour.precipitation.rate} mm/h, Humidity: ${hour.humidity}%, Pressure: ${hour.pressure} hPa, Wind: ${hour.wind.speed} km/h, Direction: ${hour.wind.direction}°, Feels Like: ${hour.temp.feels_like}°C, Dew Point: ${hour.temp.dew_point}°C, Sky Coverage: ${hour.sky.coverage}%, Visibility: ${hour.sky.visibility} km, UV Index: ${hour.sun.uvi}, Solar Radiation: ${hour.sun.radiation} W/m²</div>`;
});
}
if (data.daily && data.daily.data.length > 0) {
forecast.innerHTML += `<h3>Daily Forecast</h3>`;
data.daily.data.forEach(day => {
forecast.innerHTML += `<h4>Date: ${new Date(day.time * 1000).toLocaleDateString()}</h4>`;
forecast.innerHTML += `<div><strong>Day</strong>: Icon: ${day.day.icon}, Temperature: ${day.day.temperature}°C, Precipitation Probability: ${day.day.precipitation.probability}%, Precipitation Rate: ${day.day.precipitation.rate} mm/h, Humidity: ${day.day.humidity}%, Pressure: ${day.day.pressure} hPa, Wind: ${day.day.wind.speed} km/h, Direction: ${day.day.wind.direction}°, Feels Like: ${day.day.temp.feels_like}°C, Dew Point: ${day.day.temp.dew_point}°C, Sky Coverage: ${day.day.sky.coverage}%, Visibility: ${day.day.sky.visibility} km, UV Index: ${day.day.sun.uvi}, Solar Radiation: ${day.day.sun.radiation} W/m²</div>`;
forecast.innerHTML += `<div><strong>Night</strong>: Icon: ${day.night.icon}, Temperature: ${day.night.temperature}°C, Precipitation Probability: ${day.night.precipitation.probability}%, Precipitation Rate: ${day.night.precipitation.rate} mm/h, Humidity: ${day.night.humidity}%, Pressure: ${day.night.pressure} hPa, Wind: ${day.night.wind.speed} km/h, Direction: ${day.night.wind.direction}°, Feels Like: ${day.night.temp.feels_like}°C, Dew Point: ${day.night.temp.dew_point}°C, Sky Coverage: ${day.night.sky.coverage}%, Visibility: ${day.night.sky.visibility} km, UV Index: ${day.night.sun.uvi}, Solar Radiation: ${day.night.sun.radiation} W/m²</div>`;
});
}
if (data.nearest && data.nearest.precipitation && data.nearest.precipitation.data.length > 0) {
forecast.innerHTML += `<h3>Nearest Precipitation</h3>`;
data.nearest.precipitation.data.forEach(nearest => {
forecast.innerHTML += `<div>Time: ${new Date(nearest.time * 1000).toLocaleTimeString()}, Precipitation: ${nearest.precipitation} mm/h, Location: (${nearest.location.latitude}, ${nearest.location.longitude ?? 0}), Distance: ${nearest.position.distance ?? 0} m, Direction: ${nearest.position.direction ?? 0}°, Movement Direction: ${nearest.movement.direction ?? 0}°, Movement Speed: ${nearest.movement.speed ?? 0} km/h</div>`;
});
}
}
</script>
</body>
</html>