-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
94 lines (83 loc) · 4.45 KB
/
script.js
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
document.addEventListener('DOMContentLoaded', () => {
const dateElement = document.querySelector('.date');
const tempElement = document.querySelector('.temp');
const weatherContentElement = document.querySelector('.weather-content');
const weatherDescriptionElement = document.querySelector('.weather-description');
const tempFormatElement = document.querySelector('.temp-format');
const aqiValueElement = document.querySelector('.aqi-value');
const pollutantElements = {
co: document.querySelector('.co'),
no: document.querySelector('.no'),
no2: document.querySelector('.no2'),
o3: document.querySelector('.o3'),
so2: document.querySelector('.so2'),
pm2_5: document.querySelector('.pm2_5'),
pm10: document.querySelector('.pm10'),
nh3: document.querySelector('.nh3')
};
const apiKey = '98d48501649a41790e957711ea3b6d24';
const city = 'Ho Chi Minh City';
const weatherApiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;
const lat = 10.8231;
const lon = 106.6297;
const airPollutionApiUrl = `https://api.openweathermap.org/data/2.5/air_pollution?lat=${lat}&lon=${lon}&appid=${apiKey}`;
function updateDate() {
const now = new Date();
const options = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' };
const formattedDate = now.toLocaleDateString('en-US', options);
dateElement.textContent = formattedDate;
}
function updateWeather() {
fetch(weatherApiUrl)
.then(response => response.json())
.then(data => {
const temperature = data.main.temp;
const tempMax = data.main.temp_max;
const tempMin = data.main.temp_min;
const weatherCondition = data.weather[0].main;
const weatherDescription = data.weather[0].description;
tempElement.innerHTML = `${temperature.toFixed(1)}°C`;
tempFormatElement.innerHTML = `High/Low: ${tempMax.toFixed(1)}°C / ${tempMin.toFixed(1)}°C`;
weatherContentElement.innerHTML = `${weatherCondition} (${weatherDescription.charAt(0).toUpperCase() + weatherDescription.slice(1)})`;
})
.catch(error => {
console.error('Error fetching weather data:', error);
tempElement.innerHTML = 'Temp not available';
tempFormatElement.innerHTML = 'High/Low: N/A / N/A';
weatherContentElement.innerHTML = 'Weather not available';
});
}
function updateAirQuality() {
fetch(airPollutionApiUrl)
.then(response => response.json())
.then(data => {
const aqi = data.list[0].main.aqi;
const components = data.list[0].components;
aqiValueElement.innerHTML = `AQI Level: <span class="aqi-number">${aqi}</span>`;
const aqiNumberElement = document.querySelector('.aqi-number');
aqiNumberElement.classList.remove('value-1', 'value-2', 'value-3', 'value-4', 'value-5');
aqiNumberElement.classList.add(`value-${aqi}`);
pollutantElements.co.innerHTML = `${components.co.toFixed(1)} µg/m³`;
pollutantElements.no.innerHTML = `${components.no.toFixed(1)} µg/m³`;
pollutantElements.no2.innerHTML = `${components.no2.toFixed(1)} µg/m³`;
pollutantElements.o3.innerHTML = `${components.o3.toFixed(1)} µg/m³`;
pollutantElements.so2.innerHTML = `${components.so2.toFixed(1)} µg/m³`;
pollutantElements.pm2_5.innerHTML = `${components.pm2_5.toFixed(1)} µg/m³`;
pollutantElements.pm10.innerHTML = `${components.pm10.toFixed(1)} µg/m³`;
pollutantElements.nh3.innerHTML = `${components.nh3.toFixed(1)} µg/m³`;
})
.catch(error => {
console.error('Error fetching air quality data:', error);
aqiValueElement.innerHTML = 'AQI data not available';
Object.keys(pollutantElements).forEach(key => {
pollutantElements[key].innerHTML = 'N/A';
});
});
}
updateDate();
updateWeather();
updateAirQuality();
setInterval(updateDate, 60000);
setInterval(updateWeather, 60000);
setInterval(updateAirQuality, 60000);
});