Tweeting MythTV events in the new OAuth era

The mythtv wiki has a few scripts for tweeting MythTV stuff to twitter; although they all relied upon Basic Auth. until recently.

Taking a leaf out of Twitter Tools‘ book it’s possible to convert the LWP::UserAgent-based code to do OAuth.

Before:

#!/usr/bin/perl
use LWP::UserAgent;
my $output = shift;
my $browser = LWP::UserAgent->new;
my $url = 'http://twitter.com/statuses/update.json';
$browser->credentials('twitter.com:80', 'Twitter API', 'username', 'password');
$response = $browser->get("http://twitter.com/account/verify_credentials.json");
my $response = $browser->post($url, {status => $output});

First step is registering an App. at dev.twitter.com, so that you’ve got the necessary authentication keys to do OAuth directly with an access token.

See also: http://wphelpcenter.com/plugins/twitter-tools/

Secondly, you’ll need a means of performing OAuth in perl, I chose Net::Twitter:OAuth, the 0.02 version page‘s example forming the basis for the simplest twitter.pl upgrade.

After:

#!/usr/bin/perl
use Net::Twitter::OAuth;
my $client = Net::Twitter::OAuth->new(
consumer_key => "",
consumer_secret => "",
);
my $output = shift @ARGV;
# $client->oauth_token('', '');
# DEPRECATED: use access_token and access_token_secret instead at ./oauth-mtv-tweet.pl line X
$client->access_token('');
$client->access_token_secret('');
my $res = $client->update({ status => $output });

The more complex twitter.pl script can be upgraded in much the same way:

After:
#!/usr/bin/perl
#use LWP::UserAgent;
use Net::Twitter::OAuth;
my $client = Net::Twitter::OAuth->new(
consumer_key => "xxxxxxxxxxxxxxxxxx",
consumer_secret => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
);
$client->access_token('xxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$client->access_token_secret("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
#
use DBI;
use DBD::mysql;
use MythTV;
#
my $connect;
my $debug = 0;
my $title="";
my $subtitle="";
my $newsubtitle="";
my $starttime="";
my $chanid="";
#
##################################
# #
# Main code starts here !! #
# #
##################################
#
my $usage = "\nHow to use twitter.pl \n\ twitter.pl starttime=%STARTTIME% chanid=%CHANID% debug\n"
."\n%CHANID% = channel ID associated with the recording\n"
."%STARTTIME% = recording start time in either 'yyyy-mm-dd hh:mm:ss' or 'yyyymmddhhmmss' format\n"
."debug = enable debugging information - outputs which commands would be run etc\n";
#
# get this script's ARGS
#
#
my $num = @ARGV;
#
# if user hasn't passed enough arguments, die and print the usage info
#
if ($num le "1") {
die "$usage";
}
#
#
# Get all the arguments
#
#
foreach (@ARGV){
if (/debug/) {
$debug = 1;
}
elsif (/starttime/) {
$starttime = (split(/=/))[1];
}
elsif (/chanid/) {
$chanid = (split(/=/))[1];
}
}
#
# connect to backend
my $myth = MythTV->new;
# connect to database
my $connect = $myth->{dbh};
#
my $query = "SELECT name FROM channel WHERE chanid=$chanid";
my $query_handle = $connect->prepare($query);
$query_handle->execute or die "Unable to query channel table";
#
my ($channame) = $query_handle->fetchrow_array;
#
$query = "SELECT title, subtitle, endtime FROM recorded WHERE chanid=$chanid and starttime='$starttime'";
$query_handle = $connect->prepare($query);
$query_handle->execute or die "Unable to query settings table";
#
$query_handle->bind_columns(undef, \$title, \$subtitle, \$endtime);
$query_handle->fetch;
#
if ($subtitle)
{
$newsubtitle = " - ".$subtitle;
}
#
my $output = "Finished recording $title $newsubtitle from $channame at $endtime";
print "Chanid $chanid \n";
print "Starttime $starttime \n";
print "$output \n";
#
unless ($debug)
{
#
my $res = $client->update({ status => $output });
#
}

This entry was posted in MythTV, twitter, Web and tagged , , , , , . Bookmark the permalink.

Comments are closed.