-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathweather.html
101 lines (95 loc) · 3.76 KB
/
weather.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
<!DOCTYPE html>
<html>
<head>
<title></title>
<link href='http://fonts.googleapis.com/css?family=Lato:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="./style.css" type="text/css" />
<script src="./script.js"></script>
<script>
var weatherCodes = [
{"code": 0, "description": "Clear sky"},
{"code": 1, "description": "Mainly clear"},
{"code": 2, "description": "Partly cloudy"},
{"code": 3, "description": "Overcast"},
{"code": [45, 48], "description": "Fog and depositing rime fog"},
{"code": [51, 53, 55], "description": "Drizzle"},
{"code": [56, 57], "description": "Freezing Drizzle"},
{"code": [61, 63, 65], "description": "Rain"},
{"code": [66, 67], "description": "Freezing Rain"},
{"code": [71, 73, 75], "description": "Snow fall"},
{"code": 77, "description": "Snow grains"},
{"code": [80, 81, 82], "description": "Rain showers"},
{"code": [85, 86], "description": "Snow showers slight"},
{"code": 95, "description": "Thunderstorm"},
{"code": [96, 99], "description": "Thunderstorm with slight hail"}
]
// Function to obtain the description of a weather code
function getWeatherDescription(code) {
for (var i = 0; i < weatherCodes.length; i++) {
var entry = weatherCodes[i];
if (Array.isArray(entry.code) && entry.code.includes(code)) {
return entry.description;
} else if (entry.code === code) {
return entry.description;
}
}
return "Unknown"; // return "Unknown" if the code does not correspond
}
function fetchRequest ()
{
var url = 'https://api.open-meteo.com/v1/forecast?latitude=41.9189&longitude=8.7381&daily=temperature_2m_max,temperature_2m_min,weather_code,wind_speed_10m_max';
const requestOptions = {
method: "GET",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
},
};
fetch(url, requestOptions)
.then((response) => {
if (!response.ok) {
throw new Error(`Request error: ${response.status} - ${response.statusText}`);
}
return response.json();
})
.then((data) => {
if (data && data.daily && data.daily.time) {
var times = data.daily.time;
var maxTemps = data.daily.temperature_2m_max;
var minTemps = data.daily.temperature_2m_min;
var weatherCodes = data.daily.weather_code;
var maxWindSpeeds = data.daily.wind_speed_10m_max;
var contentDiv = document.getElementById('content');
contentDiv.innerHTML = '';
for (var i = 0; i < times.length; ++i) {
var time = times[i];
var maxTemp = maxTemps[i];
var minTemp = minTemps[i];
var weatherCode = weatherCodes[i];
var maxWindSpeed = maxWindSpeeds[i];
contentDiv.innerHTML += '<div class="cell" id="header">' + time + '</div>';
contentDiv.innerHTML += '<div class="cell">Max Temp: ' + maxTemp + '°C</div>';
contentDiv.innerHTML += '<div class="cell">Min Temp: ' + minTemp + '°C</div>';
contentDiv.innerHTML += '<div class="cell">Weather Code: ' + getWeatherDescription(weatherCode) + '</div>';
contentDiv.innerHTML += '<div class="cell">Max Wind Speed: ' + maxWindSpeed + ' m/s</div>';
}
}
})
.catch((error) => {
gb.alert(error.message);
});
}
gb.onappear = fetchRequest;
</script>
</head>
<body>
<div class="cell" id="big_header">
AJACCIO WEATHER
</div>
<div id="content">
<div class="loader"></div>
</div>
<div class="header" style="height:20px;">
</div>
</body>
</html>