Last active
May 28, 2019 12:00
-
-
Save simonbs/f6a5919959f15e29c0cf85bc9d370001 to your computer and use it in GitHub Desktop.
Scriptable script that shows what's on Danish television right now.
This file contains 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
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 siriArgs = args.siriShortcutArguments | |
let channelId = siriArgs.channel | |
let channelIds = [] | |
if (channelId != null) { | |
channelIds = [channelId] | |
} else { | |
channelIds = [ | |
"1", // DR1 | |
"3", // TV2 | |
"2", // DR2 | |
"10155" // DR3 | |
] | |
} | |
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 r = new Request(url) | |
let json = await r.loadJSON() | |
var s = {} | |
for (channel of json) { | |
let id = channel["id"] | |
let prgs = channel["programs"] | |
let prg = prgs.find(filterProgram) | |
s[id] = prg | |
} | |
let schedule = prettySchedule(s) | |
QuickLook.present(schedule) | |
if (config.runsWithSiri) { | |
if (channelId != null) { | |
let prg = s[channelId] | |
let title = prg.title | |
Speech.speak( | |
"There's currently \"" | |
+ title + "\" on " | |
+ channelTitle(channelId) + ".") | |
} else { | |
Speech.speak("Here's what's currently on TV.") | |
} | |
} | |
function prettySchedule(s) { | |
let table = new UITable() | |
let channelIds = Object.keys(s) | |
let l = channelIds.length | |
for (var i = 0; i < l; i++) { | |
let row = new UITableRow() | |
let id = channelIds[i] | |
let prg = s[id] | |
let channelCell = row.addText(channelTitle(id)) | |
let titleCell | |
let timeCell | |
if (prg) { | |
titleCell = row.addText(prg["title"]) | |
timeCell = row.addText(formattedTime(prg["start"])) | |
} else { | |
titleCell = row.addText("") | |
timeCell = row.addText("") | |
} | |
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 filterProgram(prg) { | |
let time = new Date().getTime() / 1000 | |
let start = prg["start"] | |
let stop = prg["stop"] | |
return time >= start && time <= stop | |
} | |
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