Hacking the XML in Your TiVo
by Bob DuCharme
|
Pages: 1, 2, 3
Command-line Retrieval of TiVo XML
The XML retrieved with this interface doesn't do us much good in a browser. To do something interesting with it, we want to pull the XML down from the server and pass it to an XSLT stylesheet that transforms and stores the result in a file that can be passed to the next step in our application. Remember, though, about the security dialog boxes: if you used the browser to retrieve the XML version without trying the HTML example first, you had to go through the same security dialog boxes, and a non-browser tool for retrieving the XML must negotiate the same issues.
The GNU wget tool, which is available for Windows and included with most Linux distributions, makes this easy. In the words of its home page, it's a "free software package for retrieving files using HTTP, HTTPS and FTP, the most widely used Internet protocols. It is a non-interactive command-line tool, so it may easily be called from scripts, cron jobs, terminals without X-Windows support, etc." (Fans of curl will find that it can pull XML out of their TiVos as well.)
Passing a URL as a single parameter to wget pulls down the named file to your hard disk. For example, entering the following code downloads Google's current index.html file to your current directory:
wget http://www.google.com
Dozens of optional command-line switches let you customize wget's behavior for more complex situations such as negotiating the security checks that your TiVo puts in the way of a straight HTTP request. The --http-user and --http-password parameters are self-explanatory. We'll supply the word "tivo" and the MAC ID for these values, as we did when we pointed a browser at the TiVo box and it asked for a username and password. To get past the "Web Site Certified by an Unknown Authority" warning, we'll add the --no-check-certificate switch. The -O switch is handy for setting a specific output filename, because while wget had an easy enough time figuring out that Google's homepage was in a file named index.html, the filename that it infers from a REST URL full of ampersands and percent signs can get messy. The following wget command line works identically under Windows and Linux:
wget --no-check-certificate --http-user=tivo --http-password=your-MAK-here
-O nowplaying.xml "https://192.168.2.103/TiVoConnect?
Command=QueryContainer&Container=%2FNowPlaying&Recurse=Yes"
(This single command is split into three lines to fit here. When rejoining the pieces, make sure to put a space after your http-password value but none after the question mark after TiVoConnect.) This command downloads the list of recorded episodes and stores them in the nowplaying.xml file. The quotes around the URL may not be necessary, but they make things a little less confusing for the Linux shell, where ampersands indicate that a job should run in the background.
The Recurse=Yes parameter in the URL tells the TiVoConnect application to go through the entire tree of information in the NowPlaying container, which is why it gets information about all the individual recorded episodes. With Recurse set to No, it lists only those shows whose episodes the TiVo records, as shown in the first illustration above. This is what we want for our TiVoRoll:
wget --no-check-certificate --http-user=tivo --http-password=your-MAK-here
-O tivoroll.xml "https://192.168.2.103/TiVoConnect?
Command=QueryContainer&Container=%2FNowPlaying&Recurse=No"
Once wget has pulled down an XML version of the information we want, we can use XSLT to convert it to the format we want. For the TiVoRoll added to my weblog, the stylesheet is pretty short and simple:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:t="http://www.tivo.com/developer/calypso-protocol-1.5/"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="t"
version="1.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="t:TiVoContainer">
<div class="tivoroll">
<p>
<xsl:apply-templates/>
</p>
</div>
</xsl:template>
<xsl:template match="t:Item/t:Details/t:Title">
<xsl:value-of select="."/><br/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
All text nodes are suppressed because the second template rule uses an xsl:value-of instruction to get the text that it needs: the name of each show being recorded.
Note that everything in the TiVo source XML file is in the http://www.tivo.com/developer/calypso-protocol-1.5/ namespace declared at the beginning of that file, so the stylesheet must declare that before referencing specific elements in the source document.
Using the XML that I pulled off of my TiVo just now as a source document, the stylesheet above created the following result:
<div xmlns="http://www.w3.org/1999/xhtml" class="tivoroll">
<p>Monty Python's Flying Circus
<br/>I Love Lucy
<br/>Daria
<br/>The Colbert Report
<br/>The Daily Show With Jon Stewart
<br/>The Simpsons
<br/>NHL Hockey
<br/>
</p>
</div>
Once I FTP this to the appropriate directory on my weblog's server, a Movable Type template's $MTInclude instruction incorporates it into the right-hand column of my weblog's index page.