Created
October 11, 2018 23:58
-
-
Save 2shortplanks/0094930f2d3781604c59fff3a48103af to your computer and use it in GitHub Desktop.
A scriptable script to work out what’s for lunch
This file contains 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
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(/</g,"<").replace(/>/g,">").replace(/ /g," ").replace(/&/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