Advertisement
mvan231

Tides Widget StormGlass API

Mar 17th, 2023 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.45 KB | Source Code | 0 0
  1. // by T. Francis ([email protected])
  2. // adapted from "getWebText" covid monitoring script/widget
  3. //Modified by mvan231 for use with StormGlass.io API
  4.  
  5. //Heights are in meters
  6. //Date / Time is in your local time as offset from UTC (API) responds woth UTC time
  7.  
  8. //@@@@@@@@@@@@@@@@@@@@
  9. //Start options
  10.  
  11. //paste your APIkey below from StormGlass.io
  12. const APIkey=""
  13.  
  14. //set to true to use a fake location (set to Lahaina, HI)
  15. //set to false to use device location
  16. const locTest = true
  17.  
  18. //End Options
  19. //@@@@@@@@@@@@@@@@@@@@
  20.  
  21. let dateeee = new Date()
  22. log(dateeee)
  23. //2023-03-17T02:31:00+00:00
  24. let dF1 = new DateFormatter()
  25. dF1.dateFormat = 'yyyy-MM-dd HH:mm:ssZ'
  26. //log(dF.date('2023-03-17 02:31:00+00:00'))
  27. let dF2 = new DateFormatter()
  28. dF2.dateFormat = 'yyyy-MM-dd HH:mm'
  29. let dateTest = dF2.string(dateeee)
  30. log(dateTest)
  31.  
  32. // ==> set colors
  33. // background color
  34. const bkgdColor = Color.black()
  35. // tide colors
  36. const highColor = Color.blue()
  37. const lowColor = Color.green()
  38. // heading colors
  39. const flashColor = Color.white()
  40. const stationColor = Color.blue()
  41. const todayColor = Color.white()
  42. const dateColor = Color.gray()
  43.  
  44. // ==> set font sizes
  45. const flashSize = 10
  46. const stationSize = 14
  47. const dateSize = 11
  48. const lineSize = 10
  49.  
  50. // ==> set rounding for tide heights
  51. // (number of decimal places to round to)
  52. const roundPlaces = 0
  53.  
  54. // generate dates (defaults to today & tomorrow)
  55. //const today = new Date()
  56. let currentDate = new Date();
  57. let cDay = addLeadingZeros(currentDate.getDate())
  58. let cMonth = addLeadingZeros(currentDate.getMonth() + 1)
  59. let cYear = currentDate.getFullYear()
  60. let cDateFmt = cYear+"-"+cMonth+"-"+cDay
  61. let cDateTod = cYear+"-"+cMonth+"-"+cDay
  62. let cDay1 = addLeadingZeros(Number(cDay)+1)
  63. //log(cDay1)
  64. let cDateTom = cYear+"-"+cMonth+"-"+cDay1
  65. //38.68890° N, 9.33826° W
  66. const lahainaLat=38.68890
  67. const lahainaLng=9.33826
  68. const loc = locTest? Location.reverseGeocode(lahainaLat,lahainaLng, 'en-us'):await Location.current()
  69. log(loc)
  70.  
  71. let lat = locTest?lahainaLat:loc.latitude,lng= lahainaLng?lahainaLng:loc.longitude
  72. // create api query url with station id and dates
  73. const url = `https://api.stormglass.io/v2/tide/extremes/point?lat=${lat}&lng=${lng}&start=${cDateTod}&end=${cDateTom}`
  74.  
  75. console.log(url)
  76.  
  77. // load data
  78. const req = new Request(url)
  79. req.headers = {Authorization: APIkey}
  80. var res = await req.loadJSON()
  81. //log response
  82. log(res)
  83. //log request headers
  84. log(req.headers)
  85.  
  86. // create widget or run within Scriptable
  87. if (config.runsInWidget) {
  88.   // create and show widget
  89.   let widget = createWidget(res.meta.station.name, createPredictions(), bkgdColor)
  90.   Script.setWidget(widget)
  91.   Script.complete()
  92. } else {
  93.   console.log(createPredictions())
  94.   // make table
  95.   let table = new UITable()
  96.  
  97.   // add header
  98.   let row = new UITableRow()
  99.   row.isHeader = true
  100.   row.addText(`Next tides for ${res.meta.station.name}`)
  101.   table.addRow(row)
  102.  
  103.   // fill data
  104.   for (p in res.data) {
  105.     if(res.data[p]["type"]=="high"){
  106.       var ttype = "High"
  107.     }
  108.     else {
  109.       var ttype = "Low"  
  110.     }
  111.     console.log(res.data[p])
  112.     let dd = dF1.date(res.data[p]["time"].replace("T", " "))
  113.     dd= dF2.string(dd)
  114.     log(`dd is ${dd}`)
  115.     table.addRow(createRow(ttype, dd/*res.data[p]["time"]*/))
  116.   }
  117.  
  118.   // present table
  119.   table.present()
  120. }
  121.  
  122. // create list of lists used by widget
  123. function createPredictions() {
  124.   var result = ""
  125.   var datetime = ""
  126.   var ddate = ""
  127.   var height = ""
  128.   lines = []
  129.   // make components more user friendly
  130.   for (p in res.data) {
  131.     // H/L to High/Low (tide)
  132.     if(res.data[p]["type"]=="high"){
  133.       var ttype = "High"
  134.     }
  135.     else {
  136.       var ttype = "Low"  
  137.     }
  138.     // split data and time
  139.     let dd = dF1.date(res.data[p]["time"].replace("T", " "))
  140.     dd= dF2.string(dd)
  141.     log(`dd is ${dd}`)
  142.     datetime = dd
  143.  
  144.  
  145.     height = res.data[p]["height"]
  146.     height = Math.round(height * (10^roundPlaces))/(10^roundPlaces)
  147.     // create day's entry to pass to widget
  148.     let line = [ddate,dd,ttype,height]
  149.     lines.push(line)
  150.   }
  151.   return lines
  152. }
  153.  
  154. function createRow(title, datetime) {
  155.   let row = new UITableRow()
  156.   row.addText(datetime)
  157.   row.addText(title)
  158.   return row
  159. }
  160.  
  161. function addLeadingZeros(n) {
  162.   if (n <= 9) {
  163.     return "0" + n;
  164.   }
  165.   return n
  166. }
  167.  
  168.  
  169. function createWidget(pretitle, lines, color) {
  170.   let w = new ListWidget()
  171.   w.backgroundColor = bkgdColor
  172.   // add Tides header
  173.   let flash = w.addText("Tides")
  174.   flash.textColor = Color.white()
  175.   flash.textOpacity = 1
  176.   flash.font = Font.systemFont(10)
  177.   // add station name
  178.   let preTxt = w.addText(pretitle)
  179.   preTxt.textColor = Color.red()
  180.   preTxt.textOpacity = 0.8
  181.   preTxt.font = Font.systemFont(14)
  182.   w.addSpacer(5)
  183.   // add prediction lines
  184.   var refDate = ""
  185.   var txt = ""
  186.   var dateHeader = ""
  187.   for (line in lines) {
  188.     if (lines[line][0] != refDate) {
  189.       refDate = lines[line][0]
  190.       dateHeader = w.addText(lines[line][0])
  191.       if (lines[line][0] == "Today") {
  192.         dateHeader.textColor = todayColor
  193.       }
  194.       else {
  195.         dateHeader.textColor = Color.gray()
  196.       }
  197.       dateHeader.font = Font.systemFont(11)
  198.     }
  199.     let txt = "• " + lines[line][1] + ": " + lines[line][2] + " (" + lines[line][3] + " ft.)"
  200.     let text = w.addText(txt)
  201.     if (lines[line][2] == "High") {
  202.       text.textColor = highColor
  203.     }
  204.     else {
  205.       text.textColor = lowColor
  206.     }  
  207.     text.font = Font.systemFont(10)
  208.   }
  209.   return w
  210. }
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement