| Revision 5 (by dpavlin, 2009/06/27 16:47:05) |
fixed credits and description
|
#!/usr/bin/perl
#
# Facebook appliaction which use Koha's RSS search results
# 2009-06-27 Dobrica Pavlinusic <dpavlin@rot13.org>
#
# Based on WWW::Facebook::API example
# Copyright David Leadbeater 2007 <http://dgl.cx>.
# Licensed under the same terms as Perl itself.
use strict;
use CGI;
use WWW::Facebook::API;
use Data::Dump qw/dump/;
use XML::FeedPP;
use Time::HiRes qw/time/;
our ($api_key,$secret,$rss_url,$app_path); # XXX configure this in config.pl
require 'config.pl';
# UI elements
my $facebook = WWW::Facebook::API->new(
api_key => $api_key,
secret => $secret,
app_path => $app_path,
parse => 1,
);
warn dump($facebook);
sub dashboard {
print qq|
<fb:dashboard>
<fb:action href=/$app_path/>Search</fb:action>
</fb:dashboard>
|;
'';
}
sub form {
my $q = shift;
my $v = $q->param('search');
$v = qq| value="$v"| if defined $v;
print qq|
<form method="post" action="search">
<input name="search" $v>
<input type="submit">
</form>
|;
'';
}
# dispatcher
our %action_map = (
'' => \&index_page,
info => \&info_page,
search => \&search_page,
debug => \&debug_page,
);
sub main {
# Should also work with FastCGI (via CGI::Fast).
my $q = new CGI;
print "Content-type: text/html; encoding=utf-8\r\n\r\n";
# redirect("Must be called in facebook canvas")
# unless $facebook->canvas->in_fb_canvas($q);
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
# 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).
}
my ( $action, $param ) = ( $q->path_info =~ m!^/(\w+)/?(.*)! );
print dashboard;
if ( my $s = $action_map{$action} ) {
print qq|<div style='padding-left: 2em'>|;
$s->( $q, $params );
print qq|</div>|;
}
else {
print "Action <tt>$action</tt> unknown";
warn "unknown action $action";
}
# Clear session_key (for if running under FastCGI).
$facebook->session_key(undef);
}
sub index_page {
my ( $q, $params ) = @_;
warn dump($params);
form($q);
# You could do this easier by using <fb:name>, just showing some API stuff here.
# http://wiki.developers.facebook.com/index.php/User_(FQL)
my $name = "You";
if ( my $uid = $params->{user} ) {
my $res = $facebook->fql->query( query => qq|
select
first_name, last_name,
is_app_user,
books,
locale
from user
where uid=$uid
| );
warn "# $uid res = ",dump(@{$res});
$name = "Hello $res->[0]->{first_name}, you";
}
print "$name ", ( $params ? "have" : "have't" ),
" added this application.";
if ( !$params ) {
print "<a href='", $facebook->get_add_url,
"'>Add this application</a>.";
}
}
sub info_page {
open( my $fh, '<', 'info.html' );
print <$fh>;
close($fh);
}
sub debug_page {
my ( $q, $params ) = @_;
print "<pre>";
print dump($params);
print "</pre>";
}
sub search_page {
my ( $q, $params ) = @_;
my $search = $q->param('search');
my $t = time();
my $feed = XML::FeedPP->new( $rss_url . $search );
my $t = time() - $t;
print form($q), qq|
<ol>
|;
foreach my $item ( $feed->get_item ) {
print qq|<li><a href="|, $item->link, qq|">|, $item->title, qq|</a><br>|, $item->description;
}
print qq|
</ol>
<div style="color: #ccc;">search took $t sec</div>
|;
}
sub redirect {
print("Please go <a href='"
. $facebook->get_app_url
. "'>to facebook</a>" );
exit;
}
main();