Yfaite commited on
Commit
83e924a
·
verified ·
1 Parent(s): 6cb7dfc

Error that appears solve it : API issue. Please try again later.

Browse files
Files changed (1) hide show
  1. 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
- const currentWeatherResponse = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${location}&units=metric&appid=YOUR_API_KEY`);
 
 
 
154
 
155
- if (!currentWeatherResponse.ok) {
156
- if (currentWeatherResponse.status === 404) {
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
- const currentWeatherData = await currentWeatherResponse.json();
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
- document.getElementById('location').textContent = `${data.name}, ${data.sys.country}`;
196
- document.getElementById('date').textContent = new Date(data.dt * 1000).toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
197
- document.getElementById('temp').textContent = `${Math.round(data.main.temp)}°C`;
198
- document.getElementById('weather-desc').textContent = data.weather[0].description;
199
- document.getElementById('wind-speed').textContent = `${data.wind.speed} km/h`;
200
- document.getElementById('humidity').textContent = `${data.main.humidity}%`;
201
- document.getElementById('pressure').textContent = `${data.main.pressure} hPa`;
202
- document.getElementById('visibility').textContent = `${data.visibility / 1000} km`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
 
204
- // Update weather icon
205
  const weatherIcon = document.getElementById('weather-icon');
206
- weatherIcon.innerHTML = `<img src="https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png" alt="${data.weather[0].description}">`;
207
- }
208
-
209
  function updateForecast(data) {
210
  const forecastContainer = document.getElementById('forecast');
211
  forecastContainer.innerHTML = '';
212
 
213
- // Filter to get one forecast per day (every 24 hours)
214
- const dailyForecasts = data.list.filter((forecast, index) => index % 8 === 0);
 
 
 
 
 
215
 
216
- dailyForecasts.forEach(forecast => {
217
- const date = new Date(forecast.dt * 1000);
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="https://openweathermap.org/img/wn/${forecast.weather[0].icon}.png" alt="${forecast.weather[0].description}" class="mx-auto w-16 h-16">
226
- <p class="text-gray-600 text-sm capitalize">${forecast.weather[0].description}</p>
227
  <div class="flex justify-center gap-4 mt-2">
228
- <span class="font-bold text-gray-800">${Math.round(forecast.main.temp_max)}°</span>
229
- <span class="text-gray-600">${Math.round(forecast.main.temp_min)}°</span>
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}&current_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
  }