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.
87 lines
2.9 KiB
87 lines
2.9 KiB
const log = console.log
|
|
const fs = require('fs')
|
|
const {table, getBorderCharacters} = require('table')
|
|
const argv = require('yargs').argv
|
|
|
|
const TABLE_CONFIG = {
|
|
// columns: {
|
|
// 0: { alignment: 'center' },
|
|
// 1: { alignment: 'center' },
|
|
// 2: { alignment: 'center' },
|
|
// },
|
|
border: getBorderCharacters(`ramac`)
|
|
}
|
|
const HIDE_PRECIP_LESS_THAN = 10
|
|
// const CREDIT_MSG = "Powered by Dark Sky: https://darksky.net/poweredby/"
|
|
// const CREDIT_MSG = "https://darksky.net/poweredby • https://smol.gq/wthr-src\n"
|
|
const CREDIT_MSG = "darksky.net/poweredby • smol.gq/wthr-src\n"
|
|
|
|
// 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)
|
|
}
|
|
})
|
|
}
|
|
|
|
const round5 = x => { return Math.ceil(x / 5) * 5 }
|
|
|
|
const logWeatherJson = (jsonFile, loc) => {
|
|
return new Promise((resolve, reject) => {
|
|
jsonReader(jsonFile, (err, weatherInfo) => {
|
|
if (err) {
|
|
reject(err)
|
|
}
|
|
|
|
log(loc)
|
|
|
|
if (argv.v) {
|
|
const infoList = [['H', '°C', '%P']]
|
|
for (let i = 0; i < 12; 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, TABLE_CONFIG));
|
|
} else {
|
|
// horizontal
|
|
const hoursList = ['H']
|
|
const tempsList = ['°C']
|
|
const precipList = ['%P']
|
|
for (let i = 0; i < 16; i += 2) {
|
|
const hourInfo = weatherInfo['hourly']['data'][i]
|
|
const date = new Date(hourInfo['time'] * 1000)
|
|
hoursList.push(date.getHours())
|
|
tempsList.push(Math.floor(hourInfo['apparentTemperature']))
|
|
// const precipProbability = Math.floor(hourInfo['precipProbability'] * 10) / 10
|
|
const precipProbability = round5(hourInfo['precipProbability'] * 100)
|
|
precipList.push((precipProbability < HIDE_PRECIP_LESS_THAN)
|
|
? '' : precipProbability)
|
|
}
|
|
const maxTempIndex = tempsList.indexOf(Math.max(...tempsList.slice(1)))
|
|
const minTempIndex = tempsList.indexOf(Math.min(...tempsList.slice(1)))
|
|
tempsList[maxTempIndex] = `${tempsList[maxTempIndex]}+`
|
|
tempsList[minTempIndex] = `${tempsList[minTempIndex]}-`
|
|
log(table([hoursList, tempsList, precipList], TABLE_CONFIG));
|
|
}
|
|
resolve()
|
|
})
|
|
})
|
|
}
|
|
|
|
const formatTimeUnit = unit => { return ((unit < 10) ? '0' : '') + unit }
|
|
log(CREDIT_MSG)
|
|
logTablePromises = [
|
|
logWeatherJson('./toronto.json', 'Toronto'),
|
|
logWeatherJson('./markham.json', 'Markham'),
|
|
]
|
|
Promise.all(logTablePromises).then(results => {})
|