Skip to content

Instantly share code, notes, and snippets.

@zmughal
Created February 16, 2012 13:46
Show Gist options
  • Save zmughal/1844924 to your computer and use it in GitHub Desktop.
Save zmughal/1844924 to your computer and use it in GitHub Desktop.
Use WWW::Scripter to print Facebook ticker to STDOUT.
#!/usr/bin/perl
use strict;
no warnings;# "recursion";
use WWW::Scripter;
use YAML qw/LoadFile/;
use File::HomeDir;
use File::Spec;
use HTML::TreeBuilder::XPath;
use HTML::FormatText;
use Digest::MD5 qw(md5_hex);
use Encode qw(encode_utf8 decode_utf8);
use Capture::Tiny ':all';
my $config = LoadFile(File::Spec->catfile( File::HomeDir->my_home, ".fbpc.rc"));
my $user = $config->{user} or die "no user in config";
my $pass = $config->{pass} or die "no password in config";
my $mech = WWW::Scripter->new();
$mech->use_plugin(JavaScript =>
engine => 'JE',
);
$mech->agent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en; rv:1.9.2.3) Gecko/20100401');
capture_stderr {
$mech->get("http://www.facebook.com/");
$mech->submit_form( with_fields => { email => $user, pass => $pass } );
};
print "@{[$mech->count_timers]}\n";
my $formatter = HTML::FormatText->new();
my %ticker_printed = ();
#print "Checking...";
my $timer_count = 0;
while(1) {
#print("a\n");
#print $mech->content;
my $tree = HTML::TreeBuilder::XPath->new_from_content(decode_utf8($mech->content));
#print $formatter->format($tree);
my @nodes = $tree->findnodes('//div[contains(@class,"fbFeedTickerStory")]');
#print "none found!" unless (@nodes);
#print "\n";
for my $node (@nodes) {
(my $text = decode_utf8($formatter->format($node))) =~ s/^\s+//gs;
(my $text_no_ws = $text) =~ s/\s//gs;
my $text_hash = md5_hex(encode_utf8($text_no_ws));
unless(exists $ticker_printed{$text_hash}) {
#print $node->as_XML, "\n";
binmode(STDOUT, ":utf8");
print $text;
print "----\n";
}
$ticker_printed{$text_hash} = 1;
}
sleep 4;
#print "Checking...";
capture_stderr {
if($timer_count < 5) {
$timer_count++;
$mech->check_timers;
} else {
$timer_count=0;
$mech->get("http://www.facebook.com/");
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment