Last active
October 29, 2020 10:59
-
-
Save miklosme/6569da1b5bb200ba45cc5f0e71921716 to your computer and use it in GitHub Desktop.
Scriptable script to create a FiveThirtyEight forecast widget
This file contains hidden or 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 = `https://projects.fivethirtyeight.com/2020-election-forecast/us_simulations.json`; | |
const req = new Request(url); | |
const res = await req.loadJSON(); | |
if (config.runsInWidget) { | |
const biden = res[0].simulations.filter((x) => x.winner === 'Biden').length; | |
const trump = res[0].simulations.filter((x) => x.winner === 'Trump').length; | |
let widget = new ListWidget(); | |
widget.backgroundColor = new Color('#000'); | |
let preTxt = widget.addText('FiveThirtyEight'); | |
preTxt.textColor = Color.white(); | |
preTxt.textOpacity = 0.8; | |
preTxt.font = Font.systemFont(16); | |
widget.addSpacer(20); | |
let bidenText = widget.addText(`Biden: ${biden}%`.toLocaleString()); | |
bidenText.textColor = Color.white(); | |
bidenText.font = Font.boldSystemFont(20); | |
widget.addSpacer(5); | |
let trumpText = widget.addText(`Trump: ${trump}%`.toLocaleString()); | |
trumpText.textColor = Color.white(); | |
trumpText.textOpacity = 0.8; | |
trumpText.font = Font.systemFont(18); | |
Script.setWidget(widget); | |
Script.complete(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment