require('dotenv').config() const mysql = require('mysql2/promise'); const axios = require('axios') const startDate = '2018-01' const endDate = '2018-12' async function main() { const connection = await mysql.createConnection({ host: process.env.DB_HOST, user: process.env.DB_USER, password: process.env.DB_PASSWORD, port: process.env.DB_PORT, database: 'travopti' }); const [result, fields] = await connection.execute(`SELECT * FROM regions WHERE meteostat_id IS NOT NULL`) // Promise.all(result.map(res => { // return createClimateObject(res) // })).then(results => { // let flat = results.reduce((total, element) => total.concat(element), []) // console.log(flat) // }) let temp = await Promise.all(result.map(x => createClimateObject(x))) let finalresults = temp.reduce((total, element) => total.concat(element), []) await writeToDatabase(connection, finalresults) connection.end(); } async function createClimateObject(src) { let response try { response = await axios.get(`https://api.meteostat.net/v1/climate/normals?station=${src.meteostat_id}&key=${process.env.METEOSTAT_API_KEY}`) } catch (error) { console.log("skipping: couldn't find results for following region: ") console.log(src.region + " with meteostat_id " + src.meteostat_id) return [] } if (!response.data.data) { console.log("skipping: no data for station meteostat_id " + src.meteostat_id + " (" + src.region + ")") return [] } let results = [] for (let index = 1; index <= 12; index++) { let result = { region: src.region, region_id: src.id, month: index, temperature: Object.values(response.data.data.temperature)[index - 1] ? Object.values(response.data.data.temperature)[index - 1] : null, temperature_min: Object.values(response.data.data.temperature_min)[index - 1] ? Object.values(response.data.data.temperature_min)[index - 1] : null, temperature_max: Object.values(response.data.data.temperature_max)[index - 1] ? Object.values(response.data.data.temperature_max)[index - 1] : null, precipitation: Object.values(response.data.data.precipitation)[index - 1] ? Object.values(response.data.data.precipitation)[index - 1] : null, sunshine: Object.values(response.data.data.sunshine)[index - 1] ? Object.values(response.data.data.sunshine)[index - 1] : null, } results.push(result) } return results } async function createClimateObjectFromLastYear(src) { let response = await axios.get(`https://api.meteostat.net/v1/history/monthly?station=${res.meteostat_id}&start=${startDate}&end=${endDate}&key=${process.env.METEOSTAT_API_KEY}`) let result = { region_id: src.id, } } async function writeToDatabase(dbConnection, src) { src.forEach(async (element) => { //console.log(element) await dbConnection.execute(` INSERT INTO region_climate (region_id, year, month, temperature_mean, temperature_mean_min, temperature_mean_max, percipitation, sunshine) VALUES (${element.region_id}, 0, ${element.month}, ${element.temperature}, ${element.temperature_min}, ${element.temperature_max}, ${element.precipitation}, ${element.sunshine});`) }); } main()