PVR twittering recordings in 2 lines

I'm using MythTV for my TV recording system. Things can go wrong from time to time with such a self-built, on-the-edge system, causing some recordings to fail. That's why I like to keep notified about any finished recordings, especially while I'm on the road, when it really counts that recordings work. So I taught my media PC with two lines of code to send me a tweet for each finished recording, using Horde's excellent Twitter and OAuth libraries.

The full example will actually be a bit longer than two lines to be more readable, implement error handling, etc.

What we need are the Horde_Service_Twitter package for the actual Twitter communication, Horde_Autoloader for autoloading and Horde_Serializer (not really necessary) for error message decoding. Installing them is easy, the PEAR installer will pull in any dependencies like Horde_Oauth for authentication at the Twitter API, Horde_Http for HTTP communication etc:

pear channel-discover pear.horde.org
pear install horde/horde_service_twitter horde/horde_autoloader\
 horde/horde_serialize

The hardest part is actually creating a new application on the Twitter web site, though this just means to have Twitter generating some tokens that you can use to authenticate at the twitter API. Copy those tokens into some array with authentication settings:

$keys = array(
    'consumer_key'        => '',
    'consumer_secret'     => '',
    'access_token'        => '',
    'access_token_secret' => ''
);

Next, load the autoloader:

require 'Horde/Autoloader/Default.php';

Now comes the real stuff, i.e. the 2 lines that I promised earlier. Create a twitter client:

$twitter = Horde_Service_Twitter::create(array('oauth' => $keys));

That's when you can start using the twitter client to read timelines, post tweets, retweet, find followers and so on. For a full documentation of all possible actions, see the documentation of the Horde_Service_Twitter_Statuses ($twitter->statuses) and Horde_Service_Twitter_Account ($twitter->account) classes at http://dev.horde.org/api/framework/Service_Twitter/.
But for my purposes, I just want to tweet the last recording that has been passed by MythTV as a command line argument:

$twitter->statuses->update($argv[1]);

Done.

This is the full script, including some additional error handling:

#!/usr/bin/env php
<?php

/* Keys - these are obtained when registering for the service */
$keys = array(
    'consumer_key'        => '',
    'consumer_secret'     => '',
    'access_token'        => '',
    'access_token_secret' => ''
);

/* Enable autoloading. */
require 'Horde/Autoloader/Default.php';

/* Create the Twitter client */
$twitter = Horde_Service_Twitter::create(array('oauth' => $keys));

/* Send tweet */
try {
    $twitter->statuses->update($argv[1]);
} catch (Horde_Service_Twitter_Exception $e) {
    $error = Horde_Serialize::unserialize($e->getMessage(), Horde_Serialize::JSON);
    echo "$error->error\n";
    exit(1);
}

How to integrate this into MythTV is explained in the MythTV wiki, where you also find Twitter implementations in other languages.