Skip to content

Instantly share code, notes, and snippets.

@sammillner
Created September 30, 2020 06:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sammillner/c3b427c7a4bdd734d2e76ef67cb86be7 to your computer and use it in GitHub Desktop.
Save sammillner/c3b427c7a4bdd734d2e76ef67cb86be7 to your computer and use it in GitHub Desktop.
Fivethirtyeight general election forecast
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
const url = "https://projects.fivethirtyeight.com/2020-election-forecast/us_timeseries.json"
const req = new Request(url)
const res = await req.loadJSON()
const Biden = Math.round(res[0].candidates.find(x => x.candidate == "Biden").dates[0].winprob) + "%"
const Trump = Math.round(res[0].candidates.find(x => x.candidate == "Trump").dates[0].winprob) + "%"
if (config.runsInWidget) {
// create and show widget
let widget = createWidget("General Election", `Biden: ${Biden}` , `Trump: ${Trump}`, "#000000")
Script.setWidget(widget)
Script.complete()
} else {
// make table
let table = new UITable()
// add header
let row = new UITableRow()
row.isHeader = true
row.addText("Forecast")
table.addRow(row)
// fill data
table.addRow(createRow("Biden", `${Biden}`))
table.addRow(createRow("Trump", Trump))
if (config.runsWithSiri)
Speech.speak("Biden has a ${Biden} chance of winning the election")
// present table
table.present()
}
function createRow(title, number) {
let row = new UITableRow()
row.addText(title)
row.addText(number.toString()).rightAligned()
return row
}
function createWidget(pretitle, title, subtitle, color) {
let w = new ListWidget()
w.backgroundColor = new Color(color)
let preTxt = w.addText(pretitle)
preTxt.textColor = Color.white()
preTxt.textOpacity = 0.8
preTxt.font = Font.systemFont(14)
w.addSpacer(5)
let titleTxt = w.addText(title)
titleTxt.textColor = Color.white()
titleTxt.font = Font.systemFont(22)
w.addSpacer(5)
let subTxt = w.addText(subtitle)
subTxt.textColor = Color.white()
subTxt.textOpacity = 0.8
subTxt.font = Font.systemFont(18)
return w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment