Error that appears solve it : API issue. Please try again later.
Browse files- index.html +76 -40
index.html
CHANGED
|
@@ -150,27 +150,24 @@
|
|
| 150 |
document.getElementById('error').classList.add('hidden');
|
| 151 |
try {
|
| 152 |
// Fetch current weather
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
if (!
|
| 156 |
-
|
| 157 |
-
throw new Error('City not found. Try a different spelling or nearby location.');
|
| 158 |
-
} else if (currentWeatherResponse.status === 401) {
|
| 159 |
-
throw new Error('API issue. Please try again later.');
|
| 160 |
-
} else {
|
| 161 |
-
throw new Error(`Error: ${currentWeatherResponse.statusText}`);
|
| 162 |
-
}
|
| 163 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
// Fetch forecast
|
| 168 |
-
const forecastResponse = await fetch(`https://api.openweathermap.org/data/2.5/forecast?q=${location}&units=metric&appid=YOUR_API_KEY`);
|
| 169 |
-
if (!forecastResponse.ok) throw new Error('Forecast data unavailable');
|
| 170 |
-
const forecastData = await forecastResponse.json();
|
| 171 |
-
|
| 172 |
-
// Validate we got data for the city
|
| 173 |
-
if (currentWeatherData.cod !== 200 || forecastData.cod !== "200") {
|
| 174 |
throw new Error('Incomplete weather data received');
|
| 175 |
}
|
| 176 |
// Update UI with current weather
|
|
@@ -190,45 +187,84 @@
|
|
| 190 |
document.getElementById('error-message').textContent = error.message;
|
| 191 |
}
|
| 192 |
}
|
| 193 |
-
|
| 194 |
function updateCurrentWeather(data) {
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
document.getElementById('
|
| 199 |
-
document.getElementById('
|
| 200 |
-
document.getElementById('
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
-
// Update weather icon
|
| 205 |
const weatherIcon = document.getElementById('weather-icon');
|
| 206 |
-
weatherIcon.innerHTML = `<img src="
|
| 207 |
-
|
| 208 |
-
|
| 209 |
function updateForecast(data) {
|
| 210 |
const forecastContainer = document.getElementById('forecast');
|
| 211 |
forecastContainer.innerHTML = '';
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
-
|
| 217 |
-
const date = new Date(
|
| 218 |
const dayName = date.toLocaleDateString('en-US', { weekday: 'short' });
|
|
|
|
|
|
|
| 219 |
|
| 220 |
const forecastItem = document.createElement('div');
|
| 221 |
forecastItem.className = 'weather-card p-4 text-center forecast-item';
|
| 222 |
forecastItem.innerHTML = `
|
| 223 |
<p class="font-medium text-gray-800">${dayName}</p>
|
| 224 |
<p class="text-sm text-gray-600 mb-2">${date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}</p>
|
| 225 |
-
<img src="
|
| 226 |
-
<p class="text-gray-600 text-sm capitalize">${
|
| 227 |
<div class="flex justify-center gap-4 mt-2">
|
| 228 |
-
<span class="font-bold text-gray-800">${Math.round(
|
| 229 |
-
<span class="text-gray-600">${Math.round(
|
| 230 |
</div>
|
| 231 |
-
|
| 232 |
forecastContainer.appendChild(forecastItem);
|
| 233 |
});
|
| 234 |
}
|
|
|
|
| 150 |
document.getElementById('error').classList.add('hidden');
|
| 151 |
try {
|
| 152 |
// Fetch current weather
|
| 153 |
+
// First get coordinates for location
|
| 154 |
+
const geoResponse = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${location}&count=1`);
|
| 155 |
+
if (!geoResponse.ok) throw new Error('Location service unavailable');
|
| 156 |
+
const geoData = await geoResponse.json();
|
| 157 |
|
| 158 |
+
if (!geoData.results || geoData.results.length === 0) {
|
| 159 |
+
throw new Error('City not found. Try a different spelling or nearby location.');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
}
|
| 161 |
+
|
| 162 |
+
const { latitude, longitude } = geoData.results[0];
|
| 163 |
+
|
| 164 |
+
// Fetch current weather and forecast in one call
|
| 165 |
+
const weatherResponse = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t_weather=true&hourly=temperature_2m,relativehumidity_2m,windspeed_10m,weathercode&daily=weathercode,temperature_2m_max,temperature_2m_min&timezone=auto`);
|
| 166 |
+
if (!weatherResponse.ok) throw new Error('Weather service unavailable');
|
| 167 |
+
const weatherData = await weatherResponse.json();
|
| 168 |
|
| 169 |
+
// Validate we got data
|
| 170 |
+
if (!weatherData.current_weather || !weatherData.daily) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
throw new Error('Incomplete weather data received');
|
| 172 |
}
|
| 173 |
// Update UI with current weather
|
|
|
|
| 187 |
document.getElementById('error-message').textContent = error.message;
|
| 188 |
}
|
| 189 |
}
|
|
|
|
| 190 |
function updateCurrentWeather(data) {
|
| 191 |
+
const current = data.current_weather;
|
| 192 |
+
const date = new Date(current.time);
|
| 193 |
+
|
| 194 |
+
document.getElementById('location').textContent = document.getElementById('location-input').value;
|
| 195 |
+
document.getElementById('date').textContent = date.toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
|
| 196 |
+
document.getElementById('temp').textContent = `${Math.round(current.temperature)}°C`;
|
| 197 |
+
|
| 198 |
+
// Map weather codes to descriptions
|
| 199 |
+
const weatherCodes = {
|
| 200 |
+
0: 'Clear sky',
|
| 201 |
+
1: 'Mainly clear',
|
| 202 |
+
2: 'Partly cloudy',
|
| 203 |
+
3: 'Overcast',
|
| 204 |
+
45: 'Fog',
|
| 205 |
+
48: 'Depositing rime fog',
|
| 206 |
+
51: 'Light drizzle',
|
| 207 |
+
53: 'Moderate drizzle',
|
| 208 |
+
55: 'Dense drizzle',
|
| 209 |
+
56: 'Light freezing drizzle',
|
| 210 |
+
57: 'Dense freezing drizzle',
|
| 211 |
+
61: 'Slight rain',
|
| 212 |
+
63: 'Moderate rain',
|
| 213 |
+
65: 'Heavy rain',
|
| 214 |
+
66: 'Light freezing rain',
|
| 215 |
+
67: 'Heavy freezing rain',
|
| 216 |
+
71: 'Slight snow fall',
|
| 217 |
+
73: 'Moderate snow fall',
|
| 218 |
+
75: 'Heavy snow fall',
|
| 219 |
+
77: 'Snow grains',
|
| 220 |
+
80: 'Slight rain showers',
|
| 221 |
+
81: 'Moderate rain showers',
|
| 222 |
+
82: 'Violent rain showers',
|
| 223 |
+
85: 'Slight snow showers',
|
| 224 |
+
86: 'Heavy snow showers',
|
| 225 |
+
95: 'Thunderstorm',
|
| 226 |
+
96: 'Thunderstorm with slight hail',
|
| 227 |
+
99: 'Thunderstorm with heavy hail'
|
| 228 |
+
};
|
| 229 |
+
|
| 230 |
+
const weatherDesc = weatherCodes[current.weathercode] || 'Unknown';
|
| 231 |
+
document.getElementById('weather-desc').textContent = weatherDesc;
|
| 232 |
+
document.getElementById('wind-speed').textContent = `${current.windspeed} km/h`;
|
| 233 |
|
| 234 |
+
// Update weather icon with placeholder since Open-Meteo doesn't provide icons
|
| 235 |
const weatherIcon = document.getElementById('weather-icon');
|
| 236 |
+
weatherIcon.innerHTML = `<img src="http://static.photos/weather/200x200/${current.weathercode}" alt="${weatherDesc}">`;
|
| 237 |
+
}
|
|
|
|
| 238 |
function updateForecast(data) {
|
| 239 |
const forecastContainer = document.getElementById('forecast');
|
| 240 |
forecastContainer.innerHTML = '';
|
| 241 |
|
| 242 |
+
const weatherCodes = {
|
| 243 |
+
0: 'Clear', 1: 'Mainly clear', 2: 'Partly cloudy', 3: 'Overcast',
|
| 244 |
+
45: 'Fog', 48: 'Rime fog', 51: 'Drizzle', 53: 'Drizzle', 55: 'Drizzle',
|
| 245 |
+
61: 'Rain', 63: 'Rain', 65: 'Rain', 80: 'Showers', 81: 'Showers', 82: 'Showers',
|
| 246 |
+
71: 'Snow', 73: 'Snow', 75: 'Snow', 85: 'Snow', 86: 'Snow',
|
| 247 |
+
95: 'Thunderstorm', 96: 'Thunderstorm', 99: 'Thunderstorm'
|
| 248 |
+
};
|
| 249 |
|
| 250 |
+
for (let i = 0; i < 5; i++) {
|
| 251 |
+
const date = new Date(data.daily.time[i]);
|
| 252 |
const dayName = date.toLocaleDateString('en-US', { weekday: 'short' });
|
| 253 |
+
const weatherCode = data.daily.weathercode[i];
|
| 254 |
+
const weatherDesc = weatherCodes[weatherCode] || 'Unknown';
|
| 255 |
|
| 256 |
const forecastItem = document.createElement('div');
|
| 257 |
forecastItem.className = 'weather-card p-4 text-center forecast-item';
|
| 258 |
forecastItem.innerHTML = `
|
| 259 |
<p class="font-medium text-gray-800">${dayName}</p>
|
| 260 |
<p class="text-sm text-gray-600 mb-2">${date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}</p>
|
| 261 |
+
<img src="http://static.photos/weather/200x200/${weatherCode}" alt="${weatherDesc}" class="mx-auto w-16 h-16">
|
| 262 |
+
<p class="text-gray-600 text-sm capitalize">${weatherDesc}</p>
|
| 263 |
<div class="flex justify-center gap-4 mt-2">
|
| 264 |
+
<span class="font-bold text-gray-800">${Math.round(data.daily.temperature_2m_max[i])}°</span>
|
| 265 |
+
<span class="text-gray-600">${Math.round(data.daily.temperature_2m_min[i])}°</span>
|
| 266 |
</div>
|
| 267 |
+
`;
|
| 268 |
forecastContainer.appendChild(forecastItem);
|
| 269 |
});
|
| 270 |
}
|