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.

87 lines
2.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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. if (argv.v) {
  40. const infoList = [['H', '°C', '%P']]
  41. for (let i = 0; i < 12; i++) {
  42. const hourInfo = weatherInfo['hourly']['data'][i]
  43. const date = new Date(hourInfo['time'] * 1000)
  44. infoList.push([date.getHours(),
  45. Math.floor(hourInfo['apparentTemperature']),
  46. Math.floor(hourInfo['precipProbability'])])
  47. }
  48. log(table(infoList, TABLE_CONFIG));
  49. } else {
  50. // horizontal
  51. const hoursList = ['H']
  52. const tempsList = ['°C']
  53. const precipList = ['%P']
  54. for (let i = 0; i < 16; i += 2) {
  55. const hourInfo = weatherInfo['hourly']['data'][i]
  56. const date = new Date(hourInfo['time'] * 1000)
  57. hoursList.push(date.getHours())
  58. tempsList.push(Math.floor(hourInfo['apparentTemperature']))
  59. // const precipProbability = Math.floor(hourInfo['precipProbability'] * 10) / 10
  60. const precipProbability = round5(hourInfo['precipProbability'] * 100)
  61. precipList.push((precipProbability < HIDE_PRECIP_LESS_THAN)
  62. ? '' : precipProbability)
  63. }
  64. const maxTempIndex = tempsList.indexOf(Math.max(...tempsList.slice(1)))
  65. const minTempIndex = tempsList.indexOf(Math.min(...tempsList.slice(1)))
  66. tempsList[maxTempIndex] = `${tempsList[maxTempIndex]}+`
  67. tempsList[minTempIndex] = `${tempsList[minTempIndex]}-`
  68. log(table([hoursList, tempsList, precipList], TABLE_CONFIG));
  69. }
  70. resolve()
  71. })
  72. })
  73. }
  74. const formatTimeUnit = unit => { return ((unit < 10) ? '0' : '') + unit }
  75. log(CREDIT_MSG)
  76. logTablePromises = [
  77. logWeatherJson('./toronto.json', 'Toronto'),
  78. logWeatherJson('./markham.json', 'Markham'),
  79. ]
  80. Promise.all(logTablePromises).then(results => {})