Jump to content

How to measure Facebook activity with the Facebook API and Perl

0
  sarahkim's Photo
Posted Jun 09 2010 11:37 AM

Here's a simple Perl script that uses the Facebook API to get Facebook activity stats for a webpage. We are using it to output a table that shows us how many people are sharing, liking, commenting, and clicking on O'Reilly Answers pages.

The output looks like this:

Attached Image

Perl Script

Note: You'll need to pass the script the URL you are interested in. Or you can modify the script to process a list of URLs. Or you can write a query that outputs a list of URLs and calls this script for each one, which is how we are using it.

use XML::Simple;
use Data::Dumper;

# create object
$xml = new XML::Simple;

#get url
my $url = $ARGS{url} or return; #required
my $t = $ARGS{title} or ""; #optional

# read XML file
my $file = "http://api.facebook.com/restserver.php?method=links.getStats&urls=" . $url;

my $content = get($file);
my $data = $xml->XMLin($content);

if ($data->{link_stat}->{total_count} != 0) {

   $count++;

   print "<tr style='background-color:#fff;'>";
   if ($t ne "") {
      print "<td style='padding:5px;'><a href='" . $url . "'>" . $t . "</a></td>";
   } else {
      print "<td style='padding:5px;'><a href='" . $url . "'>" . $url . "</a></td>";
   }
   print "<td style='padding:5px;text-align:center;'>" . $data->{link_stat}->{share_count} . "</td>";
   print "<td style='padding:5px;text-align:center;'>" . $data->{link_stat}->{like_count} . "</td>";
   print "<td style='padding:5px;text-align:center;'>" . $data->{link_stat}->{comment_count};
   print "<td style='padding:5px;text-align:center;'>" . $data->{link_stat}->{click_count};
   print "<td style='padding:5px;text-align:center;'>" . $data->{link_stat}->{total_count};
   print "</tr>";

   $share_count += $data->{link_stat}->{share_count};
   $like_count += $data->{link_stat}->{like_count};
   $comment_count += $data->{link_stat}->{comment_count};
   $click_count += $data->{link_stat}->{click_count};
   $total_count += $data->{link_stat}->{total_count};

}



We have a separate script that runs the query to grab the list of URLs and outputs the header and footer. Here is a snippet of that script:

# header: set counts to zero and print beginning of table
$count = 0;
$share_count = 0;
$like_count = 0;
$comment_count = 0;
$click_count = 0;
$total_count = 0;

print <<HEADER;
<h1 id="topic">Facebook Stats for O'Reilly Answers</h1>
<br />

<table cellspacing="0" cellpadding="5" border="1">
<tr style="background-color:#aaa;">
<th style="padding:5px;">Post</th>
<th style="padding:5px;"><img src="/images/icons/FBshare.png" alt="Share" /></th>
<th style="padding:5px;"><img src="/images/icons/FBlike.png" alt="Like" /></th>
<th style="padding:5px;"><img src="/images/icons/FBcomment.png" alt="Comment" /></th>
<th style="padding:5px;"><img src="/images/icons/FBclick.png" alt="Click" /></th>
<th style="padding:5px;"><img src="/images/icons/FBtotal.png" alt="Total" /></th>
</tr>
HEADER

#...

# Query for the list of URLs and call above script for each one

#...

# Footer: Print total counts
print "<tr style='font-weight:bold;'>";
print "<td style='padding: 5px;'>Totals: " . $count . " Links</td>";
print "<td style='padding: 5px; text-align: center;'>" . $share_count . "</td>";
print "<td style='padding: 5px; text-align: center;'>" . $like_count . "</td>";
print "<td style='padding: 5px; text-align: center;'>" . $comment_count . "</td>";
print "<td style='padding: 5px; text-align: center;'>" . $click_count . "</td>";
print "<td style='padding: 5px; text-align: center;'>" . $total_count . "</td>";
print "</tr>";



Tags:
2 Subscribe


3 Replies

 : Jun 11 2010 01:52 PM
I thought, reading the title that you would be using WWW::Facebook::API, not a handrolled thing like that.
 : Jun 11 2010 02:15 PM
I don't see the Facebook getStats method available as part of that Perl module. Am I missing something? In any case, I'd love to see other answers posted that show examples of how to use WWW::Facebook::API. Have you used it?

Cheers,
Sarah
0
  monkeyvegas's Photo
Posted Jun 14 2010 09:59 PM

I have used it, but not for what the example is doing, I am not sure it is possible at the moment with the module, and I wasn't expecting a script measuring what you were measuring from the title. A few other things, wouldn't it be better to use a Template module like the Template Toolkit instead of hard-coded HTML? Also, your code highlighter breaks on the ' in O'Reilly.