commit 259d8a08aba00ee9f448cfa7a9263aecdb7f185a Author: Kevin Mok Date: Sun Sep 29 01:33:31 2019 -0400 Initial commit: show hourly info in table diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60e992c --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +*.txt + +api-keys-fish.sh +dark-sky.json + +# node {{{ - + +/lib +/lib-legacy +/node_modules +*.log +/.nyc_output +/coverage +/dist +/dist-debug +/artifacts +/updates +/resources/winsetup/generated.wxs +/resources/winsetup/obj +/resources/winsetup/bin +/resources/win-chocolatey/tools/chocolateyinstall.ps1 +.vs +*.msi +*.nupkg +test/fixtures/**/.fbkpm +/tmp/ +/__tests__/fixtures/**/_* +/__tests__/fixtures/request-cache/GET/localhost/.bin +.idea +.yarn-meta +.pnp.js +.pnp +/packages/lockfile/index.js +.vscode/ + +# }}} default - diff --git a/dark-sky.fish b/dark-sky.fish new file mode 100644 index 0000000..011f3a6 --- /dev/null +++ b/dark-sky.fish @@ -0,0 +1,6 @@ +source api-keys-fish.sh +# IBM +set curl_url "https://api.darksky.net/forecast/$DARK_SKY_KEY/43.8180904,-79.3350555?exclude=minutely%2Cdaily%2Calerts%2Cflags&units=auto" + +curl --request GET --url $curl_url > dark-sky.json +node dark-sky.js > dark-sky.txt diff --git a/dark-sky.js b/dark-sky.js new file mode 100644 index 0000000..4ebe2a6 --- /dev/null +++ b/dark-sky.js @@ -0,0 +1,45 @@ +const log = console.log +const fs = require('fs') +const {table} = require('table') + +const HOURS_AHEAD = 12 + +// 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) + } + }) +} + +jsonReader('./dark-sky.json', (err, weatherInfo) => { + if (err) { + console.log(err) + return + } + + const tableConfig = { + columns: { + 0: { alignment: 'center' }, + 1: { alignment: 'center' }, + 2: { alignment: 'center' }, + } + } + const infoList = [['H', '°C', '%P']] + for (let i = 0; i < HOURS_AHEAD; 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, tableConfig)); +}) diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..a87cdb8 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,132 @@ +{ + "name": "dark-sky-script", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "ajv": { + "version": "6.10.2", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.2.tgz", + "integrity": "sha512-TXtUUEYHuaTEbLZWIKUr5pmBuhDLy+8KYtPYdcV8qC+pOZL+NKqYwvWSRrVXHn+ZmRRAu8vJTAznH7Oag6RVRw==", + "requires": { + "fast-deep-equal": "^2.0.1", + "fast-json-stable-stringify": "^2.0.0", + "json-schema-traverse": "^0.4.1", + "uri-js": "^4.2.2" + } + }, + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==" + }, + "ansi-styles": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "requires": { + "color-convert": "^1.9.0" + } + }, + "astral-regex": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/astral-regex/-/astral-regex-1.0.0.tgz", + "integrity": "sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==" + }, + "color-convert": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "requires": { + "color-name": "1.1.3" + } + }, + "color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" + }, + "emoji-regex": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", + "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==" + }, + "fast-deep-equal": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz", + "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=" + }, + "fast-json-stable-stringify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", + "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=" + }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=" + }, + "json-schema-traverse": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", + "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" + }, + "lodash": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + }, + "punycode": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", + "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" + }, + "slice-ansi": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-2.1.0.tgz", + "integrity": "sha512-Qu+VC3EwYLldKa1fCxuuvULvSJOKEgk9pi8dZeCVK7TqBfUNTH4sFkk4joj8afVSfAYgJoSOetjx9QWOJ5mYoQ==", + "requires": { + "ansi-styles": "^3.2.0", + "astral-regex": "^1.0.0", + "is-fullwidth-code-point": "^2.0.0" + } + }, + "string-width": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz", + "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==", + "requires": { + "emoji-regex": "^7.0.1", + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^5.1.0" + } + }, + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "requires": { + "ansi-regex": "^4.1.0" + } + }, + "table": { + "version": "5.4.6", + "resolved": "https://registry.npmjs.org/table/-/table-5.4.6.tgz", + "integrity": "sha512-wmEc8m4fjnob4gt5riFRtTu/6+4rSe12TpAELNSqHMfF3IqnA+CH37USM6/YR3qRZv7e56kAEAtd6nKZaxe0Ug==", + "requires": { + "ajv": "^6.10.2", + "lodash": "^4.17.14", + "slice-ansi": "^2.1.0", + "string-width": "^3.0.0" + } + }, + "uri-js": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz", + "integrity": "sha512-KY9Frmirql91X2Qgjry0Wd4Y+YTdrdZheS8TFwvkbLWf/G5KNJDCh6pKL5OZctEW4+0Baa5idK2ZQuELRwPznQ==", + "requires": { + "punycode": "^2.1.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..38018d0 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "dark-sky-script", + "version": "1.0.0", + "description": "Fetch local weather information.", + "main": "dark-sky.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Kevin Mok", + "license": "ISC", + "dependencies": { + "table": "^5.4.6" + } +}