Difference between revisions of "FacebookOSD.pl"

From MythTV Official Wiki
Jump to: navigation, search
Line 1: Line 1:
 
'''FacebookOSD.pl''' is a perl script that allows you to display Facebook Friend Status Updates on your display.
 
'''FacebookOSD.pl''' is a perl script that allows you to display Facebook Friend Status Updates on your display.
 +
===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 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 location from wherever the script it run (or you can modify the script to put them elsewhere). You need read/write access to these files:
+
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_lastrun''' - The last time the script was run
 
*'''.facebookosd_update''' - Temporarily stores text to be displayed (always contains the last message displayed)
 
*'''.facebookosd_update''' - Temporarily stores text to be displayed (always contains the last message displayed)
Line 8: Line 13:
 
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
 
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.
+
I run this script as a cron job every 5 minutes.
  
 
<pre>
 
<pre>
Line 19: Line 24:
 
use Date::Manip;
 
use Date::Manip;
 
use HTML::Entities;
 
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 );
  
my $email = "name\@domain.com"; #Change this to your facebook login email address
+
if ( !$opt_dir ) {
my $password = "password"; #Change this to your facebook password
+
    # Default to current directory
 +
    $opt_dir = "./";
 +
}
 +
if ( $opt_dir !~ /\/$/ ) {
 +
    # Add trailing slash if it's not there
 +
    $opt_dir = $opt_dir . "/";
 +
}
  
#----  Nothing to change beyond this point ----#
+
my $currentTime = localtime();
  
my $lastRun;
+
open(LOGFILE, ${opt_dir} . ".facebookosd_lastrun") or $lastRun = $currentTime;
open(LOGFILE, ".facebookosd_lastrun") or $lastRun = localtime();
 
 
if ( !$lastRun ) {
 
if ( !$lastRun ) {
 +
    # Set last run to date/time in file if it exists
 
     $lastRun = <LOGFILE>;
 
     $lastRun = <LOGFILE>;
 
     close( LOGFILE );
 
     close( LOGFILE );
 
}
 
}
$lastRun = <LOGFILE>;
 
 
my $lastRunInt = ParseDate($lastRun);
 
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 $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;
 
my %postLoginData;
$postLoginData{'email'}=$email;
+
$postLoginData{'email'}=$opt_email;
$postLoginData{'pass'}=$password;
+
$postLoginData{'pass'}=$opt_password;
 
$postLoginData{'persistent'}=1;
 
$postLoginData{'persistent'}=1;
 
$postLoginData{'login'}='Login';
 
$postLoginData{'login'}='Login';
Line 58: Line 78:
 
     my $feedurl = decode_entities($1) if $response->content =~ /title=\"Friends.* Status Updates Feed\" href=\"(http:\/\/www.facebook.com\/feeds\/friends_status.php\?id=\d+&amp;key=.*&amp;format=rss20)\"\/>/;
 
     my $feedurl = decode_entities($1) if $response->content =~ /title=\"Friends.* Status Updates Feed\" href=\"(http:\/\/www.facebook.com\/feeds\/friends_status.php\?id=\d+&amp;key=.*&amp;format=rss20)\"\/>/;
 
     $response = $browser->get($feedurl,@header);
 
     $response = $browser->get($feedurl,@header);
     my $rss = XML::RSS->new();
+
     my $rss = XML::RSS->new();  
     $rss->parse( $response->content );
+
     $rss->parse( $response->content );  
 +
 
 
     foreach my $item (@{$rss->{'items'}}) {
 
     foreach my $item (@{$rss->{'items'}}) {
 
         if ( &Date_Cmp(ParseDate($item->{'pubDate'}), $lastRunInt) >= 0 ) {
 
         if ( &Date_Cmp(ParseDate($item->{'pubDate'}), $lastRunInt) >= 0 ) {
             open( UPDATE, '>.facebookosd_update' );
+
             open( UPDATE, ">" . $opt_dir . ".facebookosd_update" );
 
             print UPDATE $item->{'title'};
 
             print UPDATE $item->{'title'};
 
             close ( UPDATE );
 
             close ( UPDATE );
             `osd_cat -p bottom -l 1 -c white -O 5 -i 40 -f -adobe-helvetica-medium-r-*-*-30-*-*-*-*-*-*-* .facebookosd_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, '>.facebookosd_lastrun' );
+
open( LASTRUN, ">" . $opt_dir . ".facebookosd_lastrun" );
 
print LASTRUN $currentTime;
 
print LASTRUN $currentTime;
 
close ( LASTRUN );
 
close ( LASTRUN );

Revision as of 16:39, 15 December 2008

FacebookOSD.pl is a perl script that allows you to display Facebook Friend Status Updates on your display.

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.

#!/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;