Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // by T. Francis ([email protected])
- // adapted from "getWebText" covid monitoring script/widget
- //Modified by mvan231 for use with StormGlass.io API
- //Heights are in meters
- //Date / Time is in your local time as offset from UTC (API) responds woth UTC time
- //@@@@@@@@@@@@@@@@@@@@
- //Start options
- //paste your APIkey below from StormGlass.io
- const APIkey=""
- //set to true to use a fake location (set to Lahaina, HI)
- //set to false to use device location
- const locTest = true
- //End Options
- //@@@@@@@@@@@@@@@@@@@@
- let dateeee = new Date()
- log(dateeee)
- //2023-03-17T02:31:00+00:00
- let dF1 = new DateFormatter()
- dF1.dateFormat = 'yyyy-MM-dd HH:mm:ssZ'
- //log(dF.date('2023-03-17 02:31:00+00:00'))
- let dF2 = new DateFormatter()
- dF2.dateFormat = 'yyyy-MM-dd HH:mm'
- let dateTest = dF2.string(dateeee)
- log(dateTest)
- // ==> set colors
- // background color
- const bkgdColor = Color.black()
- // tide colors
- const highColor = Color.blue()
- const lowColor = Color.green()
- // heading colors
- const flashColor = Color.white()
- const stationColor = Color.blue()
- const todayColor = Color.white()
- const dateColor = Color.gray()
- // ==> set font sizes
- const flashSize = 10
- const stationSize = 14
- const dateSize = 11
- const lineSize = 10
- // ==> set rounding for tide heights
- // (number of decimal places to round to)
- const roundPlaces = 0
- // generate dates (defaults to today & tomorrow)
- //const today = new Date()
- let currentDate = new Date();
- let cDay = addLeadingZeros(currentDate.getDate())
- let cMonth = addLeadingZeros(currentDate.getMonth() + 1)
- let cYear = currentDate.getFullYear()
- let cDateFmt = cYear+"-"+cMonth+"-"+cDay
- let cDateTod = cYear+"-"+cMonth+"-"+cDay
- let cDay1 = addLeadingZeros(Number(cDay)+1)
- //log(cDay1)
- let cDateTom = cYear+"-"+cMonth+"-"+cDay1
- //38.68890° N, 9.33826° W
- const lahainaLat=38.68890
- const lahainaLng=9.33826
- const loc = locTest? Location.reverseGeocode(lahainaLat,lahainaLng, 'en-us'):await Location.current()
- log(loc)
- let lat = locTest?lahainaLat:loc.latitude,lng= lahainaLng?lahainaLng:loc.longitude
- // create api query url with station id and dates
- const url = `https://api.stormglass.io/v2/tide/extremes/point?lat=${lat}&lng=${lng}&start=${cDateTod}&end=${cDateTom}`
- console.log(url)
- // load data
- const req = new Request(url)
- req.headers = {Authorization: APIkey}
- var res = await req.loadJSON()
- //log response
- log(res)
- //log request headers
- log(req.headers)
- // create widget or run within Scriptable
- if (config.runsInWidget) {
- // create and show widget
- let widget = createWidget(res.meta.station.name, createPredictions(), bkgdColor)
- Script.setWidget(widget)
- Script.complete()
- } else {
- console.log(createPredictions())
- // make table
- let table = new UITable()
- // add header
- let row = new UITableRow()
- row.isHeader = true
- row.addText(`Next tides for ${res.meta.station.name}`)
- table.addRow(row)
- // fill data
- for (p in res.data) {
- if(res.data[p]["type"]=="high"){
- var ttype = "High"
- }
- else {
- var ttype = "Low"
- }
- console.log(res.data[p])
- let dd = dF1.date(res.data[p]["time"].replace("T", " "))
- dd= dF2.string(dd)
- log(`dd is ${dd}`)
- table.addRow(createRow(ttype, dd/*res.data[p]["time"]*/))
- }
- // present table
- table.present()
- }
- // create list of lists used by widget
- function createPredictions() {
- var result = ""
- var datetime = ""
- var ddate = ""
- var height = ""
- lines = []
- // make components more user friendly
- for (p in res.data) {
- // H/L to High/Low (tide)
- if(res.data[p]["type"]=="high"){
- var ttype = "High"
- }
- else {
- var ttype = "Low"
- }
- // split data and time
- let dd = dF1.date(res.data[p]["time"].replace("T", " "))
- dd= dF2.string(dd)
- log(`dd is ${dd}`)
- datetime = dd
- height = res.data[p]["height"]
- height = Math.round(height * (10^roundPlaces))/(10^roundPlaces)
- // create day's entry to pass to widget
- let line = [ddate,dd,ttype,height]
- lines.push(line)
- }
- return lines
- }
- function createRow(title, datetime) {
- let row = new UITableRow()
- row.addText(datetime)
- row.addText(title)
- return row
- }
- function addLeadingZeros(n) {
- if (n <= 9) {
- return "0" + n;
- }
- return n
- }
- function createWidget(pretitle, lines, color) {
- let w = new ListWidget()
- w.backgroundColor = bkgdColor
- // add Tides header
- let flash = w.addText("Tides")
- flash.textColor = Color.white()
- flash.textOpacity = 1
- flash.font = Font.systemFont(10)
- // add station name
- let preTxt = w.addText(pretitle)
- preTxt.textColor = Color.red()
- preTxt.textOpacity = 0.8
- preTxt.font = Font.systemFont(14)
- w.addSpacer(5)
- // add prediction lines
- var refDate = ""
- var txt = ""
- var dateHeader = ""
- for (line in lines) {
- if (lines[line][0] != refDate) {
- refDate = lines[line][0]
- dateHeader = w.addText(lines[line][0])
- if (lines[line][0] == "Today") {
- dateHeader.textColor = todayColor
- }
- else {
- dateHeader.textColor = Color.gray()
- }
- dateHeader.font = Font.systemFont(11)
- }
- let txt = "• " + lines[line][1] + ": " + lines[line][2] + " (" + lines[line][3] + " ft.)"
- let text = w.addText(txt)
- if (lines[line][2] == "High") {
- text.textColor = highColor
- }
- else {
- text.textColor = lowColor
- }
- text.font = Font.systemFont(10)
- }
- return w
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement