Android Robot

DnnForge - NewsArticles

30
I was looking for data to put on my website a few weeks ago, and decided it would be really geeky if I was able to list the last 10 records on my TiVo. Since TiVo provides a local HTTP server that you can access the recording data in XML format, this can be easily accomplished. With your TiVo already hooked up to your local network you can access the following URL https://YourTivoIP/nowplaying/index.html. Where “YourTiVoIp” is the IP address of your server, this page is protected with a self signed certificate which creates a few extra steps we need to perform when pulling down the data with code. When accessing this page you will be prompted for a username and password. The username for the site is “tivo’ and the password is your MAK (media access key). This key can be found on the “My Tivo” configuration site at



http://www.tivo.com  The URL to pull down the XML data is https://YourTivoIp/TiVoConnect?Command=QueryContainer&Container=%2FNowPlaying&Recurse=Yes So how do we pull down data from a website? Well for this example I will be using the WebRequest and WebResponse object.

 

string address = @"www.McWherter.net";

 

// Open the web page

WebRequest request = WebRequest.Create(address);

 

// Get the response from the page

WebResponse response = request.GetResponse();

 

// Create a stream and dump the response into it

Stream recStream = response.GetResponseStream();

 

// create the stream reader to read the dumpted output

StreamReader rdStream = newStreamReader(recStream, System.Text.Encoding.UTF8);

  

// Dump the stream into a string

string dataOut = rdStream.ReadToEnd();



The above example will pull data down from a web site. But to access the TiVo site we need do perform a few more steps. The first step is to add authentication.

 

// Send the log info

request.Credentials = new NetworkCredential("tivo", passwd);


The next step is to deal with the self signed certificate. Self signed certificates cause many head aches, and I wont digress into them, only provide a solution with a disclaimer. This solution works for this implemention because I trust the TiVo server. This method should be used with caution when accessing sites that may not be trusted. This method is only recommend when you are connecting to sites you know can be trusted. To deal with the self signed certificate, first create a new class that imports System.Security.Cryptography.X509Certificates

  347 internal class AcceptCertificatePolicy : ICertificatePolicy

  348 {

  349     public AcceptCertificatePolicy()

  350     {

  351     }

  352     public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate cert, WebRequest req, int certError)

  353     {

  354         // true for all certs even if they are untrusted

  355         return true;

  356     }

  357 }


Use the code below to apply the new certificate policy.

 

// deal w/ the self signed cert

ServicePointManager.CertificatePolicy = new AcceptCertificatePolicy();

The scope of this entry does not include what the ServicePointManger is but Adarsh Krahe  has a great entry about the life cycle here http://blogs.msdn.com/adarshk/articles/345411.aspx

Now with the code above you should be able to deal with the self signed certificate, access the TiVo page that contains the XML. Now the only thing you need to do is process the XML. I decided to create a class named TiVo that contains the Show Title, Episode Title, Description and Recording Data. I then created a function named GetTivoData that parses the XML, and returns a list of the TiVo recordings. I also created a predicate that will return the last 10 shows, but again that’s another blog posting. The GetTivoData function is below and I won’t go into any detail about it. If you have any questions just shoot me an email.

  324 private List<tivo> GetTivoData()

  325 {

  326     List <tivo> tmpRtn = newList <tivo >();

  327     string address = string.Format(@"https://{0}/TiVoConnect?Command=QueryContainer&Container= 2FNowPlaying&Recurse=Yes",

  328             ConfigurationManager .AppSettings["IPTivo"]);

  329     string passwd = "xxxxxxxxxx";

  330     string buff;

  331 

  332     // Open the Tivo page

  333     WebRequest request = WebRequest .Create(address);

  334 

  335     // Send the login info

  336     request.Credentials = new NetworkCredential ("tivo", passwd);

  337 

  338     // deal w/ the self signed cert

  339     ServicePointManager .CertificatePolicy = new AcceptAllCertificatePolicy ();

  340 

  341     // Get the response from the page

  342     WebResponse response = request.GetResponse();

  343 

  344     // Create a stream and dump the response into it

  345     Stream recStream = response.GetResponseStream();

  346 

  347     // create the stream reader to read the dumpted output

  348     StreamReader rdStream = new StreamReader (recStream,System.Text.Encoding .UTF8);

  349 

  350     // Dump the stream into a string

  351     stringdataOut = rdStream.ReadToEnd();

  352 

  353     XmlDocument doc = new XmlDocument ();

  354     doc.LoadXml(dataOut);

  355 

  356     foreach (XmlNode nd in doc.ChildNodes[1].ChildNodes)

  357     {

  358         tivo tmpTivo = new tivo();

  359 

  360         System.Diagnostics.Debug .Print(nd.Name);

  361         if (nd.Name.ToLower() == "item")

  362         {

  363             string showTitle = string.Empty;

  364             string episodeTitle = string.Empty;

  365             string desc = string.Empty;

  366             string capDate = string.Empty;

  367 

  368             // loop though the nodes

  369             foreach (XmlNode showNd in nd.ChildNodes[0].ChildNodes)

  370             {

  371                 System.Diagnostics.Debug .Print(showNd.Name);

  372                 switch (showNd.Name.ToLower())

  373                 {

  374                     case "title":

  375                         tmpTivo.ShowTitle = showNd.InnerText;

  376                         break ;

  377                     case "episodetitle":

  378                         tmpTivo.EpisodeTitle = showNd.InnerText;

  379                         break ;

  380                     case "description":

  381                         tmpTivo.Description = showNd.InnerText;

  382                         break ;

  383                     case "capturedate":

  384                         //TODO: deal with this if i ever want to show the date

  385                         break ;

  386                 }

  387             }

  388 

  389             // add tivo show to the collection

  390             tmpRtn.Add(tmpTivo);

  391         }

  392     }