I am having a bit of fun with Facebook.
Last Friday I got a renewed interest in Facebook. Don't ask me why. I
don't know. I do know though that syndicating library content to
social networks (Facebook, MySpace, Delicious, etc.) seems to be a
rage. To that end I have taken a stab at writing a few Facebook
applications, and below is the simplest one shared here in the hopes
other (Perl) hackers don't spin their wheels as much as I did.
The purpose of the script is to output the results of the venerable
fortune command. Each time the program runs a new fortune will be
generated. (Actually, they are not fortunes as much as they are
interesting quotes.) The salient parts include:
1. constructing a Facebook object
2. determining the ID of the user
3. sending the output the user's profile page
4. redirecting the user's browser to their profile page
Here is the script for you reading pleasure:
#!/usr/bin/perl
# infomofortune - return a fortune to facebook
# Eric Lease Morgan <[log in to unmask]>
# January 6, 2008 - first cut; whew!
# require
use CGI;
use strict;
use WWW::Facebook::API;
# define
use constant FORTUNE => '/usr/bin/fortune';
# initialize
my $cgi = CGI->new;
my $fbml = '';
my $facebook = WWW::Facebook::API->new(
api_key => "foo",
secret => "bar",
app_path => "http://infomotions.com/facebook/"
);
# get the fortune
my $fortune = '';
open INPUT, FORTUNE . '|';
while ( <INPUT> ) { $fortune .= $_ }
close INPUT;
# build the fbml
$fbml .= $cgi->p({ style => 'font-size: small' }, $fortune );
# get the user id
my $uid = $facebook->canvas->get_user( $cgi );
# done
print $cgi->header;
print $facebook->profile->set_fbml( markup => $fbml, uid => $uid );
print $facebook->redirect( "http://www.facebook.com/profile.php?id=$uid
", $cgi );
exit;
It seems the key to writing cool Facebook applications is two-fold.
First, you need to know the API. It comes in many flavors and the
Facebook folks seem to endorse their PHP version. The Perl version
seems to work quite well. Second, it will be important to exploit the
social nature of Facebook and learn about the user and their friends.
The script above barely takes advantage of the API, and it does
nothing at all regarding social networking. My next steps will be to
write a interface to our library catalog that returns its output in
the Facebook interface. The next step will be to allow people to
create (MyLibrary) accounts and implement a recommender service based
on the user's and their friends' characteristics.
If you have a Facebook account, I think you can access and this and
the other two scripts I've written from the following URLs:
http://www.facebook.com/apps/application.php?id=8210775807
http://www.facebook.com/apps/application.php?id=8271966859
http://www.facebook.com/apps/application.php?id=8397580822
Enjoy?
--
Eric Lease Morgan
University Libraries of Notre Dame
|