Compare commits
1 Commits
master
...
fix-precip
| Author | SHA1 | Date | |
|---|---|---|---|
|
b0c5b7ff48
|
90
dark-sky.js
90
dark-sky.js
@@ -1,7 +1,7 @@
|
|||||||
const log = console.log
|
const log = console.log
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const {table, getBorderCharacters} = require('table')
|
const {table, getBorderCharacters} = require('table')
|
||||||
// const argv = require('yargs').argv
|
const argv = require('yargs').argv
|
||||||
|
|
||||||
const TABLE_CONFIG = {
|
const TABLE_CONFIG = {
|
||||||
// columns: {
|
// columns: {
|
||||||
@@ -14,21 +14,18 @@ const TABLE_CONFIG = {
|
|||||||
const HIDE_PRECIP_LESS_THAN = 10
|
const HIDE_PRECIP_LESS_THAN = 10
|
||||||
// const CREDIT_MSG = "Powered by Dark Sky: https://darksky.net/poweredby/"
|
// 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 = "https://darksky.net/poweredby • https://smol.gq/wthr-src\n"
|
||||||
const CREDIT_MSG = "darksky.net/poweredby • smol.gq/wthr-src\n\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) => {
|
||||||
fs.readFile(filePath, (err, fileData) => {
|
fs.readFile(filePath, (err, fileData) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
log(err)
|
|
||||||
return cb && cb(err)
|
return cb && cb(err)
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const object = JSON.parse(fileData)
|
const object = JSON.parse(fileData)
|
||||||
// log(filePath, object['latitude'])
|
|
||||||
return cb && cb(null, object)
|
return cb && cb(null, object)
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
log(err)
|
|
||||||
return cb && cb(err)
|
return cb && cb(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -36,96 +33,55 @@ const jsonReader = (filePath, cb) => {
|
|||||||
|
|
||||||
const round5 = x => { return Math.ceil(x / 5) * 5 }
|
const round5 = x => { return Math.ceil(x / 5) * 5 }
|
||||||
|
|
||||||
const updateTempHigh = (temp, index, tempHigh) => {
|
const logWeatherJson = (jsonFile, loc) => {
|
||||||
// +1 to offset header
|
|
||||||
return (temp > tempHigh[0]) ? [temp, index + 1] : tempHigh
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateTempLow = (temp, index, tempLow) => {
|
|
||||||
return (temp < tempLow[0]) ? [temp, index + 1] : tempLow
|
|
||||||
}
|
|
||||||
|
|
||||||
const getWeatherTable = (jsonFile, loc, orientation) => {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
jsonReader(jsonFile, (err, weatherInfo) => {
|
jsonReader(jsonFile, (err, weatherInfo) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err)
|
reject(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
let output = loc + '\n'
|
log(loc)
|
||||||
// value, index
|
|
||||||
let tempHigh = [-100, -1]
|
|
||||||
let tempLow = [100, -1]
|
|
||||||
|
|
||||||
weatherInfo['hourly']['data'][0] = weatherInfo['currently']
|
if (argv.v) {
|
||||||
if (orientation == '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)
|
||||||
const temp = Math.floor(hourInfo['apparentTemperature'])
|
infoList.push([date.getHours(),
|
||||||
tempHigh = updateTempHigh(temp, i, tempHigh)
|
Math.floor(hourInfo['apparentTemperature']),
|
||||||
tempLow = updateTempLow(temp, i, tempHigh)
|
Math.floor(hourInfo['precipProbability'])])
|
||||||
const precipProbability = round5(hourInfo['precipProbability'] * 100)
|
|
||||||
const precipText = ((precipProbability < HIDE_PRECIP_LESS_THAN)
|
|
||||||
? '' : precipProbability)
|
|
||||||
infoList.push([
|
|
||||||
date.getHours(),
|
|
||||||
temp,
|
|
||||||
((hourInfo['summary'].includes('Rain')) ? 'Y' : ''),
|
|
||||||
precipText,
|
|
||||||
])
|
|
||||||
}
|
}
|
||||||
infoList[tempHigh[1]][1] = `${infoList[tempHigh[1]][1]}+`
|
log(table(infoList, TABLE_CONFIG));
|
||||||
infoList[tempLow[1]][1] = `${infoList[tempLow[1]][1]}-`
|
|
||||||
output += 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)
|
||||||
const temp = Math.floor(hourInfo['apparentTemperature'])
|
|
||||||
tempHigh = updateTempHigh(temp, i/2, tempHigh)
|
|
||||||
tempLow = updateTempLow(temp, i/2, tempHigh)
|
|
||||||
hoursList.push(date.getHours())
|
hoursList.push(date.getHours())
|
||||||
tempsList.push(temp)
|
tempsList.push(Math.floor(hourInfo['apparentTemperature']))
|
||||||
rainList.push((hourInfo['summary'].includes('Rain')) ? 'Y' : '')
|
// const precipProbability = Math.floor(hourInfo['precipProbability'] * 10) / 10
|
||||||
const precipProbability = round5(hourInfo['precipProbability'] * 100)
|
const precipProbability = round5(hourInfo['precipProbability'] * 100)
|
||||||
precipList.push((precipProbability < HIDE_PRECIP_LESS_THAN)
|
precipList.push((precipProbability < HIDE_PRECIP_LESS_THAN)
|
||||||
? '' : precipProbability)
|
? '' : precipProbability)
|
||||||
}
|
}
|
||||||
tempsList[tempHigh[1]] = `${tempsList[tempHigh[1]]}+`
|
const maxTempIndex = tempsList.indexOf(Math.max(...tempsList.slice(1)))
|
||||||
tempsList[tempLow[1]] = `${tempsList[tempLow[1]]}-`
|
const minTempIndex = tempsList.indexOf(Math.min(...tempsList.slice(1)))
|
||||||
output += table([hoursList, tempsList, rainList, precipList],
|
tempsList[maxTempIndex] = `${tempsList[maxTempIndex]}+`
|
||||||
TABLE_CONFIG)
|
tempsList[minTempIndex] = `${tempsList[minTempIndex]}-`
|
||||||
|
log(table([hoursList, tempsList, precipList], TABLE_CONFIG));
|
||||||
}
|
}
|
||||||
resolve(output)
|
resolve()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatTimeUnit = unit => { return ((unit < 10) ? '0' : '') + unit }
|
const formatTimeUnit = unit => { return ((unit < 10) ? '0' : '') + unit }
|
||||||
|
log(CREDIT_MSG)
|
||||||
const getTablePromises = [
|
logTablePromises = [
|
||||||
getWeatherTable('./markham.json', 'Markham', 'h'),
|
logWeatherJson('./toronto.json', 'Toronto'),
|
||||||
getWeatherTable('./markham.json', 'Markham', 'v'),
|
logWeatherJson('./markham.json', 'Markham'),
|
||||||
getWeatherTable('./toronto.json', 'Toronto', 'h'),
|
|
||||||
getWeatherTable('./toronto.json', 'Toronto', 'v'),
|
|
||||||
]
|
]
|
||||||
Promise.all(getTablePromises).then(tables => {
|
Promise.all(logTablePromises).then(results => {})
|
||||||
const mText = tables[0]
|
|
||||||
const mvText = tables[1]
|
|
||||||
const tText = tables[2]
|
|
||||||
const tvText = tables[3]
|
|
||||||
const writeErrorHandler = error => { if (error) { throw error } }
|
|
||||||
fs.writeFile('mt.txt', CREDIT_MSG + mText + '\n' + tText, writeErrorHandler)
|
|
||||||
fs.writeFile('mtv.txt', CREDIT_MSG + mvText + '\n' + tvText, writeErrorHandler)
|
|
||||||
fs.writeFile('m.txt', CREDIT_MSG + mText, writeErrorHandler)
|
|
||||||
fs.writeFile('mv.txt', CREDIT_MSG + mvText, writeErrorHandler)
|
|
||||||
fs.writeFile('t.txt', CREDIT_MSG + tText, writeErrorHandler)
|
|
||||||
fs.writeFile('tv.txt', CREDIT_MSG + tvText, writeErrorHandler)
|
|
||||||
})
|
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ update-weather-json() {
|
|||||||
# echo "curl --request GET --url $curl_url > $2.json"
|
# echo "curl --request GET --url $curl_url > $2.json"
|
||||||
}
|
}
|
||||||
|
|
||||||
# update-weather-json "43.8180904,-79.3350555" "markham"
|
update-weather-json "43.8180904,-79.3350555" "markham"
|
||||||
# update-weather-json "43.6596426,-79.3976676" "toronto"
|
update-weather-json "43.6596426,-79.3976676" "toronto"
|
||||||
|
|
||||||
node dark-sky.js
|
node dark-sky.js > dark-sky.txt
|
||||||
|
# node dark-sky.js
|
||||||
|
|||||||
Reference in New Issue
Block a user