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.

113 lines
4.2 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\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 getWeatherTable = (jsonFile, loc, orientation) => {
  33. return new Promise((resolve, reject) => {
  34. jsonReader(jsonFile, (err, weatherInfo) => {
  35. if (err) {
  36. reject(err)
  37. }
  38. let output = loc + '\n'
  39. // log(loc)
  40. weatherInfo['hourly']['data'][0] = weatherInfo['currently']
  41. if (orientation == 'v') {
  42. const infoList = [['H', '°C', 'R','%P']]
  43. for (let i = 0; i < 12; i++) {
  44. const hourInfo = weatherInfo['hourly']['data'][i]
  45. const date = new Date(hourInfo['time'] * 1000)
  46. const precipProbability = round5(hourInfo['precipProbability'] * 100)
  47. const precipText = ((precipProbability < HIDE_PRECIP_LESS_THAN)
  48. ? '' : precipProbability)
  49. infoList.push([
  50. date.getHours(),
  51. Math.floor(hourInfo['apparentTemperature']),
  52. ((hourInfo['summary'].includes('Rain')) ? 'Y' : ''),
  53. precipText,
  54. ])
  55. }
  56. output += table(infoList, TABLE_CONFIG)
  57. // log(table(infoList, TABLE_CONFIG))
  58. } else {
  59. // horizontal
  60. const hoursList = ['H']
  61. const tempsList = ['°C']
  62. const rainList = ['R']
  63. const precipList = ['%P']
  64. for (let i = 0; i < 16; i += 2) {
  65. const hourInfo = weatherInfo['hourly']['data'][i]
  66. const date = new Date(hourInfo['time'] * 1000)
  67. hoursList.push(date.getHours())
  68. tempsList.push(Math.floor(hourInfo['apparentTemperature']))
  69. rainList.push((hourInfo['summary'].includes('Rain')) ? 'Y' : '')
  70. const precipProbability = round5(hourInfo['precipProbability'] * 100)
  71. precipList.push((precipProbability < HIDE_PRECIP_LESS_THAN)
  72. ? '' : precipProbability)
  73. }
  74. const maxTempIndex = tempsList.indexOf(Math.max(...tempsList.slice(1)))
  75. const minTempIndex = tempsList.indexOf(Math.min(...tempsList.slice(1)))
  76. tempsList[maxTempIndex] = `${tempsList[maxTempIndex]}+`
  77. tempsList[minTempIndex] = `${tempsList[minTempIndex]}-`
  78. output += table([hoursList, tempsList, rainList, precipList],
  79. TABLE_CONFIG)
  80. // log(table([hoursList, tempsList, rainList, precipList], TABLE_CONFIG))
  81. }
  82. resolve(output)
  83. })
  84. })
  85. }
  86. const formatTimeUnit = unit => { return ((unit < 10) ? '0' : '') + unit }
  87. const getTablePromises = [
  88. getWeatherTable('./markham.json', 'Markham', 'h'),
  89. getWeatherTable('./markham.json', 'Markham', 'v'),
  90. getWeatherTable('./toronto.json', 'Toronto', 'h'),
  91. getWeatherTable('./toronto.json', 'Toronto', 'v'),
  92. ]
  93. Promise.all(getTablePromises).then(tables => {
  94. const mText = tables[0]
  95. const mvText = tables[1]
  96. const tText = tables[2]
  97. const tvText = tables[3]
  98. const writeErrorHandler = error => { if (error) { throw error } }
  99. fs.writeFile('mt.txt', CREDIT_MSG + mText + '\n' + tText, writeErrorHandler)
  100. fs.writeFile('mtv.txt', CREDIT_MSG + mvText + '\n' + tvText, writeErrorHandler)
  101. fs.writeFile('m.txt', CREDIT_MSG + mText, writeErrorHandler)
  102. fs.writeFile('mv.txt', CREDIT_MSG + mvText, writeErrorHandler)
  103. fs.writeFile('t.txt', CREDIT_MSG + tText, writeErrorHandler)
  104. fs.writeFile('tv.txt', CREDIT_MSG + tvText, writeErrorHandler)
  105. })