Skip to content

Instantly share code, notes, and snippets.

@simonbs
Last active May 28, 2019 12:00
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/bdb5caf98de703edc39aaa0018b69719 to your computer and use it in GitHub Desktop.
Save simonbs/bdb5caf98de703edc39aaa0018b69719 to your computer and use it in GitHub Desktop.
Scriptable script that shows what's on Danish television tonight.
let windowStart = new Date()
let windowEnd = new Date()
windowStart.setHours(19)
windowStart.setMinutes(00)
windowStart.setSeconds(00)
windowEnd.setHours(21)
windowEnd.setMinutes(30)
windowEnd.setSeconds(00)
let channelIds = [
"1", // DR1
"3", // TV2
"2", // DR2
"10155" // DR3
]
let epg = await loadEPG(channelIds)
let schedule = createSchedule(epg, windowStart, windowEnd)
let table = createTable(schedule)
QuickLook.present(table)
if (config.runsWithSiri) {
Speech.speak("Here's what's on TV tonight.")
}
async function loadEPG(channelIds) {
let date = new Date()
let y = "" + date.getFullYear()
let m = "" + (date.getMonth() + 1)
let d = "" + date.getDate()
let dateStr = y + "-" + zeroPrefix(m) + "-" + zeroPrefix(d)
let channels = channelIds.map(e => "ch=" + e).join("&")
let baseURL = "https://tvtid-api.api.tv2.dk/api/tvtid/v1/epg/dayviews"
let url = baseURL + "/" + dateStr + "?" + channels
let req = new Request(url)
return await req.loadJSON()
}
function createSchedule(epg, windowStartDate, windowEndDate) {
let schedule = []
for (channel of epg) {
let programs = channel["programs"].filter(prg => {
let windowStartTime = windowStart.getTime()
let windowEndTime = windowEnd.getTime()
let prgStart = prg["start"] * 1000
return prgStart >= windowStart && prgStart <= windowEndTime
})
for (program of programs) {
schedule.push({
"channelId": channel["id"],
"title": program["title"],
"start": program["start"],
"stop": program["stop"]
})
}
}
return schedule.sort((a, b) => {
if (a["start"] < b["start"]) {
return -1
} else if (a["start"] > b["start"]) {
return 1
} else {
return 0
}
})
}
function createTable(schedule) {
let table = new UITable()
for (program of schedule) {
let row = new UITableRow()
let channelId = program["channelId"]
let channelCell = row.addText(channelTitle(channelId))
let titleCell = row.addText(program["title"])
let timeCell = row.addText(
""
+ formattedTime(program["start"]))
timeCell.rightAligned()
channelCell.widthWeight = 15
titleCell.widthWeight = 70
timeCell.widthWeight = 15
table.addRow(row)
}
return table
}
function formattedTime(t) {
let d = new Date(t * 1000)
return ""
+ zeroPrefix(d.getHours().toString())
+ ":"
+ zeroPrefix(d.getMinutes().toString())
}
function channelTitle(id) {
if (id == 1) {
return "DR1"
} else if (id == 2) {
return "DR2"
} else if (id == 3) {
return "TV 2"
} else if (id == 10155) {
return "DR3"
} else {
return "UNKNOWN"
}
}
function channelImage(id) {
let name = null
if (id == 1) {
name = "dr1"
} else if (id == 3) {
name = "tv2"
} else if (id == 10155) {
name = "dr3"
}
if (name != null) {
let fm = FileManager.iCloud()
let docs = fm.documentsDirectory()
let path = fm.joinPath(docs, "assets/channels/" + name + ".png")
return fm.readImage(path)
} else {
return null
}
}
function zeroPrefix(str) {
if (str.length == 1) {
return "0"+str
} else {
return str
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment