Wednesday, November 4, 2015

Minor Hacking: MythTV, Kodi, Trakt.tv

I previously posted about getting Kodi, the Crunchyroll add-on, and the Trakt.tv add-on to play nicely together. In this post, I'm doing something similar with Kodi, the MythTV PVR add-on, and the Trakt.tv addon.
As I've mentioned before, I use MythTV to record over-the-air broadcast TV. I then use Kodi on my Fire TV to watch the recordings. My goal is to have my MythTV recordings that I watch in Kodi automatically update Trakt.tv.

The same basic principle applies as the last time I did this: Follow the data flow, track the information at each step, and fix whatever connections are missing. In this case, this is what ought to be happening:
  1. MythTV PVR add-on tells Kodi to play a recording
  2. Kodi notifies Trakt add-on that the video is playing
  3. Trakt add-on updates the Trakt.tv site
Looks very similar to the process from the Crunchyroll integration, which should make this pretty straightforward. Of course, there's always a hitch. In this case, that hitch is that the MythTV PVR add-on provides very little information about what is actually playing. All I could find was a single field called "label" of the form "Show Name - Episode Name." For example, "The Flash - The Darkness and the Light" for a recent Flash episode.

I spent a while poking around the MythTV PVR addon and the Kodi APIs looking for something more, but as far as I can tell, there's no other data about the recording available. The MythTV backend actually knows some more stuff, like the TVDB ID of the episode, which I know because it's accessible through MythWeb on my server. But the add-on doesn't seem to be translating that information through to Kodi. Trying to modify the add-on would be more than what I consider "minor hacking", I'm afraid - it's a large and complicated beast. (I did put in a request for the developer to consider adding more data about the recordings, though.)

So for now, we're stuck with the label as our only data source. I suppose it's possible that you might occasionally get lucky and the Trakt.tv search function would actually hit the right episode based on that label, but most of the time you'll get nothing. However, Trakt does provide an episode search function through the same text search API that I used in the Crunchyroll integration. Split that label into the show and episode name, and Trakt will be happy to search for just the episode name.

Turns out that Trakt is pretty good at fuzzy searching...too good. Searching for that example Flash episode "The Darkness and the Light" returns hundreds of episodes from all kinds of shows, using the words Darkness, Light, or both. I forced a stricter search by using quotation marks around the title, and that got things down to a more reasonable level. It's still likely that more than one show has episodes with the same title, so I also had to loop through all the results and compare the show names. Once I find a search result that also has a show name of "The Flash", I know I've got the right one.

Those search results conveniently include all kinds of information that Trakt knows about the episode, including the season number and episode number, and the internal Trakt ID for the show. That makes it easy to tell the scrobbler function which episode to update. Here's the code changes to implement this.

I'm not totally happy with this particular hack. It relies too heavily on that "label" field - if the MythTV PVR add-on changes the format, or stops setting it altogether, the whole thing falls apart. But it works for now, and if my request to the MythTV PVR add-on developer for more recording data comes through, I can change the Trakt.tv add-on accordingly.