Browse Source

Add rain information

master
Kevin Mok 5 years ago
parent
commit
b79c359475
Signed by: Kevin-Mok GPG Key ID: AEA75288DC135CF5
  1. 38
      dark-sky.js

38
dark-sky.js

@ -11,7 +11,10 @@ const TABLE_CONFIG = {
// }, // },
border: getBorderCharacters(`ramac`) border: getBorderCharacters(`ramac`)
} }
const CREDIT_MSG = "Powered by Dark Sky: https://darksky.net/poweredby/"
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 // https://medium.com/@osiolabs/read-write-json-files-with-node-js-92d03cc82824
const jsonReader = (filePath, cb) => { const jsonReader = (filePath, cb) => {
@ -28,6 +31,8 @@ const jsonReader = (filePath, cb) => {
}) })
} }
const round5 = x => { return Math.ceil(x / 5) * 5 }
const logWeatherJson = (jsonFile, loc) => { const logWeatherJson = (jsonFile, loc) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
jsonReader(jsonFile, (err, weatherInfo) => { jsonReader(jsonFile, (err, weatherInfo) => {
@ -37,34 +42,44 @@ const logWeatherJson = (jsonFile, loc) => {
log(loc) log(loc)
weatherInfo['hourly']['data'][0] = weatherInfo['currently']
if (argv.v) { if (argv.v) {
const infoList = [['H', '°C', '%P']]
const infoList = [['H', '°C', 'R','%P']]
for (let i = 0; i < 12; i++) { for (let i = 0; i < 12; i++) {
const hourInfo = weatherInfo['hourly']['data'][i] const hourInfo = weatherInfo['hourly']['data'][i]
const date = new Date(hourInfo['time'] * 1000) const date = new Date(hourInfo['time'] * 1000)
infoList.push([date.getHours(),
const precipProbability = round5(hourInfo['precipProbability'] * 100)
const precipText = ((precipProbability < HIDE_PRECIP_LESS_THAN)
? '' : precipProbability)
infoList.push([
date.getHours(),
Math.floor(hourInfo['apparentTemperature']), Math.floor(hourInfo['apparentTemperature']),
Math.floor(hourInfo['precipProbability'])])
((hourInfo['summary'].includes('Rain')) ? 'Y' : ''),
precipText,
])
} }
log(table(infoList, TABLE_CONFIG)); log(table(infoList, TABLE_CONFIG));
} else { } else {
// horizontal // horizontal
const hoursList = ['H'] const hoursList = ['H']
const tempsList = ['°C'] const tempsList = ['°C']
const rainList = ['R']
const precipList = ['%P'] const precipList = ['%P']
for (let i = 0; i < 16; i += 2) { for (let i = 0; i < 16; i += 2) {
const hourInfo = weatherInfo['hourly']['data'][i] const hourInfo = weatherInfo['hourly']['data'][i]
const date = new Date(hourInfo['time'] * 1000) const date = new Date(hourInfo['time'] * 1000)
hoursList.push(date.getHours()) hoursList.push(date.getHours())
tempsList.push(Math.floor(hourInfo['apparentTemperature'])) tempsList.push(Math.floor(hourInfo['apparentTemperature']))
const precipProbability = Math.floor(hourInfo['precipProbability'])
precipList.push((precipProbability < 20) ? '' : precipProbability)
rainList.push((hourInfo['summary'].includes('Rain')) ? 'Y' : '')
const precipProbability = round5(hourInfo['precipProbability'] * 100)
precipList.push((precipProbability < HIDE_PRECIP_LESS_THAN)
? '' : precipProbability)
} }
const maxTempIndex = tempsList.indexOf(Math.max(...tempsList.slice(1))) const maxTempIndex = tempsList.indexOf(Math.max(...tempsList.slice(1)))
const minTempIndex = tempsList.indexOf(Math.min(...tempsList.slice(1))) const minTempIndex = tempsList.indexOf(Math.min(...tempsList.slice(1)))
tempsList[maxTempIndex] = `${tempsList[maxTempIndex]}+` tempsList[maxTempIndex] = `${tempsList[maxTempIndex]}+`
tempsList[minTempIndex] = `${tempsList[minTempIndex]}-` tempsList[minTempIndex] = `${tempsList[minTempIndex]}-`
log(table([hoursList, tempsList, precipList], TABLE_CONFIG));
log(table([hoursList, tempsList, rainList, precipList], TABLE_CONFIG));
} }
resolve() resolve()
}) })
@ -72,14 +87,9 @@ const logWeatherJson = (jsonFile, loc) => {
} }
const formatTimeUnit = unit => { return ((unit < 10) ? '0' : '') + unit } const formatTimeUnit = unit => { return ((unit < 10) ? '0' : '') + unit }
log(CREDIT_MSG)
logTablePromises = [ logTablePromises = [
logWeatherJson('./toronto.json', 'Toronto'), logWeatherJson('./toronto.json', 'Toronto'),
logWeatherJson('./markham.json', 'Markham'), logWeatherJson('./markham.json', 'Markham'),
] ]
Promise.all(logTablePromises).then(results => {
// const now = new Date()
// const lastUpdatedTime = `${formatTimeUnit(now.getHours())}:${formatTimeUnit(now.getMinutes())}`
// log(`${CREDIT_MSG}\n\nLast updated: ${lastUpdatedTime}`)
log(CREDIT_MSG)
})
Promise.all(logTablePromises).then(results => {})
Loading…
Cancel
Save