From 884c24fbd4474cdc084b30f2e51826099d5c9e22 Mon Sep 17 00:00:00 2001 From: Kevin Mok Date: Wed, 2 Oct 2019 16:58:39 -0400 Subject: [PATCH] High/low for vertical table --- dark-sky.js | 36 +++++++++++++++++++++++++++--------- dark-sky.sh | 4 ++-- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/dark-sky.js b/dark-sky.js index 5519528..bf0fedc 100644 --- a/dark-sky.js +++ b/dark-sky.js @@ -20,12 +20,15 @@ const CREDIT_MSG = "darksky.net/poweredby • smol.gq/wthr-src\n\n" const jsonReader = (filePath, cb) => { fs.readFile(filePath, (err, fileData) => { if (err) { + log(err) return cb && cb(err) } try { const object = JSON.parse(fileData) + // log(filePath, object['latitude']) return cb && cb(null, object) } catch(err) { + log(err) return cb && cb(err) } }) @@ -33,6 +36,15 @@ const jsonReader = (filePath, cb) => { const round5 = x => { return Math.ceil(x / 5) * 5 } +const updateTempHigh = (temp, index, tempHigh) => { + // +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) => { jsonReader(jsonFile, (err, weatherInfo) => { @@ -41,7 +53,9 @@ const getWeatherTable = (jsonFile, loc, orientation) => { } let output = loc + '\n' - // log(loc) + // value, index + let tempHigh = [-100, -1] + let tempLow = [100, -1] weatherInfo['hourly']['data'][0] = weatherInfo['currently'] if (orientation == 'v') { @@ -49,18 +63,22 @@ const getWeatherTable = (jsonFile, loc, orientation) => { for (let i = 0; i < 12; i++) { const hourInfo = weatherInfo['hourly']['data'][i] const date = new Date(hourInfo['time'] * 1000) + const temp = Math.floor(hourInfo['apparentTemperature']) + tempHigh = updateTempHigh(temp, i, tempHigh) + tempLow = updateTempLow(temp, i, tempHigh) const precipProbability = round5(hourInfo['precipProbability'] * 100) const precipText = ((precipProbability < HIDE_PRECIP_LESS_THAN) ? '' : precipProbability) infoList.push([ date.getHours(), - Math.floor(hourInfo['apparentTemperature']), + temp, ((hourInfo['summary'].includes('Rain')) ? 'Y' : ''), precipText, ]) } + infoList[tempHigh[1]][1] = `${infoList[tempHigh[1]][1]}+` + infoList[tempLow[1]][1] = `${infoList[tempLow[1]][1]}-` output += table(infoList, TABLE_CONFIG) - // log(table(infoList, TABLE_CONFIG)) } else { // horizontal const hoursList = ['H'] @@ -70,20 +88,20 @@ const getWeatherTable = (jsonFile, loc, orientation) => { for (let i = 0; i < 16; i += 2) { const hourInfo = weatherInfo['hourly']['data'][i] 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()) - tempsList.push(Math.floor(hourInfo['apparentTemperature'])) + tempsList.push(temp) 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 minTempIndex = tempsList.indexOf(Math.min(...tempsList.slice(1))) - tempsList[maxTempIndex] = `${tempsList[maxTempIndex]}+` - tempsList[minTempIndex] = `${tempsList[minTempIndex]}-` + tempsList[tempHigh[1]] = `${tempsList[tempHigh[1]]}+` + tempsList[tempLow[1]] = `${tempsList[tempLow[1]]}-` output += table([hoursList, tempsList, rainList, precipList], TABLE_CONFIG) - // log(table([hoursList, tempsList, rainList, precipList], TABLE_CONFIG)) } resolve(output) }) diff --git a/dark-sky.sh b/dark-sky.sh index 346637b..7fb7cb3 100755 --- a/dark-sky.sh +++ b/dark-sky.sh @@ -9,7 +9,7 @@ update-weather-json() { # echo "curl --request GET --url $curl_url > $2.json" } -update-weather-json "43.8180904,-79.3350555" "markham" -update-weather-json "43.6596426,-79.3976676" "toronto" +# update-weather-json "43.8180904,-79.3350555" "markham" +# update-weather-json "43.6596426,-79.3976676" "toronto" node dark-sky.js