climate api implementation 1
This commit is contained in:
parent
1cdc7f1bc3
commit
1f17d76151
@ -91,30 +91,30 @@ CREATE TABLE IF NOT EXISTS `regions` (
|
||||
DELETE FROM `regions`;
|
||||
/*!40000 ALTER TABLE `regions` DISABLE KEYS */;
|
||||
INSERT INTO `regions` (`id`, `region`, `country_id`, `meteostat_id`, `lon`, `lat`, `created_at`, `updated_at`) VALUES
|
||||
(1, 'Buenos Aires', 1, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(2, 'Melbourne', 2, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(3, 'Sydney', 2, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(4, 'Bruessel', 3, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(5, 'Rio de Janei..', 4, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(6, 'Sao Paolo', 4, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(7, 'Toronto', 5, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(8, 'Santiago de ..', 6, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(9, 'Peking', 7, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(10, 'Shanghai', 7, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(11, 'Bogota', 8, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(12, 'Kairo', 9, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(13, 'London', 10, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(1, 'Buenos Aires', 1, '87585', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(2, 'Melbourne', 2, '94866', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(3, 'Sydney', 2, '94767', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(4, 'Bruessel', 3, '06458', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(5, 'Rio de Janeiro', 4, '83755', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(6, 'Sao Paolo', 4, '83780', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(7, 'Toronto', 5, '71624', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(8, 'Santiago de Chile', 6, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(9, 'Peking', 7, '54511', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(10, 'Shanghai', 7, '58362', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(11, 'Bogota', 8, '80222', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(12, 'Kairo', 9, '62366', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(13, 'London', 10, '03772', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(14, 'Paris', 11, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(15, 'Berlin', 12, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(16, 'Athen', 13, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(17, 'Hongkong', 14, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(18, 'Reykjavik', 15, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(18, 'Reykjavik', 15, '04030', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(19, 'Delhi', 16, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(20, 'Mumbai', 16, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(20, 'Mumbai', 16, '43003', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(21, 'Dublin', 17, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(22, 'Rom', 18, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(23, 'Tokio', 19, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(24, 'Kuala Lumpur', 20, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(24, 'Kuala Lumpur', 20, '48647', NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(25, 'Mexico City', 21, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(26, 'Marrakesch', 22, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(27, 'Amsterdam', 23, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
@ -136,7 +136,7 @@ INSERT INTO `regions` (`id`, `region`, `country_id`, `meteostat_id`, `lon`, `lat
|
||||
(43, 'Las Vegas', 36, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(44, 'Miami', 36, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(45, 'New York', 36, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(46, 'San Francis ..', 36, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(46, 'San Francisco', 36, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03'),
|
||||
(47, 'Dubai', 37, NULL, NULL, NULL, '2020-06-09 22:11:03', '2020-06-09 22:11:03');
|
||||
/*!40000 ALTER TABLE `regions` ENABLE KEYS */;
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
METEOSTAT_API_KEY=LMlDskju
|
||||
DB_HOST=192.168.43.52
|
||||
DB_HOST=localhost
|
||||
DB_USER=root
|
||||
DB_PASSWORD=devtest
|
||||
DB_PORT=3307
|
||||
DB_PORT=3306
|
||||
@ -0,0 +1,64 @@
|
||||
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 => {
|
||||
console.log(res)
|
||||
return axios.get(`https://api.meteostat.net/v1/climate/normals?station=${res.meteostat_id}&key=${process.env.METEOSTAT_API_KEY}`)
|
||||
})).then(responses => {
|
||||
responses.forEach(response => {
|
||||
//console.log(response.data)
|
||||
})
|
||||
//console.log(responses[0])
|
||||
})
|
||||
|
||||
connection.end();
|
||||
}
|
||||
|
||||
async function createClimateObject(src) {
|
||||
let response = await axios.get(`https://api.meteostat.net/v1/climate/normals?station=${res.meteostat_id}&key=${process.env.METEOSTAT_API_KEY}`)
|
||||
let temperatures = response.data.data.temperature
|
||||
let precipitations = response.data.data.precipitation
|
||||
let sunshines = response.data.data.sunshine
|
||||
|
||||
console.log(Object.entries(temperatures)[0])
|
||||
let results = []
|
||||
for (let index = 1; index <= 12; index++) {
|
||||
//const element = array[index];
|
||||
let result = {
|
||||
region: src.region,
|
||||
region_id: src.id,
|
||||
month: index,
|
||||
temperature: Object.values(temperatures)[index],
|
||||
precipitation: Object.values(precipitations)[index],
|
||||
sunshine: Object.values(sunshines)[index],
|
||||
}
|
||||
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,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
@ -1,13 +1,73 @@
|
||||
var mysql = require('mysql');
|
||||
require('dotenv').config()
|
||||
console.log(process.env);
|
||||
|
||||
var connection = mysql.createConnection({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
port: process.env.DB_PORT
|
||||
port: process.env.DB_PORT,
|
||||
database: 'travopti'
|
||||
});
|
||||
|
||||
connection.connect();
|
||||
|
||||
// var pool = mysql.createPool({
|
||||
// connectionLimit: 10,
|
||||
// host: process.env.DB_HOST,
|
||||
// user: process.env.DB_USER,
|
||||
// password: process.env.DB_PASSWORD,
|
||||
// port: process.env.DB_PORT,
|
||||
// database: 'travopti'
|
||||
// });
|
||||
|
||||
// let travoptidb = {}
|
||||
// travoptidb.all = () => {
|
||||
// return new Promise((resolve, reject) => {
|
||||
// pool.query(`SELECT * FROM regions`, (err, results) => {
|
||||
// if (err) {
|
||||
// return reject(err)
|
||||
// }
|
||||
// return resolve(results)
|
||||
// })
|
||||
// })
|
||||
// }
|
||||
|
||||
connection.connect((err) => {
|
||||
if (err) throw err;
|
||||
console.log('Database connected!')
|
||||
});
|
||||
|
||||
|
||||
exports.getRegions = () => {
|
||||
let sql = `SELECT * FROM regions`;
|
||||
console.log(connection.state)
|
||||
if (connection.state === 'disconnected') {
|
||||
setTimeout(() => console.log('waiting...'), 1000);
|
||||
}
|
||||
console.log('executed')
|
||||
let res = {}
|
||||
connection.query(sql, (error, results, fields) => {
|
||||
if (error) {
|
||||
return console.error(error.message);
|
||||
}
|
||||
console.log('innercallback(1)')
|
||||
res = results[0]
|
||||
});
|
||||
console.log('outsidecallback(2)')
|
||||
return res;
|
||||
}
|
||||
|
||||
exports.getBYTdata = () => {
|
||||
connection.query(`SELECT * FROM regions_byt`, (error, results, fields) => {
|
||||
if (error) {
|
||||
return console.error(error.message);
|
||||
}
|
||||
console.log(results[0])
|
||||
nres = results.map((obj) => {
|
||||
return obj.region
|
||||
})
|
||||
//console.log(nres);
|
||||
});
|
||||
}
|
||||
|
||||
exports.end = () => connection.end();
|
||||
|
||||
// module.exports = connection;
|
||||
|
||||
1129
backend/package-lock.json
generated
1129
backend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -9,8 +9,13 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^0.19.2",
|
||||
"dotenv": "^8.2.0",
|
||||
"express": "^4.17.1",
|
||||
"mysql": "^2.18.1"
|
||||
"mysql": "^2.18.1",
|
||||
"mysql2": "^2.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.4"
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user