Fetch local weather from Dark Sky API.
https://wthr.ml/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.1 KiB
45 lines
1.1 KiB
const log = console.log
|
|
const fs = require('fs')
|
|
const {table} = require('table')
|
|
|
|
const HOURS_AHEAD = 12
|
|
|
|
// https://medium.com/@osiolabs/read-write-json-files-with-node-js-92d03cc82824
|
|
const jsonReader = (filePath, cb) => {
|
|
fs.readFile(filePath, (err, fileData) => {
|
|
if (err) {
|
|
return cb && cb(err)
|
|
}
|
|
try {
|
|
const object = JSON.parse(fileData)
|
|
return cb && cb(null, object)
|
|
} catch(err) {
|
|
return cb && cb(err)
|
|
}
|
|
})
|
|
}
|
|
|
|
jsonReader('./dark-sky.json', (err, weatherInfo) => {
|
|
if (err) {
|
|
console.log(err)
|
|
return
|
|
}
|
|
|
|
const tableConfig = {
|
|
columns: {
|
|
0: { alignment: 'center' },
|
|
1: { alignment: 'center' },
|
|
2: { alignment: 'center' },
|
|
}
|
|
}
|
|
const infoList = [['H', '°C', '%P']]
|
|
for (let i = 0; i < HOURS_AHEAD; i++) {
|
|
const hourInfo = weatherInfo['hourly']['data'][i]
|
|
const date = new Date(hourInfo['time'] * 1000)
|
|
infoList.push([date.getHours(),
|
|
Math.floor(hourInfo['apparentTemperature']),
|
|
Math.floor(hourInfo['precipProbability'])])
|
|
}
|
|
|
|
log(table(infoList, tableConfig));
|
|
})
|