-
Notifications
You must be signed in to change notification settings - Fork 6
Examples
These examples (and any other use of the API which passes username and password in the URL) will only work if TVheadend is configured to use 'basic' or 'basic and digest' authentication.
Parameters passed to TVH using the GET method must be URI-encoded:
curl 'http://user:pass@localhost:9981/api/epg/events/grid?limit=999&channel=BBC%20ONE'
Alternatively use the POST Method:
curl --data 'limit=999&channel=BBC ONE' 'http://user:pass@localhost:9981/api/epg/events/grid'
To make the output more human-readable, pipe it through json_pp (included in the perl package on many distributions) or jq.
This simple example lists some details about upcoming timers, sorted in date order. To work through a PHP-enabled web server, the PHP.INI setting "allow_url_fopen" must be ON.
<!DOCTYPE html>
<html>
<head>
<title>TVH PHP test</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr><th>Date</th><th>Start</th><th>End</th><th>Title</th></tr>
<?php
$timers = get_timers();
foreach($timers as $t) {
$start = strftime("%H:%M", $t["start"]);
$stop = strftime("%H:%M", $t["stop"]);
$date = strftime("%a %e/%m", $t["start"]);
echo "<tr><td>$date</td><td>$start</td><td>$stop</td><td>{$t['disp_title']}</td></tr>";
}
function get_timers() {
$url = "http://admin:[email protected]:9981/api/dvr/entry/grid_upcoming?sort=start";
$json = file_get_contents($url);
$j = json_decode($json, true);
$ret = &$j["entries"];
return $ret;
}
?>
</table>
</body>
</html>
For an example of what can be done with the API in PHP see https://github.com/dave-p/TVHadmin.
It is a 'feature' of Javascript that a script can only access remote content from the same source (IP and port number) as the script was loaded from. Hence to call the TVHeadend API from Javascript, the script must be hosted on TVHeadend's built-in web server. To do this, place your script in /usr/share/tvheadend/src/webui/static
; it can then be accessed from URL http://user:[email protected]:9981/static/
. Note that this is unintended behaviour and may change in the future.
This example carries out the same task as the PHP example above. Your TVHeadend user and password are required in the req.open line.
<!DOCTYPE html>
<html>
<head>
<title>TVH Javascript test</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
table, th, td {
border: 1px solid black;
}
</style>
<script type="text/javascript">
var url = "http://your.server:9981/api/dvr/entry/grid_upcoming?sort=start";
var req = new XMLHttpRequest();
req.onreadystatechange=function() {
if (req.readyState==4 && req.status==200) {
var data = req.response;
const table = document.getElementById("result");
data.entries.forEach(function(entry) {
var start = new Date(entry.start * 1000);
var stop = new Date(entry.stop * 1000);
var row = table.insertRow();
html = "<td>" + start.toLocaleDateString() +
"</td><td>" + start.toLocaleTimeString() +
"</td><td>" + stop.toLocaleTimeString() +
"</td><td>" + entry.disp_title + "</td>";
row.innerHTML = html;
});
}
};
req.open('GET', url, true, "user", "pass");
req.responseType = 'json';
req.send(null);
</script>
</head>
<body>
<table>
<thead>
<tr>
<th>Date</th>
<th>Start</th>
<th>End</th>
<th>Title</th>
</tr>
</thead>
<tbody id="result"></tbody>
</table>
</body>
</html>
The TVHeadend source includes a utility to examine and modify the contents of the internal TVH database. See https://github.com/tvheadend/tvheadend/tree/master/lib/api/python.