From 3ad87f085eaa49364b170b2bc4555b1cd5be6f5a Mon Sep 17 00:00:00 2001 From: Kevin Mok Date: Sun, 29 Sep 2019 14:29:13 -0400 Subject: [PATCH] Markham/Toronto tables, horizontal orientation Credit Dark Sky. --- .gitignore | 2 +- dark-sky.fish | 13 +++-- dark-sky.js | 76 ++++++++++++++++++--------- package-lock.json | 130 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +- todo.md | 23 ++++++++ 6 files changed, 218 insertions(+), 29 deletions(-) create mode 100644 todo.md diff --git a/.gitignore b/.gitignore index 60e992c..7909ae4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ *.txt api-keys-fish.sh -dark-sky.json +*.json # node {{{ - diff --git a/dark-sky.fish b/dark-sky.fish index 19457bb..3d36150 100755 --- a/dark-sky.fish +++ b/dark-sky.fish @@ -1,8 +1,15 @@ #!/usr/bin/fish 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 +# takes (coords, location_name) as parameters +function update-weather-json + set curl_url "https://api.darksky.net/forecast/$DARK_SKY_KEY/$argv[1]?exclude=minutely%2Cdaily%2Calerts%2Cflags&units=auto" + curl --request GET --url $curl_url > $argv[2].json + # echo "curl --request GET --url $curl_url > $argv[2].json" +end + +update-weather-json "43.8180904,-79.3350555" "markham" +update-weather-json "43.6596426,-79.3976676" "toronto" + node dark-sky.js > dark-sky.txt diff --git a/dark-sky.js b/dark-sky.js index c564ee2..6666661 100644 --- a/dark-sky.js +++ b/dark-sky.js @@ -1,8 +1,17 @@ const log = console.log const fs = require('fs') const {table, getBorderCharacters} = require('table') +const argv = require('yargs').argv -const HOURS_AHEAD = 12 +const TABLE_CONFIG = { + // columns: { + // 0: { alignment: 'center' }, + // 1: { alignment: 'center' }, + // 2: { alignment: 'center' }, + // }, + border: getBorderCharacters(`ramac`) +} +const CREDIT_MSG = "Powered by Dark Sky: https://darksky.net/poweredby/" // https://medium.com/@osiolabs/read-write-json-files-with-node-js-92d03cc82824 const jsonReader = (filePath, cb) => { @@ -19,28 +28,47 @@ const jsonReader = (filePath, cb) => { }) } -jsonReader('./dark-sky.json', (err, weatherInfo) => { - if (err) { - console.log(err) - return - } - - const tableConfig = { - columns: { - 0: { alignment: 'center' }, - 1: { alignment: 'center' }, - 2: { alignment: 'center' }, - }, - border: getBorderCharacters(`ramac`) - } - 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'])]) - } +const logWeatherJson = (jsonFile, loc) => { + return new Promise((resolve, reject) => { + jsonReader(jsonFile, (err, weatherInfo) => { + if (err) { + reject(err) + } + + log(loc) + + if (argv.v) { + const infoList = [['H', '°C', '%P']] + for (let i = 0; i < 12; 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, TABLE_CONFIG)); + } else { + // horizontal + const hoursList = ['H'] + const tempsList = ['°C'] + const precipList = ['%P'] + for (let i = 0; i < 9; i++) { + const hourInfo = weatherInfo['hourly']['data'][i] + const date = new Date(hourInfo['time'] * 1000) + hoursList.push(date.getHours()) + tempsList.push(Math.floor(hourInfo['apparentTemperature'])) + precipList.push(Math.floor(hourInfo['precipProbability'])) + } + log(table([hoursList, tempsList, precipList], TABLE_CONFIG)); + } + resolve() + }) + }) +} - log(table(infoList, tableConfig)); +logTablePromises = [ + logWeatherJson('./markham.json', 'Markham'), + logWeatherJson('./toronto.json', 'Toronto')] +Promise.all(logTablePromises).then(results => { + log(`${CREDIT_MSG}\n\nLast updated: ${new Date().toLocaleTimeString()}`) }) diff --git a/package-lock.json b/package-lock.json index a87cdb8..3d2b26b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,6 +33,21 @@ "resolved": "https://registry.npmjs.org/astral-regex/-/astral-regex-1.0.0.tgz", "integrity": "sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==" }, + "camelcase": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==" + }, + "cliui": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-5.0.0.tgz", + "integrity": "sha512-PYeGSEmmHM6zvoef2w8TPzlrnNpXIjTipYK780YswmIP9vjxmd6Y2a3CB2Ks6/AU8NHjZugXvo8w3oWM2qnwXA==", + "requires": { + "string-width": "^3.1.0", + "strip-ansi": "^5.2.0", + "wrap-ansi": "^5.1.0" + } + }, "color-convert": { "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", @@ -46,6 +61,11 @@ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, + "decamelize": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", + "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=" + }, "emoji-regex": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", @@ -61,6 +81,19 @@ "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=" }, + "find-up": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", + "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", + "requires": { + "locate-path": "^3.0.0" + } + }, + "get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==" + }, "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", @@ -71,16 +104,66 @@ "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" }, + "locate-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", + "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", + "requires": { + "p-locate": "^3.0.0", + "path-exists": "^3.0.0" + } + }, "lodash": { "version": "4.17.15", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" }, + "p-limit": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.1.tgz", + "integrity": "sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==", + "requires": { + "p-try": "^2.0.0" + } + }, + "p-locate": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", + "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", + "requires": { + "p-limit": "^2.0.0" + } + }, + "p-try": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", + "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==" + }, + "path-exists": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" + }, "punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" }, + "require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=" + }, + "require-main-filename": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz", + "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==" + }, + "set-blocking": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", + "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=" + }, "slice-ansi": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-2.1.0.tgz", @@ -127,6 +210,53 @@ "requires": { "punycode": "^2.1.0" } + }, + "which-module": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz", + "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=" + }, + "wrap-ansi": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-5.1.0.tgz", + "integrity": "sha512-QC1/iN/2/RPVJ5jYK8BGttj5z83LmSKmvbvrXPNCLZSEb32KKVDJDl/MOt2N01qU2H/FkzEa9PKto1BqDjtd7Q==", + "requires": { + "ansi-styles": "^3.2.0", + "string-width": "^3.0.0", + "strip-ansi": "^5.0.0" + } + }, + "y18n": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", + "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==" + }, + "yargs": { + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-14.0.0.tgz", + "integrity": "sha512-ssa5JuRjMeZEUjg7bEL99AwpitxU/zWGAGpdj0di41pOEmJti8NR6kyUIJBkR78DTYNPZOU08luUo0GTHuB+ow==", + "requires": { + "cliui": "^5.0.0", + "decamelize": "^1.2.0", + "find-up": "^3.0.0", + "get-caller-file": "^2.0.1", + "require-directory": "^2.1.1", + "require-main-filename": "^2.0.0", + "set-blocking": "^2.0.0", + "string-width": "^3.0.0", + "which-module": "^2.0.0", + "y18n": "^4.0.0", + "yargs-parser": "^13.1.1" + } + }, + "yargs-parser": { + "version": "13.1.1", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.1.tgz", + "integrity": "sha512-oVAVsHz6uFrg3XQheFII8ESO2ssAf9luWuAd6Wexsu4F3OtIW0o8IribPXYrD4WC24LWtPrJlGy87y5udK+dxQ==", + "requires": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + } } } } diff --git a/package.json b/package.json index 38018d0..022353b 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "author": "Kevin Mok", "license": "ISC", "dependencies": { - "table": "^5.4.6" + "table": "^5.4.6", + "yargs": "^14.0.0" } } diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..272d197 --- /dev/null +++ b/todo.md @@ -0,0 +1,23 @@ +# Todo +- add to my site +- deliver text/HTML depending on User-Agent in header + - https://nginx.org/en/docs/http/ngx_http_browser_module.html + - https://www.nginx.com/resources/wiki/modules/user_agent/ + - https://www.whatsmyua.info/api/v1/ua + - colors + - ANSI escape codes + - https://github.com/gabx/archlinux/blob/master/etc/scripts/colortest +- fetch on load + - Lua os.execute in Nginx + - fetch JSON in JS + - generate table dynamically +- icons + +# Done +- display both locations + - location text +- table orientation +- credit Dark Sky + - "Powered by Dark Sky: https://darksky.net/poweredby/" + - Dark Sky image + - https://darksky.net/dev/docs/terms