Wednesday, October 28, 2015

Minor Hacking: Crunchyroll,, Kodi

I've been using lately to track movies and TV shows as I watch them. Not that keeping track of such things is of any great importance, but I like keeping a list and rating the things I watch. Trakt keeps a nice calendar of upcoming new episodes, and it looks a whole lot prettier than the Google docs spreadsheet that I used to use.

I was pleased to see that there's a Kodi add-on to automatically update the site when you watch something. This is known as "scrobbling," a term invented (as far as I know) by in relation to music. Using sites like Trakt (for videos) or (for music) is possible without the automatic updates you get with scrobbling, but it's kind of a pain, and easy to forget to update. So the scrobbler add-on is a great feature.

I watch a goodly amount of anime via Crunchyroll, which has its own website and app, but neither of those communicate with Trakt. Fortunately there's a Kodi add-on for Crunchyroll, too. In theory, watching a video in Kodi using the Crunchyroll add-on should be automatically logged to Trakt, via the Trakt add-on. Nothing is ever that simple, though! I got it all set up, and...nothing happened.

Well, I didn't spend 15 years as a system integrator without learning a few things about getting recalcitrant software packages to work nicely together. This isn't exactly a multi-million-dollar ERP system like the ones I worked on as an IT consultant and technical lead, but the principle is the same. 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. Crunchyroll add-on tells Kodi to play a video
  2. Kodi notifies Trakt add-on that the video is playing
  3. Trakt add-on updates the site
First step, identify where the process is breaking down. Kodi has a log file which includes add-on messages, and they'll dump a lot of info if you enable debug mode. I was able to identify messages relating to the first two steps, but the third was missing. The Trakt add-on knew there was a video playing, but it never updated the site. A little digging through the Trakt add-on code showed me why: it didn't have enough information. Specifically, the name of the video was missing (as well as season/episode numbers, for TV shows).

Now that we know what's missing, the next step is to backtrack through the process to find where that information should be coming from. The Crunchyroll add-on should be telling us what video it's playing (step 1 in the process). I poked around the add-on code until I found the spot where the video was being played, and saw that it was setting a field called Title. The Trakt add-on, however, was looking for a different field called TVShowTitle, plus season and episode fields. A bit of work in the Python script added those extra fields. (Here's the code, for anyone interested.)

With that update in place, the Trakt add-on was able to identify what was being played, and it did indeed scrobble...when I played a show through the Crunchyroll add-on, the site showed it as played. Success!

Well, partial success. Turns out that this only worked for some shows. In other cases, the Trakt add-on would try to scrobble, but an error something like "method exists, but no record found" would show up in the log, and nothing on the site got updated. After a bit more research, I determined that this was happening because the show title on Crunchyroll and were different. Trakt actually knows about alternative titles, including the ones that Crunchyroll uses, but the scrobble attempt wasn't looking at alternative titles.

This problem could be fixed at either end of the process: at step 1, by making the Crunchyroll add-on use the primary title; or at step 3, by looking at the alternative titles in Trakt. I decided to go with the change in Trakt, because this might affect more than just Crunchyroll. Other add-ons might have the same issue. If I changed the Trakt add-on code, then every add-on attempting to use an alternative show title should work.

This was a more complex update than the first one. In the API, there's a scrobble method, which the add-on was using. But that method has no ability to recognize alternative titles. Fortunately the API also has a search method, which can find those alternative titles. The fix involved adding that search method call to the Trakt add-on, using it to search for the alternative title, taking the result and extracting the primary title, and finally calling the scrobble method using the primary title. (Here's the code.)

With both updates in place, all the shows I'm watching in Kodi via the Crunchyroll add-on are being updated on the site. (At least, all of them thus far.) The code changes have been submitted to the add-on maintainers to be included in a future update, so other people will get the same functionality without having to re-invent this particular wheel.

No comments:

Post a Comment