Skip to content

Instantly share code, notes, and snippets.

@2shortplanks
Created October 11, 2018 23:58
Show Gist options
  • Save 2shortplanks/0094930f2d3781604c59fff3a48103af to your computer and use it in GitHub Desktop.
Save 2shortplanks/0094930f2d3781604c59fff3a48103af to your computer and use it in GitHub Desktop.
A scriptable script to work out what’s for lunch
const url = "http://www.rcscares.org/lunch-menu/"
try {
// ###########################################
// work out the name of the dow we want lunch for
// ###########################################
const days = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"
]
const now = new Date()
let dayIndex = now.getDay()
// take one away since our named days start on
// monday not sunday. Add one if lunchtime
// has already passed today
if (now.getHours() < 12) {
dayIndex--
}
// lunch is only M-F
// sunday? saturday? want monday
if (dayIndex < 0 || dayIndex > 4) {
dayIndex = 0
}
const day = days[ dayIndex ]
// ###########################################
// download webpage
// ###########################################
const r = new Request(url)
let body = await r.loadString()
// ###########################################
// parse out the bit we care about
// ###########################################
const m = body.match(new RegExp(`<strong[^>]*>\s*${day}.*(?:(?!strong).)+`,'s'))
const output = m[0].replace(new RegExp("(.)(?:<strong(?!>NO)|This institution).*$","s"), "$1")
// ###########################################
// output
// ###########################################
// note that this is very bad HTML stripping, but
// its good enough. replace tags with periods so
// that siri puts the right pauses in
const str = output.replace(/<[^>]*>/g, '.').replace(/^[.]+/g,"").replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&nbsp;/g," ").replace(/&amp;/g,"&")
Speech.speak(str)
WebView.loadHTML(`<html><body style='font-size:48pt'>${output}</body></html>`)
// ###########################################
// error handling
// ###########################################
} catch {
Speech.speak('Problem parsing webpage')
Safari.openInApp(url)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment