flame/utils/getExternalWeather.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

const Config = require('../models/Config');
const Weather = require('../models/Weather');
const axios = require('axios');
const getExternalWeather = async () => {
2021-05-19 19:26:57 +03:00
// Get config from database
const config = await Config.findAll();
// Find and check values
const secret = config.find(pair => pair.key === 'WEATHER_API_KEY');
const lat = config.find(pair => pair.key === 'lat');
const long = config.find(pair => pair.key === 'long');
if (!secret) {
2021-05-19 19:26:57 +03:00
console.log('API key was not found. Weather updated failed');
return;
}
2021-05-19 19:26:57 +03:00
if (!lat || !long) {
console.log('Location was not found. Weather updated failed');
return;
}
// Fetch data from external API
try {
2021-05-19 19:26:57 +03:00
const res = await axios.get(`http://api.weatherapi.com/v1/current.json?key=${secret.value}&q=${lat.value},${long.value}`);
// For dev
// console.log(res.data);
// Save weather data
const cursor = res.data.current;
await Weather.create({
externalLastUpdate: cursor.last_updated,
tempC: cursor.temp_c,
tempF: cursor.temp_f,
isDay: cursor.is_day,
conditionText: cursor.condition.text,
conditionCode: cursor.condition.code
});
} catch (err) {
console.log(err);
console.log('External API request failed');
return;
}
}
module.exports = getExternalWeather;