Programming in Facebook hussein suleman uct cs honours
Programming in Facebook hussein suleman uct cs honours 2007
What is Facebook?
How Facebook Apps work p Every profile page is made up of many independent portlets or profile boxes. p Each portlet is generated statically by a possibly external application and stored by Facebook. It could be an iframe, but in most cases is not. p p Portlets are not actually changed in the profile page – they are changed elsewhere! p Facebook has mechanisms to link in applications, and APIs for applications to communicate with Facebook.
Facebook Communication request for profile request for canvas content Facebook API use update profile box, etc. canvas user facebook. com application (UCT)
What can the API do Get a list of friend ids or friends who are running the same app p Get and set notifications of events p Set content of profile box p Get specific information on a user p Add to the Newsfeed for a user p etc. p See: http: //developers. facebook. com/documentation. php? v=1. 0&doc=
FBML Language used by Facebook to specify any fragment of Web pages. p Mostly just like HTML! p Forms get additional fields added to pass Facebook parameters silently. p Images are redirected to local copies. p Links must be absolute (parameters do not work as expected in all toolkits). p Lots of additional Facebook-specific items p n n <fb: name uid=…> for a user’s name <fb: profile-pic …> for a user’s profile picture
FQL p Facebook Query Language allows access to user database with language that is similar to SQL. p Example: n n SELECT first_name FROM user WHERE uid=$params->{user} To get the first name of a user based on the user-id
Creating an Application p What you need: n n Web Server API toolkit or roll-your-own p Add the Developer application to your profile. p Switch to the Developer application. p Apply for a key.
What does a typical application do? Check CGI parameters. p Authenticate using Facebook API. p Draw configuration Web page on canvas. p Allow user to update options … p Write FBML to profile box. p Save per-user state in database or in filesystem. p
Example: Profile Box
Example: Application Canvas
The Code – index. pl p p p #!/usr/bin/perl # # sample adapted from Facebook Perl API example # hussein suleman # 31 july 2007 p p use use use p p p p # create FB object with keys included my $facebook = WWW: : Facebook: : API->new( api_key => "8 a 3 b 63 fde 6 e 900 f 853 c 51903 e 6689 e 5 b", secret => "f 1818 d 69 d 7140 fa 0192 b 623 b 55 e 7 da 59", app_path => "hussein_testb", parse => 1, ); p p # remember to add proxy for UCT! $facebook->ua->proxy ('http', 'http: //cache. uct. ac. za: 8080/'); p p sub main { # handler CGI parameters my $q = new CGI; print $q->header; p p strict; CGI; WWW: : Facebook: : API; Data: : Dumper; LWP: : User. Agent; XML: : DOM; p p redirect("Must be called in facebook canvas") unless $facebook->canvas->in_fb_canvas($q); p p my $params = $facebook->canvas->validate_sig($q); if ( $params->{user} ) { # Canvas takes care of setting up a session for us, no need to call the p
p p p p # auth methods. $facebook->session_key( $params->{session_key} ); } else { # User hasn't added app (could reject/display info/whatever) # (Or handle later when a user is needed). } p my ( $action, $param ) = ( $q->path_info =~ m!^/(w+)/? (. *)! ); p # trigger action depending on parameters passed to app if ($action eq '') { handle_index($param, $params); } else { print_error ("cannot understand request"); } p p p } p # canvas drawing routine sub handle_index { my ( $param, $params ) = @_; p p print "<fb: header/>"; print "<h 1>Watch My Students</h 1>"; print "<fb: profile-pic uid="$params->{user}"/> "; # list data from disk file with links to delete entries print "<h 2><fb: name uid="$params->{user}" useyou="false"/>'s Current Wat ched Entries</h 2>"; my $fbml = "<b>Watch My Students</b><hr/><p>Sneak peek at what the 1016 stude nts are chatting about in the class forum. . . </p><ul>"; print "<ul>";
p p p p p p p p open (my $ofile, "data. txt"); my $po = 0; while (my $aline = <$ofile>) { $po++; chomp $aline; print "<li><a href="http: //banzai. cs. uct. ac. za/~hussein/cgi-bin/2007/face book/perl/delete. pl? position=$po">Delete</a> $aline</li>"; $fbml. = "<li>$aline</li>"; } close ($ofile); print "</ul>"; $fbml. = "</ul>"; print "<hr/>"; # list data from RSS feed with links to add entries print "<h 2><fb: name uid="$params->{user}" useyou="false"/>'s Latest RSS Entries</h 2>"; print "<ul>"; my $results = get_RSS(); $po = 0; foreach my $aresult (@$results) { $po++; $aresult->[2] =~ s/<[^>]+>//go; print "<li><a href="http: //banzai. cs. uct. ac. za/~hussein/cgi-bin/2007/face book/perl/add. pl? position=$po">$aresult->[0] (by $aresult->[1])</a><br/>$aresul t->[2]</li>"; } print "</ul>"; # add application if it has not been added if ( !$params ) { print "<a href='", $facebook->get_add_url,
p p "'>Add this application</a>. "; } p p # update profile box $facebook->profile->set_fbml (markup => $fbml); p } p # deal with users trying to use app without logging into FB sub redirect { div_error("Please go <a href='". $facebook->get_app_url. "'>to facebook</a>" ); exit; } p p p p p p # general error handler sub print_error { print "<div style='padding: 20 px'>", join( "", @_ ), "</div>"; } # retrieve and parse an RSS feed sub get_RSS { my $ua = new LWP: : User. Agent(); $ua->timeout(10); $ua->proxy ('http: //cache. uct. ac. za: 8080/'); my $response = $ua->get('http: //banzai. cs. uct. ac. za/php. BB 2 -1016/rss. php'); my @results = (); if ($response->is_success) { my $parser = new XML: : DOM: : Parser; my $doc = $parser->parse ($response->content); foreach my $anitem ($doc->get. Elements. By. Tag. Name ('item'))
p { p p p p p p p p } my $title = ''; foreach my $anode ($anitem->get. Elements. By. Tag. Name ('title')) { if ($anode->has. Child. Nodes == 1) { $title = $anode->get. First. Child->to. String; } } my $author = ''; foreach my $anode ($anitem->get. Elements. By. Tag. Name ('author')) { if ($anode->has. Child. Nodes == 1) { $author = $anode->get. First. Child->to. String; } } my $description = ''; foreach my $anode ($anitem->get. Elements. By. Tag. Name ('description')) { if ($anode->has. Child. Nodes == 1) { $description = $anode->get. First. Child->to. String; } } $description =~ s/< /</go; $description =~ s/> />/go; $description =~ s/[nr]//go; push (@results, [$title, $author, $description]); p } p @results; p } p main();
The Code - add. pl p p p #!/usr/bin/perl # # sample adapted from Facebook Perl API example # hussein suleman # 31 july 2007 use use strict; CGI; LWP: : User. Agent; XML: : DOM; sub main { my $q = new CGI; p handle_add ($q->param('position')); p print "Location: http: //apps. facebook. com/hussein_testb/nn"; p }
p p p p p p p p sub handle_add { my ( $position ) = @_; $position--; my $results = get_RSS(); my $aresult = $$results[$position]; if (defined $aresult) { $aresult->[2] =~ s/<[^>]+>//go; open (my $ofile, ">>data. txt"); print $ofile "<a href="$aresult->[1]"><b>$aresult->[0]</b></a> $aresult>[2]n"; close ($ofile); } } sub get_RSS { my $ua = new LWP: : User. Agent(); $ua->timeout(10); $ua->proxy ('http: //cache. uct. ac. za: 8080/'); my $response = $ua->get('http: //banzai. cs. uct. ac. za/php. BB 2 -1016/rss. php'); my @results = (); if ($response->is_success) { my $parser = new XML: : DOM: : Parser; my $doc = $parser->parse ($response->content); foreach my $anitem ($doc->get. Elements. By. Tag. Name ('item')) { my $title = ''; foreach my $anode ($anitem->get. Elements. By. Tag. Name ('title')) { if ($anode->has. Child. Nodes == 1)
p p p p p p p p foreach my $anode ($anitem->get. Elements. By. Tag. Name ('title')) { if ($anode->has. Child. Nodes == 1) { $title = $anode->get. First. Child->to. String; } } my $guid = ''; foreach my $anode ($anitem->get. Elements. By. Tag. Name ('guid')) { if ($anode->has. Child. Nodes == 1) { $guid = $anode->get. First. Child->to. String; } } my $description = ''; foreach my $anode ($anitem->get. Elements. By. Tag. Name ('description')) { if ($anode->has. Child. Nodes == 1) { $description = $anode->get. First. Child->to. String; } } $description =~ s/< /</go; $description =~ s/> />/go; $description =~ s/[nr]//go; push (@results, [$title, $guid, $description]); } } p p } @results;
The Code – delete. pl p p p p p #!/usr/bin/perl # # sample adapted from Facebook Perl API example # hussein suleman # 31 july 2007 use strict; use CGI; sub main { my $q = new CGI; handle_delete ($q->param('position')); p print "Location: http: //apps. facebook. com/hussein_testb/nn "; p p } p p sub handle_delete { my ( $position ) = @_; p p p open (my $ofile, "data. txt"); my @lines = <$ofile>; close ($ofile); p splice (@lines, $position-1, 1); p p p p } open (my $ofile, ">data. txt"); foreach my $aline (@lines) { print $ofile $aline; } close ($ofile); p main();
- Slides: 24