Skip to content

Instantly share code, notes, and snippets.

@simonbs
Created July 2, 2022 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonbs/cfd35e0598edd4128d977c4ec5c7db90 to your computer and use it in GitHub Desktop.
Save simonbs/cfd35e0598edd4128d977c4ec5c7db90 to your computer and use it in GitHub Desktop.
Shows rankings for Tour de France 2022.
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: yellow; icon-glyph: bicycle;
let results = await loadResults()
if (config.runsInWidget) {
if (config.widgetFamily == "large") {
let widget = createWidget(results, 8)
await widget.presentLarge()
} else if (config.widgetFamily == "medium") {
let widget = createWidget(results, 3)
await widget.presentMedium()
} else {
let result = results.find(isVingegaard)
let widget = await createRiderWidget(result)
await widget.presentSmall()
}
} else {
let table = createTable(results)
await table.present()
}
async function createRiderWidget(result) {
let img = await loadVingegaardImage()
let widget = new ListWidget()
widget.backgroundColor = new Color("#fbc702")
widget.backgroundImage = img
let wrank = widget.addText(result.rank)
let wvalue = widget.addText(result.value)
wrank.textColor = Color.black()
wrank.font = Font.boldSystemFont(24)
wvalue.textColor = Color.black()
wvalue.font = Font.boldSystemFont(20)
widget.addSpacer()
widget.setPadding(6, 10, 0, 0)
return widget
}
function createWidget(results, count) {
let widget = new ListWidget()
widget.spacing = 5
widget.backgroundColor = new Color("#fbc702")
for (let i = 0; i < count; i++) {
let result = results[i]
let stack1 = widget.addStack()
stack1.layoutHorizontally()
let wrank = stack1.addText(result.rank + ".")
wrank.font = Font.body()
wrank.textColor = Color.black()
stack1.addSpacer(5)
let stack2 = stack1.addStack()
stack2.layoutVertically()
let stack3 = stack2.addStack()
stack3.layoutHorizontally()
let wname = stack3.addText(result.name)
wname.font = Font.body()
wname.textColor = Color.black()
stack3.addSpacer()
let wvalue= stack3.addText(result.value)
wvalue.font = Font.body()
wvalue.textColor = Color.black()
let wsubtitle = stack2.addText(result.subtitle + " (" + result.countryCode + ")")
wsubtitle.font = Font.caption1()
wsubtitle.textColor = Color.black()
}
return widget
}
function createTable(results) {
let table = new UITable()
table.showSeparator = true
for (result of results) {
let row = createRow(result)
table.addRow(row)
}
return table
}
function createRow(result) {
let countryCode = result.countryCode
let subtitle = result.subtitle + " (" + countryCode + ")"
let row = new UITableRow()
row.cellSpacing = 5
row.height = 64
let rankCell = row.addText(result.rank)
let nameCell = row.addText(result.name, subtitle)
let valueCell = row.addText(result.value)
rankCell.widthWeight = 12
nameCell.widthWeight = 58
valueCell.widthWeight = 30
valueCell.rightAligned()
let titleFontSize = 16
let subtitleFontSize = 13
if (countryCode == "DEN") {
rankCell.titleFont = Font.boldSystemFont(titleFontSize)
nameCell.titleFont = Font.boldSystemFont(titleFontSize)
nameCell.subtitleFont = Font.boldSystemFont(subtitleFontSize)
valueCell.titleFont = Font.boldSystemFont(titleFontSize)
} else {
rankCell.titleFont = Font.systemFont(titleFontSize)
nameCell.titleFont = Font.systemFont(titleFontSize)
nameCell.subtitleFont = Font.systemFont(subtitleFontSize)
valueCell.titleFont = Font.systemFont(titleFontSize)
}
return row
}
async function loadResults() {
let url = "https://appservicesport.tv2api.dk/cycling/stages/2373364/results?all=true"
let request = new Request(url)
request.headers = { "Accept": "application/json" }
let json = await request.loadJSON()
return json[0].results
}
function isVingegaard(result) {
return result.name == "Jonas Vingegaard"
}
async function loadVingegaardImage() {
let url = "https://i.ibb.co/4RD1cnk/F584475-A-93-F2-4-C5-D-BC72-87326752143-B.png"
let request = new Request(url)
return request.loadImage()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment