Skip to content

Instantly share code, notes, and snippets.

@simonbs
Created January 2, 2020 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonbs/28e3f3083dc84d6ebc143f23eea44418 to your computer and use it in GitHub Desktop.
Save simonbs/28e3f3083dc84d6ebc143f23eea44418 to your computer and use it in GitHub Desktop.
Reads slope data from skeikampen.no
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: snowflake;
let html = await loadHTML()
let stickyItems = getStickyItems(html)
let liftCounts = getLiftCounts(stickyItems[1])
let slopeCounts = getSlopeCounts(stickyItems[2])
let snowDepth = getSnowDepth(stickyItems[3])
let windSpeed = getWindSpeed(stickyItems[4])
let tableTexts = [
createLiftCountText(liftCounts),
createSlopeCountText(slopeCounts),
createSnowDepthText(snowDepth),
createWindSpeedText(windSpeed),
createWeatherText(stickyItems[0])
]
let table = createTable(tableTexts)
if (config.runsWithSiri) {
let speech = createSpeech(liftCounts, snowDepth)
Speech.speak(speech)
}
await table.present()
function createTable(texts) {
let table = new UITable()
table.showSeparators = true
addImageRow(table)
for (text of texts) {
addTextRow(table, text)
}
return table
}
function addImageRow(table) {
let url = "https://tdolillehammer.no/skeikampen/paastigning.jpg"
let row = new UITableRow()
row.height = 300
let cell = row.addImageAtURL(url)
cell.centerAligned()
table.addRow(row)
}
function addTextRow(table, text) {
let row = new UITableRow()
let cell = row.addText(text)
table.addRow(row)
}
async function loadHTML() {
let url = "https://skeikampen.no/aktiviteter/vinter/loypestatus/alpint-loypeservice/"
let req = new Request(url)
return await req.loadString()
}
function getStickyItems(html) {
let divBegin = "<div class=\"sk-sticky\">"
let divEnd = "</div>"
return html
.split(divBegin)[1]
.split(divEnd)[0]
.split("\n")
.map(e => e.trim())
.filter(e => e.length > 0)
.map(removeHTMLFromStickyItem)
.slice(0, -1)
}
function removeHTMLFromStickyItem(e) {
let p1 = /^<span class="sk-sticky__item">/
let p2 = /<\/span>$/
return e
.replace(p1, "")
.replace(p2, "")
}
function getLiftCounts(text) {
let p = /^([0-9]+)\s\/\s([0-9]+)/
let m = text.match(p)
return {
open: m[1],
total: m[2]
}
}
function getSlopeCounts(text) {
let p = /^([0-9]+)\s\/\s([0-9]+)/
let m = text.match(p)
return {
open: m[1],
total: m[2]
}
}
function getSnowDepth(text) {
let p = /([0-9]+)\scm$/
let m = text.match(p)
return m[1]
}
function getWindSpeed(text) {
let p = /([0-9]+)\smps$/
let m = text.match(p)
return m[1]
}
function createLiftCountText(liftCounts) {
return "🚡 "
+ liftCounts.open
+ " / "
+ liftCounts.total
+ " lifts open"
}
function createSlopeCountText(slopeCounts) {
return "⛷ "
+ slopeCounts.open
+ " / "
+ slopeCounts.total
+ " slopes open"
}
function createSnowDepthText(snowDepth) {
return "❄️ Snow depth: "
+ snowDepth
+ " cm"
}
function createWindSpeedText(windSpeed) {
return "💨 Wind speed: "
+ windSpeed
+ " m/s"
}
function createWeatherText(weather) {
return "⛅️ " + weather
}
function createSpeech(liftCounts, snowDepth) {
return "There are "
+ liftCounts.open
+ " out of "
+ liftCounts.total
+ " lifts open and the snow is "
+ snowDepth
+ " cm deep."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment