Last active
May 7, 2021 15:21
-
-
Save achhunna/042179d5c26c68ce09ecc8ea962c33c2 to your computer and use it in GitHub Desktop.
`How's the surf` script using Surfline API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: cyan; icon-glyph: swimmer; | |
// Checks surf height at Pacifica Lindmar beach | |
// Using Surfline's services API | |
const baseUrl = "https://services.surfline.com/kbyg/spots/forecasts" | |
const paramsList = [ | |
{ | |
type: 'wave', | |
query: [ | |
{ | |
key: 'spotId', | |
value: '5842041f4e65fad6a7708976', | |
}, | |
{ | |
key: 'days', | |
value: 1, | |
}, | |
{ | |
key: 'intervalHours', | |
value: 1, | |
}, | |
{ | |
key: 'maxHeights', | |
value: false, | |
}, | |
], | |
}, | |
{ | |
type: 'weather', | |
query: [ | |
{ | |
key: 'spotId', | |
value: '5842041f4e65fad6a7708976', | |
}, | |
{ | |
key: 'days', | |
value: 1, | |
}, | |
{ | |
key: 'intervalHours', | |
value: 1, | |
}, | |
], | |
} | |
] | |
const surfDataUrl = generateUrl(baseUrl, paramsList[0]) | |
const weatherDataUrl = generateUrl(baseUrl, paramsList[1]) | |
function generateUrl(baseUrl, params) { | |
const baseParams = params.query.reduce((acc, query, i) => { | |
let amp = '&' | |
if (i === 0 || i === params.query.length - 1) { | |
amp = '' | |
} | |
return `${acc + amp + query.key}=${query.value}` | |
}, '') | |
return `${baseUrl}/${params.type}?${baseParams}` | |
} | |
function getData(waves, temps) { | |
const currentSec = Math.round(Date.now() / 1000) | |
let surfHeight | |
let temperature | |
for (let i = 0; i < waves.length; i++) { | |
if (waves[i].timestamp && waves[i].timestamp >= currentSec && temps[i].timestamp >= currentSec) { | |
surfHeight = Math.round((waves[i].surf.min + waves[i].surf.max)/2) | |
temperature = Math.round(temps[i].temperature) | |
break | |
} | |
} | |
return { surfHeight, temperature } | |
} | |
let req = new Request(surfDataUrl) | |
const surfData = await req.loadJSON() | |
req = new Request(weatherDataUrl) | |
const weatherData = await req.loadJSON() | |
const waves = surfData.data.wave | |
const temps = weatherData.data.weather | |
const dataObj = getData(waves, temps) | |
let response = `surf height is around ${dataObj.surfHeight} feet, and weather is ${dataObj.temperature} degrees.` | |
console.log(response) | |
if (config.runsWithSiri) { | |
if (response.length > 0) { | |
Speech.speak(" Currently, in Pacifica, " + response) | |
} else { | |
Speech.speak("Oops, Something went wrong") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment