Skip to main content
  1. Blog Archive/

Scripting MythTV

At the end of June, I gave a short talk to the London Ruby Users Group on some code I’ve been working on to enable me to interface with MythTV on a programatic level. MythTV is Open Source software which allows the recording and playback of TV on a Linux box, turning it into a PVR. I use this at home to record TV from Freeview.

MythTV has a web interface written for it in the form of MythWeb, and it allows decent remote interaction with the system. It is also my main mode of interaction when trawling for interesting programmes to record. It’s much more convenient to use a keyboard and mouse to navigate, rather than a remote control. This is especially true here in the UK, where there are many Freeview channels that could contain content of interest.

Creating a pleasant API

However, if you’re looking to play around with integration, and prototype some ideas you have to help you navigate and interact in different ways, you’re not going to have an easy ride. There’s no single clean implementation of the functionality you can access in any one language. PHP drives the main MythWeb code, but it also mixes in some Perl to access some fairly major functionality (downloading/streaming the recorded programmes). There are also Python bindings within the MythTV codebase, but I’m not aware of anything substantial built on top of them, and what is implemented is mainly concerned with metadata.

The main functions I wanted a MythTV integration library to offer were the ability to list what had been recorded, obtain a thumbnail for that recording, and to be able to stream the data to you. This was achieved in the 0.1.0 release. Now we’re up to version 0.2.0, it now supports listing of EPG data, and creation/editing of recording schedules. This paves the way for experiments in automatic scheduling of recordings. There are many potentially interesting sources of TV reviews to scrape. For instance, if you like what The Guardian has to say about Television in its Watch this section, then extracting the titles from that page, and feeding them to ruby-mythtv is easy.

require 'ruby-mythtv'

# Connect to the server
mythbackend, mythdb = MythTV.connect(:host => 'mythtv.localdomain',
                                      :database_password => 'password')

# Find matches on our search term, and limit the results to 5 matches
programs = mythdb.list_programs(:conditions => ['title LIKE ?', "%Bruce Parry%"],
                                :limit => 5)

# Take the first program match, and convert it to a recording schedule
new_schedule = MythTV::RecordingSchedule.new(programs[0], mythdb)
new_schedule.save

# Signal the backend of recording changes for our recording schedule entry
mythbackend.reschedule_recordings(new_schedule.recordid)

# Let the backend resolve matches
sleep(1)

# Enumerate the list of pending recordings, find ours, and check for any conflicts
pending_recordings = mythbackend.query_pending
conflicts = pending_recordings.find { |p| p.recordid == new_schedule.recordid &&
                                          p.recstatus_sym == :rsConflict }

# If conflicts is empty, then all is good. If it is populated, then action needs
# to be taken, such as bumping the priority, or removing the clashes....

ruby-mythtv is present both on Rubyforge and Github. Major release versions will be present on Rubyforge, and development versions will be present on Github. This method allows me to push out code which I consider functional, but not necessarily finalised. Release early, release often, as the saying goes, and this is all the more relevant when forking projects and sharing ideas is central to the way Github works. There’s nothing worse than code which is “almost there”, languishing in your “I really should finish it” folder, destined never to see the light of day.

Nick Ludlam
Author
Nick Ludlam
Software Developer and Prototyper in Mixed Media