Skip to content

Instantly share code, notes, and snippets.

@zmughal
Created July 26, 2012 04:50
Show Gist options
  • Save zmughal/3180308 to your computer and use it in GitHub Desktop.
Save zmughal/3180308 to your computer and use it in GitHub Desktop.
Use WWW::HtmlUnit to get Facebook ticker
#!/usr/bin/perl
use strict;
no warnings;# "recursion";
use WWW::HtmlUnit::Sweet;
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 $agent = WWW::HtmlUnit::Sweet->new(
version => 'FIREFOX_3',
url => 'http://www.facebook.com/'
);
$agent->setThrowExceptionOnScriptError(0);
#$agent->setJavaScriptEnabled(0);
#$agent->submit_form( with_fields => { email => $user, pass => $pass } );
my $forms = $agent->getForms();
my @forms = map { $forms->[$_] } 0..($forms->length-1);
#use DDP; p $_->getId() for @forms;
my $login = (grep { $_->getId() eq 'login_form' } @forms)[0];
#my $button = form.getInputByName("submitbutton");
my $button = $agent->getByXPath(q,//input[@type='submit'],)->[0];
my $user_field = $login->getInputByName("email");
$user_field->setValueAttribute($user);
my $pass_field = $login->getInputByName("pass");
$pass_field->setValueAttribute($pass);
my $feed_page = $button->click();
#$agent->setJavaScriptEnabled(1);
$agent->setJavaScriptTimeout(-1); # never timeout
use DDP; p $feed_page->getWebResponse()->getContentAsString();
$agent->wait_for( sub {
$agent->getByXPath(q,//div[@class='fbFeedTicker'],);
});
my $wait;
my $ticker = $agent->getByXPath(q,//div[@class='fbFeedTicker'],)->[0];
do {
$wait = $agent->waitForBackgroundJavaScript(1000);
print "waiting\n";
#use DDP; p $feed_page->asText();
use DDP; p $ticker->asText();
} while($wait > 0);
#      while (i > 0)        {            i =
# webClient.waitForBackgroundJavaScript(1000); 
#           if (i == 0)   
#         {                break;            }            synchronized
# (page)             {                System.out.println("wait");       
#         page.wait(500);            }        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment