blob: 602e5842bcc33a227da1d50542596e01d8ebc9e0 (
plain)
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
|
function loadWeather() {
let data = JSON.parse(window.localStorage.weather);
let temp = data.current.temp;
let humid = data.current.humidity;
let feels_like = data.current.feels_like;
let uvi = data.current.uvi;
let desc = data.current.weather[0].description;
document.getElementById("weatherTemp").innerHTML = `${temp}°`;
document.getElementById("weatherFeels").innerHTML = `${feels_like}°`;
document.getElementById("weatherHumid").innerHTML = `${humid}%`;
document.getElementById("weatherUVI").innerHTML = `${uvi}`;
let icon = `https://openweathermap.org/img/wn/${data.current.weather[0].icon}@2x.png`
let icon_el = document.getElementById("weatherIcon")
icon_el.src = icon;
for(var i = 1; i <= 3; i++){
document.getElementById(`day${i}icon`).src
document.getElementById(`day${i}high`).innerHTML = `${data.daily[i].temp.max}°`
document.getElementById(`day${i}low`).innerHTML = `${data.daily[i].temp.min}°`
document.getElementById(`day${i}pop`).innerHTML = `${data.daily[i].pop * 100}%`
document.getElementById(`day${i}desc`).innerHTML = data.daily[i].weather.reduce( (acc, el) => {
if(acc.length == 0) { return el.description }
else { return acc + " then " + el.description}
}, "")
}
}
let fetchWeather = true;
if(window.localStorage.weather_date){
let last_update = new Date(Date.parse(window.localStorage.weather_date))
let diff = new Date() - last_update;
// If one hour difference don't fetch
if(diff / (1000 * 60 * 60) < 1) {
fetchWeather = false;
}
}
if(fetchWeather){
fetch(`https://api.openweathermap.org/data/2.5/onecall?lat=${LAT}&lon=${LON}&APPID=${OPEN_WEATHER_API_KEY}&units=${WEATHER_UNITS}&exclude=hourly,minutely`)
.then(response => response.json())
.then(data => {
window.localStorage.weather_date = new Date();
window.localStorage.weather = JSON.stringify(data);
loadWeather()
});
} else {
loadWeather()
}
|