Skip to content

Instantly share code, notes, and snippets.

@simonbs
Created March 10, 2019 08:50
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/05e9aced762616180d9a90e1dfd4405b to your computer and use it in GitHub Desktop.
Save simonbs/05e9aced762616180d9a90e1dfd4405b to your computer and use it in GitHub Desktop.
Scriptable Script: Cycling Classifications
let url = "https://sports-data.api.tv2.dk/sports-data-backend/cycling/stages/1720933/standings"
let req = new Request(url)
req.headers = { "Accept": "application/json" }
let json = await req.loadJSON()
let rawStandings = json["cyclingGeneralStandings"]
let standings = rawStandings.map(mapStanding)
let table = formatStandings(standings)
QuickLook.present(table)
if (config.runsWithSiri) {
let tts = siriText(standings)
Speech.speak(tts)
Pasteboard.copy(tts)
}
function siriText(standings) {
let f = standings[0]
let s = standings.find(isFuglsang)
return s["name"]
+ " is " + ordinal(s["rank"])
+ " with a result of "
+ f["result"] + " " + s["result"]
+ "."
}
function isFuglsang(standing) {
return standing["name"] == "Jakob Fuglsang"
}
function formatStandings(standings) {
let table = new UITable()
table.showSeparator = true
for (s of standings) {
let row = createRow(s)
table.addRow(row)
}
return table
}
function createRow(standing) {
let s = standing
let row = new UITableRow()
row.isHeader = isFuglsang(s)
row.cellSpacing = 5
let jerseyId = s["shortTeamName"].toLowerCase()
let jerseyURL = "http://gfx.tv2.dk/images/_static/1720933/jerseys/"+jerseyId+".png"
let rankCell = row.addText(s["rank"])
let nameCell = row.addText(s["shortName"])
let teamJerseyCell = row.addImageAtURL(jerseyURL)
let teamNameCell = row.addText(s["shortTeamName"])
let resultCell = row.addText(s["result"])
rankCell.widthWeight = 12
nameCell.widthWeight = 38
teamJerseyCell.widthWeight = 15
teamJerseyCell.centerAligned()
teamNameCell.widthWeight = 15
resultCell.widthWeight = 30
resultCell.rightAligned()
return row
}
function mapStanding(raw) {
let p = raw["participant"]
return {
"rank": raw["rank"],
"name": p["name"],
"shortName": p["shortName"],
"teamName": p["team"]["name"],
"shortTeamName": p["team"]["shortName"],
"result": raw["result"]
}
}
function ordinal(i) {
let j = i % 10
let k = i % 100
if (j == 1 && k != 11) {
return i + "st"
}
if (j == 2 && k != 12) {
return i + "nd"
}
if (j == 3 && k != 13) {
return i + "rd"
}
return i + "th"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment