Action
Takapuna Tides
Queries Tidespy for tide information for a location - requires an API key (free) from Tidespy and setting of placename id (currently set to Takapuna, NZ => 3147).
Set to use metric units and request 2 days’ information, but tweak away… (and forgive my dubious coding)
Steps
-
defineTemplateTag
name placename
template 3147
-
script
let placename = draft.getTemplateTag("placename"); let credential = Credential.create("TideSpy", "API key for access to TideSpy raw API."); credential.addTextField("apiKey", "API Key"); credential.authorize(); let todayDate = strftime(new Date(),"%Y%m%d"); let output = []; let header = "| Tide | Date-Time | Time Until | Height | \n|:----|---:|----:|----:|\n"; let http = HTTP.create(); // create HTTP object //var response = http.request({"url": queryurl,"method": "GET"}); let response = http.request({ "url": "http://tidespy.com/api/tideturns", "method": "GET", "parameters": { "pn":placename, "unit":"m", "start":todayDate, "days":"2", "key":credential.getValue("apiKey") } }); if (response.success){ let tides = JSON.parse(response.responseText); let dayMinute = 60*(parseInt(strftime(new Date(),"%H"))) + parseInt(strftime(new Date(),"%M")); for(let x in tides.Turns){ // Ignore past tide information for today if(tides.Turns[x].Date == todayDate && tides.Turns[x].Minute < dayMinute) { continue; }else{ let hourminute = pad(Math.floor(tides.Turns[x].Minute/60),2) + ":" + pad(Math.floor(tides.Turns[x].Minute % 60),2); let isodate = tides.Turns[x].Date.substring(0,4) + "-" + tides.Turns[x].Date.substring(4,6) + "-" + tides.Turns[x].Date.substring(6); let timeUntil = timeBetweenNow(isodate,hourminute); output.push("|" + tides.Turns[x].HorL + "|" + isodate + " " + hourminute + "|" + timeUntil + "|" + tides.Turns[x].Height + "|"); } } draft.setTemplateTag("content", header + output.join("\n")); }else{ console.log(response.statusCode); console.log(response.error); } // F U N C T I O N S function pad(num, size) { let s = num+""; while (s.length < size) s = "0" + s; return s; } // Calculate the time in hours and minutes between the time now and an ISO date and 24-hour time function timeBetweenNow(d,m){ let nowDate = strftime(new Date(),"%Y-%m-%dT%H:%M"); let diff = Math.abs(new Date(d + "T" + m) - new Date(nowDate)); let totalmin = Math.floor(diff/60000); let hour = Math.floor(totalmin/60); let min = Math.floor(totalmin % 60); return hour + "h " + min +"m"; }
-
htmlpreview
<!DOCTYPE html> <html dir="auto"> <head> <title>[[title]]</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> @charset "utf-8"; :root { --main-bg-color: white; --main-color: black; --alternate-bg-color: #efefef; --alternate-color: #222222; --main-border-color: #BBBBBB; --link-color: #627EC9; } @media (prefers-color-scheme: dark) { :root { --main-bg-color: #222222; --main-color: #eeeeee; --alternate-bg-color: #444444; --alternate-color: #cccccc; --main-border-color: #AAAAAA; --link-color: #627EC9; } } html { font-size: 100%; font-family: -apple-system, BlinkMacSystemFont, "helvetica neue", helvetica, roboto, noto, "segoe ui", arial, sans-serif; line-height: 1.4; } body { margin: 0; padding: 1em; background-color: var(--main-bg-color); color: var(--main-color); } @media (max-device-width: 480px) {} @media (min-device-width: 481px) { body { margin: auto; max-width: 600px; } } blockquote { font-style: italic; margin: 1.5em 2em; padding: 1em; background-color: var(--alternate-bg-color); color: var(--alternate-color); } a { color: var(--link-color); } pre { display: block; overflow: scroll; width: 100%; background-color: var(--alternate-bg-color); padding: .5em 1em; margin: 1em 0; } code { background-color: var(--alternate-bg-color); color: var(--alternate-color); font-family: Menlo, Courier, sans-serif; padding: 2px 3px; } table { margin: 1.5em 0; border: 1px solid var(--main-border-color); border-collapse: collapse; } th { padding: .25em .5em; background: var(--alternate-bg-color); border: 1px solid var(--main-border-color); } td { padding: .25em .5em; border: 1px solid var(--main-border-color); } img { max-width: 90%; } </style> </head> <body> %%[[content]]%% </body> </html>
Options
-
After Success Default Notification Info Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.