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.

45 lines
1.1 KiB

  1. const log = console.log
  2. const fs = require('fs')
  3. const {table} = require('table')
  4. const HOURS_AHEAD = 12
  5. // https://medium.com/@osiolabs/read-write-json-files-with-node-js-92d03cc82824
  6. const jsonReader = (filePath, cb) => {
  7. fs.readFile(filePath, (err, fileData) => {
  8. if (err) {
  9. return cb && cb(err)
  10. }
  11. try {
  12. const object = JSON.parse(fileData)
  13. return cb && cb(null, object)
  14. } catch(err) {
  15. return cb && cb(err)
  16. }
  17. })
  18. }
  19. jsonReader('./dark-sky.json', (err, weatherInfo) => {
  20. if (err) {
  21. console.log(err)
  22. return
  23. }
  24. const tableConfig = {
  25. columns: {
  26. 0: { alignment: 'center' },
  27. 1: { alignment: 'center' },
  28. 2: { alignment: 'center' },
  29. }
  30. }
  31. const infoList = [['H', '°C', '%P']]
  32. for (let i = 0; i < HOURS_AHEAD; i++) {
  33. const hourInfo = weatherInfo['hourly']['data'][i]
  34. const date = new Date(hourInfo['time'] * 1000)
  35. infoList.push([date.getHours(),
  36. Math.floor(hourInfo['apparentTemperature']),
  37. Math.floor(hourInfo['precipProbability'])])
  38. }
  39. log(table(infoList, tableConfig));
  40. })