Wednesday, October 15, 2008

Rest APIs for SageTV

Last week I reported that I had created a Java RPC api for SageTV. This week I've released the Rest API addition.

Using the Rest APIs you can access any of the SageTV services using a simple Url syntax. It helps to know and understand the javadoc, since the Rest APIs follow that structure.

For example to tell SageTV to refresh its media file collection, you can use the following url:

http://server:8080/sagex/rpcXml/Global/RunLibraryImportScan/false

or if you wanted to find a list of clients connected to your server:

http://server:8080/sagex/rpcXml/Global/GetConnectedClients


All responses are returned as Xml and documented in the project's wiki.

Monday, October 6, 2008

Java 5 Features - Part 1 - Formatting

I've been using Java for a long time, and one of the first comments I made about Java, when I switched from C, was "Where's printf?". Most people will never need to use printf, and in the few cases that you need to format something, you can use Java's MessageFormat class, at least that's what Java enthusiasts would have you believe.

As you may know, Java 5 introduced the printf() function. Even though I knew it was there, I still resisted using it until recently. And now, I can't imagine not having it. I don't know why I resisted using it? I used Generics as soon as it was availble, but printf() just eluded me, even though I found it to a great function in C.

I may be one of the last people that still write command line applications. I like command line applications, and a well written command line application can support a gui, if needed. But, command line applications require a lot of printing to the console screen. In java, this resulted in a lot of ugly code like

System.out.println("Unable to open File: " + file.getAbsolutePath() + " for writing. Please check permissions.");


But using printf, you can clean that up a little....
System.out.printf("Unable to open File: %s for writing. Please check permissions.", file.getAbsolutePath());


I don't suggest that you abuse the printf function, but know it's there. Also know that the String function has a static method for using printf formatting. This can be handy at times when you need to log a lot of data in a single line, or if you need to simply format a String for some reason.

For example,
log.debug(String.format("The Process: %s took %sms to run.", process, time));


or

String msg = String.format("%-20s: %s", cmd, description);


The whole printf function can do a lot of pretty complex formatting, so check it out, and the next your are printing messages to the console, just maybe you'll use printf().

Java RPC for SageTV

I released a Java RPC library for SageTV. Sometimes, I find myself needing remote access to SageTV to do things that are just not convenient to do from the SageUI or the web server interface.

Initially the small project started out as a part of the GUI redesign of the Batch Metadata tools that I released earlier, but I thought it would be useful in other cases. (The GUI redesign is not released yet, but it's getting there)

The API is completely generated from the public SageTV javadoc, so it is complete. Also, the API enables you to write code and then choose to either embed it in SageTV or run it externally.

An example use of this api would include... Write a small Java utility to find all Watched TV Shows and then move them to an archival area. Or, on a nightly basis, find all HD recorded shows and transcode them into SD so that your SD MVP extenders can play them as well.

Both the client and the server are included in the same small jar (~70k), and the client will attempt to automatically find the running RPC server, so it should be zero configuration from the client side.

I hope the API may be of use to people that want to write java applications that interact with SageTV.