StatusOSD.pl

From MythTV Official Wiki
Jump to: navigation, search

Important.png Note: The correct title of this article is statusOSD.pl. It appears incorrectly here due to technical restrictions.

statusOSD.pl is a perl script that allows you to display Facebook Friend Status and/or Twitter updates on your display.

Arguments

  • -fblogin - Facebook Email Address
  • -fbpassword - Facebook Password
  • -twlogin - Twitter Account
  • -twpassword - Twitter Password
  • -dir - Directory to store working files
  • -help - Usage Message

To run just facebook updates, include only the facebook arguments. To run just Twitter updates include only the Twitter arguments. Script will end silently if you pass no / mismatched arguments.

The first time the script is run it will not display any facebook updates and the last few twitter updates, but will set the last run date/time and the last twitter update displayed. The next time the script is run it will display any updates that are new since the last time the script was run.


The script will write out two text files in the -dir location. You need read/write access to these files:

  • .statusosd_lastrun - The last time the script was run and last twitter update id displayed
  • .statusosd_update - Temporarily stores text to be displayed (always contains the last message displayed)

The script requires perl modules stated at the top of the script as well as XOSD which can be found here: http://www.ignavus.net/software.html

I run this script as a cron job every 5 minutes like this:

*/5 *   *   *   *    DISPLAY=:0 /usr/local/bin/statusOSD.pl -twlogin twittername -twpassword twitterpwd -fblogin name@domain.com -fbpassword facebookpwd -dir /home/mythtv
#!/usr/bin/perl -w
use strict;

use HTTP::Cookies;
use LWP::UserAgent;
use XML::RSS;
use Date::Manip;
use HTML::Entities;
use Getopt::Long;
use Net::Twitter;

use vars qw( $opt_fblogin $opt_fbpassword $opt_twlogin $opt_twpassword $opt_dir $opt_help $lastRun $twit_lastid $updateText );
my @optl = ( "fblogin=s", "fbpassword=s", "twlogin=s", "twpassword=s", "dir=s", "help" );
our $osdcat_bin = `which osd_cat`;
chomp($osdcat_bin);

sub usage
{
    print "Usage: $0\n" .
          "       -fblogin <facebook email address> -fbpassword <facebook password>\n" .
          "       -twlogin <twitter login>          -twpassword <twitter password>\n" .
          "       -dir <temporary file location>\n";
    print "ERROR: osd_cat cannot be found in your \$PATH. It is required for this script to run.\n" if ( !$osdcat_bin );
    exit;
}

usage() if ( !GetOptions( @optl ) || $opt_help || !$osdcat_bin );

if ( !$opt_dir ) {
    # Default to current directory
    $opt_dir = "./";
}
if ( $opt_dir !~ /\/$/ ) {
    # Add trailing slash if it's not there
    $opt_dir = $opt_dir . "/";
}

my $currentTime = localtime();

open(LOGFILE, ${opt_dir} . ".statusosd_lastrun") or $lastRun = $currentTime;
if ( !$lastRun ) {
    # Set last run to date/time in file if it exists
    $lastRun = <LOGFILE>;
    $twit_lastid = <LOGFILE>;
    close( LOGFILE );
}
chomp( $lastRun );
chomp( $twit_lastid ) if $twit_lastid;
my $lastRunInt = ParseDate($lastRun);
my $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6';
my $command = $osdcat_bin . " -w -p bottom -d 10 -l 1 -c white -O 5 -i 40 -f -adobe-helvetica-medium-r-*-*-30-*-*-*-*-*-*-* " . $opt_dir . ".statusosd_update";

if ( $opt_fblogin && $opt_fbpassword ) {
    my %postLoginData;
    $postLoginData{'email'}=$opt_fblogin;
    $postLoginData{'pass'}=$opt_fbpassword;
    $postLoginData{'persistent'}=1;
    $postLoginData{'login'}='Login';

    our $response;
    our @header = ('Referer'=>'http://www.facebook.com', 'User-Agent'=>$user_agent);
    our $cookie_jar = HTTP::Cookies->new(file=>'fbkCookies.dat',autosave=>1, ignore_discard=>1);
    our $browser = LWP::UserAgent->new; #init browser
    $browser->cookie_jar($cookie_jar);
    $browser->get('http://www.facebook.com/login.php',@header);
    $response = $browser->post('https://login.facebook.com/login.php',\%postLoginData,@header);

    if ( $response->content =~ /Incorrect Email/ ) {
        print "Login Failed...\n";
    } else {
        $response = $browser->get("http://www.facebook.com/friends/",@header);
        my $feedurl = decode_entities($1) if $response->content =~ /title=\"Friends.* Status Updates Feed\" href=\"(http:\/\/www.facebook.com\/feeds\/friends_status.php\?id=\d+&key=.*&format=rss20)\"\/>/;
        $response = $browser->get($feedurl,@header);
        my $rss = XML::RSS->new(); 
        $rss->parse( $response->content ); 
        foreach my $item (@{$rss->{'items'}}) {
            if ( &Date_Cmp(ParseDate($item->{'pubDate'}), $lastRunInt) >= 0 ) {
                open( UPDATE, ">" . $opt_dir . ".statusosd_update" );
                print UPDATE $item->{'title'};
                close( UPDATE );
                `${command}`;
            }
        }
    }
}

if ( $opt_twlogin && $opt_twpassword ) {
    my $twit = Net::Twitter->new(username=>$opt_twlogin, password=>$opt_twpassword );
    my $updates;
    if ( $twit_lastid ) {
        $updates = $twit->friends_timeline({'since_id' => $twit_lastid});
    } else {
        $updates = $twit->friends_timeline;
    }
    if ( $updates ) {
        @$updates = reverse(@$updates);
        foreach my $item (@{$updates}) {
            open( UPDATE, ">" . $opt_dir . ".statusosd_update" );
            print UPDATE $item->{'user'}{'name'} . ": " . $item->{'text'} . "\n";
            close( UPDATE );
            $twit_lastid = $item->{'id'};
            `${command}`; 
        }
    }
}
open( LASTRUN, ">" . $opt_dir . ".statusosd_lastrun" );
print LASTRUN $currentTime . "\n";
print LASTRUN $twit_lastid || "";
close ( LASTRUN );

exit;