-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
204 lines (190 loc) · 7.12 KB
/
script.js
File metadata and controls
204 lines (190 loc) · 7.12 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// script.js - API key embedded for demo (Option 1)
const API_KEY = "bd5e378503939ddaee76f12ad7a97608"; // <-- embedded key (public demo)
const ICON_URL = "https://openweathermap.org/img/wn/"; // e.g. ICON_URL + "10d@2x.png"
const $ = id => document.getElementById(id);
// elements
const cityInput = $("cityInput");
const searchBtn = $("searchBtn");
const geoBtn = $("geoBtn");
const loadingEl = $("loading");
const errorEl = $("error");
const currentEl = $("current");
const locationName = $("locationName");
const currentIcon = $("currentIcon");
const tempEl = $("temp");
const descEl = $("description");
const humidityEl = $("humidity");
const windEl = $("wind");
const forecastEl = $("forecast");
const forecastTitle = $("forecastTitle");
const themeToggle = $("themeToggle");
// events
searchBtn.addEventListener("click", () => fetchByCity(cityInput.value.trim()));
cityInput.addEventListener("keydown", (e) => { if (e.key === "Enter") fetchByCity(cityInput.value.trim()); });
geoBtn.addEventListener("click", useGeolocation);
themeToggle.addEventListener("change", () => document.body.classList.toggle("dark", themeToggle.checked));
// helpers
function showError(msg) {
errorEl.textContent = msg;
errorEl.classList.remove("hidden");
loadingEl.classList.add("hidden");
currentEl.classList.add("hidden");
forecastEl.classList.add("hidden");
forecastTitle.classList.add("hidden");
}
function clearError() {
errorEl.classList.add("hidden");
errorEl.textContent = "";
}
function showLoading() {
loadingEl.classList.remove("hidden");
clearError();
}
function hideLoading() {
loadingEl.classList.add("hidden");
}
if (!API_KEY) {
console.warn("No OpenWeatherMap API key found. Please add one.");
showError("No API key configured.");
}
// fetch current weather by city
async function fetchByCity(city) {
if (!city) { showError("Please enter a city name."); return; }
if (!API_KEY) { showError("API key not set."); return; }
showLoading();
try {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(city)}&appid=${API_KEY}&units=metric`;
const res = await fetch(url);
const data = await res.json();
if (!res.ok) { throw new Error(data.message || "City not found"); }
renderCurrent(data);
await fetchForecast(data.coord.lat, data.coord.lon);
// save last city
try { localStorage.setItem("lastCity", city); } catch(e){/*ignore*/ }
} catch (err) {
showError(err.message || "Failed to fetch weather");
} finally { hideLoading(); }
}
// fetch forecast (5-day / 3-hour forecast) and convert to daily
async function fetchForecast(lat, lon) {
try {
const url = `https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`;
const res = await fetch(url);
const data = await res.json();
if (!res.ok) { throw new Error(data.message || "Forecast error"); }
const daily = reduceForecastToDaily(data.list);
renderForecast(daily);
} catch (err) {
forecastEl.classList.add("hidden");
forecastTitle.classList.add("hidden");
console.warn("Forecast fetch failed", err);
}
}
// convert 3-hour chunks to one per day (choose item near 12:00 or middle)
function reduceForecastToDaily(list) {
const days = {};
list.forEach(item => {
const d = new Date(item.dt * 1000);
const dayKey = d.toISOString().slice(0,10);
if (!days[dayKey]) days[dayKey] = [];
days[dayKey].push(item);
});
const results = [];
Object.keys(days).slice(0,6).forEach(dayKey => {
const items = days[dayKey];
let pick = items.reduce((best, cur) => {
const target = 12;
const bestHour = new Date(best.dt*1000).getUTCHours();
const curHour = new Date(cur.dt*1000).getUTCHours();
return (Math.abs(curHour - target) < Math.abs(bestHour - target)) ? cur : best;
}, items[0]);
results.push(pick);
});
return results.slice(0,5);
}
function renderCurrent(data) {
clearError();
currentEl.classList.remove("hidden");
const icon = data.weather[0].icon;
currentIcon.src = `${ICON_URL}${icon}@2x.png`;
currentIcon.alt = data.weather[0].description || "weather icon";
locationName.textContent = `${data.name}, ${data.sys?.country || ""}`;
tempEl.innerHTML = `${Math.round(data.main.temp)}<sup>°C</sup>`;
descEl.textContent = data.weather[0].description || "";
humidityEl.textContent = `Humidity: ${data.main.humidity}%`;
windEl.textContent = `Wind: ${data.wind.speed} m/s`;
applyBackgroundByWeather(data.weather[0].id);
}
function renderForecast(dailyList) {
if (!dailyList || dailyList.length === 0) {
forecastEl.classList.add("hidden");
forecastTitle.classList.add("hidden");
return;
}
forecastEl.innerHTML = "";
dailyList.forEach(item => {
const d = new Date(item.dt * 1000);
const weekday = d.toLocaleDateString(undefined, { weekday: "short" });
const icon = item.weather[0].icon;
const min = Math.round(item.main.temp_min);
const max = Math.round(item.main.temp_max);
const el = document.createElement("div");
el.className = "day";
el.innerHTML = `
<div class="dayname">${weekday}</div>
<img src="${ICON_URL}${icon}@2x.png" alt="${item.weather[0].description}" />
<div class="temps">${max}° / ${min}°</div>
<div class="desc small">${item.weather[0].description}</div>
`;
forecastEl.appendChild(el);
});
forecastTitle.classList.remove("hidden");
forecastEl.classList.remove("hidden");
}
function applyBackgroundByWeather(id) {
const body = document.body;
if (id >= 200 && id < 600) {
body.style.background = "linear-gradient(180deg,#2b5876,#4e4376)";
} else if (id >= 600 && id < 700) {
body.style.background = "linear-gradient(180deg,#83a4d4,#b6fbff)";
} else if (id === 800) {
body.style.background = "linear-gradient(180deg,#56ccf2,#2f80ed)";
} else {
body.style.background = "linear-gradient(180deg,#cfd9df,#e2ebf0)";
}
}
// geolocation
function useGeolocation() {
if (!navigator.geolocation) {
showError("Geolocation not supported by your browser.");
return;
}
showLoading();
navigator.geolocation.getCurrentPosition(async pos => {
const lat = pos.coords.latitude;
const lon = pos.coords.longitude;
try {
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`;
const res = await fetch(url);
const data = await res.json();
if (!res.ok) throw new Error(data.message || "Reverse geocode error");
renderCurrent(data);
await fetchForecast(lat, lon);
} catch (err) {
showError(err.message || "Failed to get location weather.");
} finally {
hideLoading();
}
}, err => {
hideLoading();
showError("Unable to retrieve your location: " + (err.message || "permission denied"));
}, { timeout: 10000 });
}
// load last searched city
try {
const last = localStorage.getItem("lastCity");
if (last) fetchByCity(last);
else fetchByCity("Bengaluru");
} catch(e) {
fetchByCity("Bengaluru");
}