Fetch local weather from Dark Sky API. https://wthr.ml/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.3 KiB

  1. const log = console.log
  2. const fs = require('fs')
  3. const {table, getBorderCharacters} = require('table')
  4. const argv = require('yargs').argv
  5. const TABLE_CONFIG = {
  6. // columns: {
  7. // 0: { alignment: 'center' },
  8. // 1: { alignment: 'center' },
  9. // 2: { alignment: 'center' },
  10. // },
  11. border: getBorderCharacters(`ramac`)
  12. }
  13. const HIDE_PRECIP_LESS_THAN = 10
  14. // const CREDIT_MSG = "Powered by Dark Sky: https://darksky.net/poweredby/"
  15. // const CREDIT_MSG = "https://darksky.net/poweredby • https://smol.gq/wthr-src\n"
  16. const CREDIT_MSG = "darksky.net/poweredby • smol.gq/wthr-src\n"
  17. // https://medium.com/@osiolabs/read-write-json-files-with-node-js-92d03cc82824
  18. const jsonReader = (filePath, cb) => {
  19. fs.readFile(filePath, (err, fileData) => {
  20. if (err) {
  21. return cb && cb(err)
  22. }
  23. try {
  24. const object = JSON.parse(fileData)
  25. return cb && cb(null, object)
  26. } catch(err) {
  27. return cb && cb(err)
  28. }
  29. })
  30. }
  31. const round5 = x => { return Math.ceil(x / 5) * 5 }
  32. const logWeatherJson = (jsonFile, loc) => {
  33. return new Promise((resolve, reject) => {
  34. jsonReader(jsonFile, (err, weatherInfo) => {
  35. if (err) {
  36. reject(err)
  37. }
  38. log(loc)
  39. weatherInfo['hourly']['data'][0] = weatherInfo['currently']
  40. if (argv.v) {
  41. const infoList = [['H', '°C', 'R','%P']]
  42. for (let i = 0; i < 12; i++) {
  43. const hourInfo = weatherInfo['hourly']['data'][i]
  44. const date = new Date(hourInfo['time'] * 1000)
  45. const precipProbability = round5(hourInfo['precipProbability'] * 100)
  46. const precipText = ((precipProbability < HIDE_PRECIP_LESS_THAN)
  47. ? '' : precipProbability)
  48. infoList.push([
  49. date.getHours(),
  50. Math.floor(hourInfo['apparentTemperature']),
  51. ((hourInfo['summary'].includes('Rain')) ? 'Y' : ''),
  52. precipText,
  53. ])
  54. }
  55. log(table(infoList, TABLE_CONFIG));
  56. } else {
  57. // horizontal
  58. const hoursList = ['H']
  59. const tempsList = ['°C']
  60. const rainList = ['R']
  61. const precipList = ['%P']
  62. for (let i = 0; i < 16; i += 2) {
  63. const hourInfo = weatherInfo['hourly']['data'][i]
  64. const date = new Date(hourInfo['time'] * 1000)
  65. hoursList.push(date.getHours())
  66. tempsList.push(Math.floor(hourInfo['apparentTemperature']))
  67. rainList.push((hourInfo['summary'].includes('Rain')) ? 'Y' : '')
  68. const precipProbability = round5(hourInfo['precipProbability'] * 100)
  69. precipList.push((precipProbability < HIDE_PRECIP_LESS_THAN)
  70. ? '' : precipProbability)
  71. }
  72. const maxTempIndex = tempsList.indexOf(Math.max(...tempsList.slice(1)))
  73. const minTempIndex = tempsList.indexOf(Math.min(...tempsList.slice(1)))
  74. tempsList[maxTempIndex] = `${tempsList[maxTempIndex]}+`
  75. tempsList[minTempIndex] = `${tempsList[minTempIndex]}-`
  76. log(table([hoursList, tempsList, rainList, precipList], TABLE_CONFIG));
  77. }
  78. resolve()
  79. })
  80. })
  81. }
  82. const formatTimeUnit = unit => { return ((unit < 10) ? '0' : '') + unit }
  83. log(CREDIT_MSG)
  84. logTablePromises = [
  85. logWeatherJson('./toronto.json', 'Toronto'),
  86. logWeatherJson('./markham.json', 'Markham'),
  87. ]
  88. Promise.all(logTablePromises).then(results => {})