1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-19 19:49:37 +02:00
flame/utils/getExternalWeather.js

38 lines
1.2 KiB
JavaScript
Raw Normal View History

const Weather = require('../models/Weather');
const axios = require('axios');
const loadConfig = require('./loadConfig');
const getExternalWeather = async () => {
const { WEATHER_API_KEY: secret, lat, long } = await loadConfig();
// Fetch data from external API
try {
2024-09-10 22:27:29 +02:00
const res = await axios.get('https://api.open-meteo.com/v1/forecast', { params: {
latitude: lat,
longitude: long,
current: ['is_day', 'temperature_2m', 'relative_humidity_2m', 'cloud_cover', 'wind_speed_10m', 'weathercode'],
} });
// Save weather data
const cursor = res.data.current;
const weatherData = await Weather.create({
2024-09-10 22:27:29 +02:00
externalLastUpdate: "externalLastUpdate",
tempC: cursor.temperature_2m, // can not be 0 put like minus kelvin
tempF: cursor.temperature_2m,
isDay: Boolean(cursor.is_day),
cloud: cursor.cloud_cover,
conditionText: "conditionText",
conditionCode: 1,
humidity: cursor.relative_humidity_2m,
windK: cursor.wind_speed_10m,
windM: cursor.wind_speed_10m,
});
return weatherData;
} catch (err) {
2024-09-10 22:27:29 +02:00
throw new Error(err);
//throw new Error('External API request failed');
}
};
module.exports = getExternalWeather;