Skip to content

Instantly share code, notes, and snippets.

@sammillner
Created September 30, 2020 06:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sammillner/96922a9ebc82f2460c12778b12cbe35c to your computer and use it in GitHub Desktop.
Save sammillner/96922a9ebc82f2460c12778b12cbe35c to your computer and use it in GitHub Desktop.
Fivethirtyeight senate 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/senate_us_latest.json"
const req = new Request(url)
const res = await req.loadJSON()
const classicWinProb = Math.round(res.find(f => f.type == "classic").candidates.find(p => p.candidate == "Democrats").winprob) + "%"
const classicWinSeats = res.find(f => f.type == "classic").candidates.find(p => p.candidate == "Democrats").seats.mean.toFixed(1)
const liteWinProb = Math.round(res.find(f => f.type == "lite").candidates.find(p => p.candidate == "Democrats").winprob) + "%"
const liteWinSeats = res.find(f => f.type == "lite").candidates.find(p => p.candidate == "Democrats").seats.mean.toFixed(1)
const deluxeWinProb = Math.round(res.find(f => f.type == "deluxe").candidates.find(p => p.candidate == "Democrats").winprob) + "%"
const deluxeWinSeats = res.find(f => f.type == "deluxe").candidates.find(p => p.candidate == "Democrats").seats.mean.toFixed(1)
if (config.runsInWidget) {
// create and show widget
let widget = createWidget(
"Senate",
`Lite: ${liteWinProb} (${liteWinSeats})`,
`Classic: ${classicWinProb} (${classicWinSeats})`,
`Deluxe: ${deluxeWinProb} (${deluxeWinSeats})`,
"#000000")
Script.setWidget(widget)
Script.complete()
} else {
// make table
let table = new UITable()
// add header
let row = new UITableRow()
row.isHeader = true
row.addText("Senate Forecast")
table.addRow(row)
// fill data
table.addRow(createRow("Lite", liteWinProb, liteWinSeats))
table.addRow(createRow("Classic", classicWinProb, classicWinSeats))
table.addRow(createRow("Deluxe", deluxeWinProb, deluxeWinSeats))
if (config.runsWithSiri)
Speech.speak("The Democrats have a ${liteWinProb} chance of winning the senate")
// present table
table.present()
}
function createRow(title, winProb, winSeats) {
let row = new UITableRow()
row.addText(title)
row.addText(winProb.toString()).rightAligned()
row.addText(winSeats.toString()).rightAligned()
return row
}
function createWidget(pretitle, lite, classic, deluxe, 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 liteText = w.addText(lite)
liteText.textColor = Color.white()
liteText.font = Font.systemFont(10)
w.addSpacer(5)
let classicText = w.addText(classic)
classicText.textColor = Color.white()
classicText.font = Font.systemFont(10)
w.addSpacer(5)
let deluxeText = w.addText(deluxe)
deluxeText.textColor = Color.white()
deluxeText.font = Font.systemFont(10)
w.addSpacer(5)
return w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment