|
|
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\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 getWeatherTable = (jsonFile, loc, orientation) => { return new Promise((resolve, reject) => { jsonReader(jsonFile, (err, weatherInfo) => { if (err) { reject(err) } let output = loc + '\n' // log(loc)
weatherInfo['hourly']['data'][0] = weatherInfo['currently'] if (orientation == 'v') { const infoList = [['H', '°C', 'R','%P']] for (let i = 0; i < 12; i++) { const hourInfo = weatherInfo['hourly']['data'][i] const date = new Date(hourInfo['time'] * 1000) const precipProbability = round5(hourInfo['precipProbability'] * 100) const precipText = ((precipProbability < HIDE_PRECIP_LESS_THAN) ? '' : precipProbability) infoList.push([ date.getHours(), Math.floor(hourInfo['apparentTemperature']), ((hourInfo['summary'].includes('Rain')) ? 'Y' : ''), precipText, ]) } output += table(infoList, TABLE_CONFIG) // log(table(infoList, TABLE_CONFIG))
} else { // horizontal
const hoursList = ['H'] const tempsList = ['°C'] const rainList = ['R'] 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'])) 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]}-` output += table([hoursList, tempsList, rainList, precipList], TABLE_CONFIG) // log(table([hoursList, tempsList, rainList, precipList], TABLE_CONFIG))
} resolve(output) }) }) }
const formatTimeUnit = unit => { return ((unit < 10) ? '0' : '') + unit }
const getTablePromises = [ getWeatherTable('./markham.json', 'Markham', 'h'), getWeatherTable('./markham.json', 'Markham', 'v'), getWeatherTable('./toronto.json', 'Toronto', 'h'), getWeatherTable('./toronto.json', 'Toronto', 'v'), ] Promise.all(getTablePromises).then(tables => { 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) })
|