-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
173 lines (154 loc) · 7.19 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
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
// const options = {
// method: "GET",
// headers: {
// "X-RapidAPI-Key": "1d53d369c2msh369610687b84d7ep18ba10jsnf572dd61be67",
// "X-RapidAPI-Host": "weather-by-api-ninjas.p.rapidapi.com"
// }
// };
// const getWeather = (city) =>{
// cityName.innerHTML=city
// fetch('https://weather-by-api-ninjas.p.rapidapi.com/v1/weather?city='+city, options)
// .then(response => {
// if (!response.ok) {
// throw new Error('HTTP error! status: ${response.status}');
// }
// return response.json();
// })
// .then(response => {
// console.log(response);
// // Populate your HTML elements with the data
// temp.innerHTML = response.temp
// min_temp.innerHTML = response.min_temp
// max_temp.innerHTML = response.max_temp
// cloud_pct.innerHTML = response.cloud_pct
// feels_like.innerHTML = response.feels_like
// humidity.innerHTML = response.humidity
// wind_speed.innerHTML = response.wind_speed
// wind_degrees.innerHTML = response.wind_degrees
// sunrise.innerHTML = response.sunrise
// sunset.innerHTML = response.sunset
// })
// .catch(err => {
// console.error(err);
// alert("An error occurred while fetching the weather data. Please try again later.");
// });
// }
// submit.addEventListener("click",(e)=>{
// e.preventDefault()
// getWeather(city.value)
// })
// getWeather("Delhi")
// -----------------------------------------------------------------------------------------------------------------------------------
// const apiKey = '641a05a07fccc72af7a85bd094cc42f2'; // Replace with your OpenWeatherMap API key
// async function getWeather(city) {
// const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;
// try {
// const response = await fetch(url);
// if (!response.ok) {
// throw new Error(`HTTP error! status: ${response.status}`);
// }
// const data = await response.json();
// updateWeatherUI(data);
// } catch (error) {
// console.error('Error:', error);
// alert("An error occurred while fetching the weather data. Please try again later.");
// }
// }
// function updateWeatherUI(data) {
// document.getElementById('cityName').textContent = data.name;
// document.getElementById('temp').textContent = data.main.temp;
// document.getElementById('min_temp').textContent = data.main.temp_min;
// document.getElementById('max_temp').textContent = data.main.temp_max;
// document.getElementById('cloud_pct').textContent = data.clouds.all;
// document.getElementById('feels_like').textContent = data.main.feels_like;
// document.getElementById('humidity').textContent = data.main.humidity;
// document.getElementById('wind_speed').textContent = data.wind.speed;
// document.getElementById('wind_degrees').textContent = data.wind.deg;
// document.getElementById('sunrise').textContent = new Date(data.sys.sunrise * 1000).toLocaleTimeString();
// document.getElementById('sunset').textContent = new Date(data.sys.sunset * 1000).toLocaleTimeString();
// }
// // Event listener for form submission
// document.getElementById('submit').addEventListener('click', (e) => {
// e.preventDefault(); // Prevent form submission
// const city = document.getElementById('city').value;
// getWeather(city);
// });
// // Event listener for dropdown selection
// document.querySelectorAll('.dropdown-item').forEach(item => {
// item.addEventListener('click', (e) => {
// const city = e.target.textContent;
// getWeather(city);
// });
// });
// // Initial call to populate default weather data
// getWeather("Delhi");
const apiKey = '641a05a07fccc72af7a85bd094cc42f2'; // Replace with your OpenWeatherMap API key
async function getWeather(city) {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error:', error);
return null;
}
}
function updateWeatherUI(data) {
if (!data) return;
document.getElementById('cityName').textContent = data.name;
document.getElementById('temp').textContent = `${data.main.temp}`;
document.getElementById('min_temp').textContent = `${data.main.temp_min}`;
document.getElementById('max_temp').textContent = `${data.main.temp_max}`;
document.getElementById('cloud_pct').textContent = `${data.clouds.all}`;
document.getElementById('feels_like').textContent = `${data.main.feels_like}`;
document.getElementById('humidity').textContent = `${data.main.humidity}`;
document.getElementById('wind_speed').textContent = `${data.wind.speed}`;
document.getElementById('wind_degrees').textContent = `${data.wind.deg}`;
document.getElementById('sunrise').textContent = new Date(data.sys.sunrise * 1000).toLocaleTimeString();
document.getElementById('sunset').textContent = new Date(data.sys.sunset * 1000).toLocaleTimeString();
}
async function updateWeatherTable() {
const cities = ['Chennai', 'Mumbai', 'Lucknow', 'Kolkata'];
for (const city of cities) {
const data = await getWeather(city);
if (data) {
const row = document.querySelector(`tr[data-city='${city}']`);
if (row) {
const cells = row.querySelectorAll('td');
cells[0].textContent = `${data.clouds.all}%`;
cells[1].textContent = `${data.main.temp}°C`;
cells[2].textContent = `${data.main.feels_like}°C`;
cells[3].textContent = `${data.main.humidity}%`;
cells[4].textContent = `${data.main.temp_min}°C`;
cells[5].textContent = `${data.main.temp_max}°C`;
cells[6].textContent = `${data.wind.speed} m/s`;
cells[7].textContent = `${data.wind.deg}°`;
cells[8].textContent = new Date(data.sys.sunrise * 1000).toLocaleTimeString();
cells[9].textContent = new Date(data.sys.sunset * 1000).toLocaleTimeString();
}
}
}
}
// Event listener for form submission
document.getElementById('submit').addEventListener('click', async (e) => {
e.preventDefault(); // Prevent form submission
const city = document.getElementById('city').value;
const data = await getWeather(city);
updateWeatherUI(data);
});
// Event listener for dropdown selection
document.querySelectorAll('.dropdown-item').forEach(item => {
item.addEventListener('click', async (e) => {
const city = e.target.textContent;
const data = await getWeather(city);
updateWeatherUI(data);
});
});
// Initial calls to populate weather data
getWeather("Delhi").then(updateWeatherUI);
updateWeatherTable();
// Update table every 10 minutes
setInterval(updateWeatherTable, 600000);