bodegAI/services/weather_service.js
2024-11-05 21:28:29 -06:00

31 lines
929 B
JavaScript

const axios = require('axios');
const getWeather = async (latitude, longitude) => {
try {
const response = await axios.get('https://api.open-meteo.com/v1/forecast', {
params: {
latitude: latitude,
longitude: longitude,
current_weather: true,
timezone: 'auto'
}
});
if (!response.data.current_weather) {
throw new Error('Datos de clima no disponibles.');
}
const weather = response.data.current_weather;
return {
temperature: weather.temperature,
windspeed: weather.windspeed,
winddirection: weather.winddirection,
weathercode: weather.weathercode,
time: weather.time
};
} catch (error) {
throw new Error(`Error al obtener el clima: ${error.message}`);
}
};
module.exports = { getWeather };