FacebookOSD.pl

From MythTV Official Wiki
Revision as of 01:20, 15 December 2008 by Therealbrad (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

FacebookOSD.pl is a perl script that allows you to display Facebook Friend Status Updates on your display. 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 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

Save this script somewhere in your $PATH and run as a cron job every couple of minutes or so.

#!/usr/bin/perl -w
use strict;

use HTTP::Cookies;
use LWP::UserAgent;
use XML::RSS;
use Date::Manip;
use HTML::Entities;

my $email = "name\@domain.com"; #Change this to your facebook login email address
my $password = "password"; #Change this to your facebook password

#----  Nothing to change beyond this point ----#

my $lastRun;
open(LOGFILE, ".facebookosd_lastrun") or $lastRun = localtime();
$lastRun = <LOGFILE>;
my $lastRunInt = ParseDate($lastRun);
my $currentTime = localtime();
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 $updates;
my $updateText;

my %postLoginData;
$postLoginData{'email'}=$email;
$postLoginData{'pass'}=$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, '>.facebookosd_update' );
            print UPDATE $item->{'title'};
            close ( UPDATE );
            `osd_cat -p bottom -l 1 -c white -O 5 -i 40 -f -adobe-helvetica-medium-r-*-*-30-*-*-*-*-*-*-* .facebookosd_update`
        }
    }
}

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

exit;