Difference between revisions of "Twitter recording status"

From MythTV Official Wiki
Jump to: navigation, search
m (Add note about overuse of #mythTV hashtag in tweets)
Line 29: Line 29:
  
 
'''NOTE:''' Some people regard Twitter as a handy news resource and search on tags based on things they like to keep up on - say MythTV for example. Inserting #MythTV in your userjob twitter script makes all your recording tweets appear in the search feed meaning that people who genuinely want to follow real news items have to add the username your script uses to their filter.  As of 11th November 2009 the filter string is quite short but as time goes by it'll get longer.  '''PLEASE''' choose a different tag, or no tag at all. The majority of people wanting to follow #MythTV items are probably not interested in what you record or watch ;-)
 
'''NOTE:''' Some people regard Twitter as a handy news resource and search on tags based on things they like to keep up on - say MythTV for example. Inserting #MythTV in your userjob twitter script makes all your recording tweets appear in the search feed meaning that people who genuinely want to follow real news items have to add the username your script uses to their filter.  As of 11th November 2009 the filter string is quite short but as time goes by it'll get longer.  '''PLEASE''' choose a different tag, or no tag at all. The majority of people wanting to follow #MythTV items are probably not interested in what you record or watch ;-)
 +
 +
A more complete Twitter user job script is possible which tweets the actual channel name:  Save the script as twitter.pl, mark it as executable as before & set the user job to run as <pre> twitter.pl starttime=%STARTTIME% chanid=%CHANID%</pre>
 +
 +
 +
<pre>
 +
#!/usr/bin/perl
 +
use LWP::UserAgent;
 +
use DBI;
 +
use DBD::mysql;
 +
use MythTV;
 +
 +
$connect = undef;
 +
$debug = 0;
 +
$title="";
 +
$subtitle="";
 +
$newsubtitle="";
 +
$starttime="";
 +
$chanid="";
 +
 +
##################################
 +
#                                #
 +
#    Main code starts here !!    #
 +
#                                #
 +
##################################
 +
 +
$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
 +
#
 +
 +
$num = $#ARGV + 1;
 +
 +
# 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 ($_ =~ m/debug/) {
 +
        $debug = 1;
 +
    }
 +
    elsif ($_ =~ m/starttime/) {
 +
        $starttime = (split(/\=/,$_))[1];
 +
    }
 +
    elsif ($_ =~ m/chanid/) {
 +
        $chanid = (split(/\=/,$_))[1];
 +
    }
 +
}
 +
 +
# connect to backend
 +
my $myth = new MythTV();
 +
# connect to database
 +
$connect = $myth->{'dbh'};
 +
 +
$query = "SELECT name FROM channel WHERE chanid=$chanid";
 +
$query_handle = $connect->prepare($query);
 +
$query_handle->execute()  || 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()  || die "Unable to query settings table";
 +
 +
$query_handle->bind_columns(undef, \$title, \$subtitle, \$endtime);
 +
$query_handle->fetch();
 +
 +
if ($subtitle)
 +
{
 +
$newsubtitle = " - ".$subtitle;
 +
}
 +
 +
$output = "Finished recording $title $newsubtitle from $channame at $endtime";
 +
    print "Chanid $chanid \n";
 +
    print "Starttime $starttime \n";
 +
    print "$output \n";
 +
 +
if ($debug)
 +
{
 +
}
 +
else
 +
{
 +
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});
 +
}
 +
</pre>

Revision as of 13:55, 30 November 2009

It's fairly simple to set up a user job to send out recording status updates via Twitter. Paste the following code into a file (for example, twitter.pl)

#!/usr/bin/perl
use LWP::UserAgent;
my $output = shift @ARGV;
 
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});

Edit the username and password to match your twitter user and password. Save the file, make it executable, and put it somewhere in your path. In this example we'll put the file in /usr/bin.

chmod +x twitter.pl
cp twitter.pl /usr/bin/

Stop your backend and run mythtv-setup. In step 1, General, you must adjust two options. First, you must allow the new user job to be run on this backend. For example, if your new User Job is the first one, tick the "Allow User Job #1 on this backend." On the User Job setup page, give your job a name, such as "Post-record Twitter." Then you can use something like the following command line:

/usr/bin/twitter.pl "Finished recording %TITLE% (%SUBTITLE%) on %CHANID% at %ENDTIMEISO%.  Backend was %HOSTNAME%."

You can insert information as you wish, using any of the variables from User Jobs. Complete the setup and you will now have a user job which you can set to run at the end of individual or all recording rules. you can now edit your recording rules and set the user job to run at the end of each recording to update your twitter status.

NOTE: Some people regard Twitter as a handy news resource and search on tags based on things they like to keep up on - say MythTV for example. Inserting #MythTV in your userjob twitter script makes all your recording tweets appear in the search feed meaning that people who genuinely want to follow real news items have to add the username your script uses to their filter. As of 11th November 2009 the filter string is quite short but as time goes by it'll get longer. PLEASE choose a different tag, or no tag at all. The majority of people wanting to follow #MythTV items are probably not interested in what you record or watch ;-)

A more complete Twitter user job script is possible which tweets the actual channel name: Save the script as twitter.pl, mark it as executable as before & set the user job to run as
 twitter.pl starttime=%STARTTIME% chanid=%CHANID%


#!/usr/bin/perl
use LWP::UserAgent;
use DBI;
use DBD::mysql;
use MythTV;

$connect = undef;
$debug = 0;
$title="";
$subtitle="";
$newsubtitle="";
$starttime="";
$chanid="";

##################################
#                                #
#    Main code starts here !!    #
#                                #
##################################

$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
#

$num = $#ARGV + 1;

# 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 ($_ =~ m/debug/) {
        $debug = 1;
    }
    elsif ($_ =~ m/starttime/) {
        $starttime = (split(/\=/,$_))[1];
    }
    elsif ($_ =~ m/chanid/) {
        $chanid = (split(/\=/,$_))[1];
    }
}

# connect to backend
my $myth = new MythTV();
# connect to database
$connect = $myth->{'dbh'};

$query = "SELECT name FROM channel WHERE chanid=$chanid";
$query_handle = $connect->prepare($query);
$query_handle->execute()  || 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()  || die "Unable to query settings table";

$query_handle->bind_columns(undef, \$title, \$subtitle, \$endtime);
$query_handle->fetch();

if ($subtitle)
{
$newsubtitle = " - ".$subtitle;
}

$output = "Finished recording $title $newsubtitle from $channame at $endtime";
    print "Chanid $chanid \n";
    print "Starttime $starttime \n";
    print "$output \n";

if ($debug)
{
}
else
{
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});
}