FacebookOSD.pl

From MythTV Official Wiki
Jump to: navigation, search

FacebookOSD.pl is a perl script that allows you to display Facebook Friend Status Updates on your display.
If you're looking for the script that does Facebook and Twitter updates, it's called StatusOSD.pl.

Arguments

  • -email - Facebook Email Address
  • -password - Facebook Password
  • -dir - Directory to store working files

The first time the script is run it will not display any updates, but will set the last run date/time (to avoid displaying EVERY Friend Status Update). The next time the script is run it will display any Friends Status 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:

  • .facebookosd_lastrun - The last time the script was run
  • .facebookosd_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 ./facebookOSD.pl -email me\@mydomain.com -password mypassword -dir ~/
#!/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 vars qw( $opt_email $opt_password $opt_dir $opt_help $lastRun $updateText );
my @optl = ( "email=s", "password=s", "dir=s", "help" );

sub usage
{
    print "Usage: $0\n" .
          "       -email <facebook email address> -password <facebook password> -dir <temporary file location>\n";
    exit;
}

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

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} . ".facebookosd_lastrun") or $lastRun = $currentTime;
if ( !$lastRun ) {
    # Set last run to date/time in file if it exists
    $lastRun = <LOGFILE>;
    close( LOGFILE );
}
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 %postLoginData;
$postLoginData{'email'}=$opt_email;
$postLoginData{'pass'}=$opt_password;
$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 . ".facebookosd_update" );
            print UPDATE $item->{'title'};
            close ( UPDATE );
            my $command = "/usr/bin/osd_cat -w -p bottom -d 10 -l 1 -c white -O 5 -i 40 -f -adobe-helvetica-medium-r-*-*-30-*-*-*-*-*-*-* " . $opt_dir . ".facebookosd_update";
            system($command);
        }
    }
}

open( LASTRUN, ">" . $opt_dir . ".facebookosd_lastrun" );
print LASTRUN $currentTime;
close ( LASTRUN );

exit;