1 |
#!/usr/bin/perl |
2 |
# Copyright David Leadbeater 2007 <http://dgl.cx>. |
3 |
# Licensed under the same terms as Perl itself. |
4 |
|
5 |
# A simple example of using WWW::Facebook::API within a facebook canvas. |
6 |
# You will need to change the api_key, secret and app_path below to match your |
7 |
# account. To get an api_key and secret, go to |
8 |
# http://www.facebook.com/developers/editapp.php?new, choose "Use FBML" and |
9 |
# enter a unique name for the canvas which you should put into app_path. |
10 |
|
11 |
use strict; |
12 |
use CGI; |
13 |
use WWW::Facebook::API; |
14 |
use Data::Dump qw/dump/; |
15 |
use XML::FeedPP; |
16 |
use Time::HiRes qw/time/; |
17 |
|
18 |
our ($api_key,$secret,$rss_url,$app_path); # XXX configure this in config.pl |
19 |
require 'config.pl'; |
20 |
|
21 |
# UI elements |
22 |
|
23 |
sub dashboard { |
24 |
print qq| |
25 |
<fb:dashboard> |
26 |
<fb:action href=/$app_path/>Search</fb:action> |
27 |
<fb:action href=/$app_path/info>info</fb:action> |
28 |
</fb:dashboard> |
29 |
|; |
30 |
''; |
31 |
} |
32 |
|
33 |
sub form { |
34 |
my $q = shift; |
35 |
my $v = $q->param('search'); |
36 |
$v = qq| value="$v"| if defined $v; |
37 |
|
38 |
print qq| |
39 |
<form method="post" action="search"> |
40 |
<input name="search" $v> |
41 |
<input type="submit"> |
42 |
</form> |
43 |
|; |
44 |
''; |
45 |
} |
46 |
|
47 |
|
48 |
my $facebook = WWW::Facebook::API->new( |
49 |
api_key => $api_key, |
50 |
secret => $secret, |
51 |
app_path => $app_path, |
52 |
parse => 1, |
53 |
); |
54 |
|
55 |
our %action_map = ( |
56 |
'' => \&index_page, |
57 |
info => \&info_page, |
58 |
search => \&search_page, |
59 |
debug => \&debug_page, |
60 |
); |
61 |
|
62 |
sub main { |
63 |
|
64 |
# Should also work with FastCGI (via CGI::Fast). |
65 |
my $q = new CGI; |
66 |
print "Content-type: text/html; encoding=utf-8\r\n\r\n"; |
67 |
|
68 |
# redirect("Must be called in facebook canvas") |
69 |
# unless $facebook->canvas->in_fb_canvas($q); |
70 |
|
71 |
my $params = $facebook->canvas->validate_sig($q); |
72 |
if ( $params->{user} ) { |
73 |
|
74 |
# Canvas takes care of setting up a session for us, no need to call the |
75 |
# auth methods. |
76 |
$facebook->session_key( $params->{session_key} ); |
77 |
} |
78 |
else { |
79 |
|
80 |
# User hasn't added app (could reject/display info/whatever) |
81 |
# (Or handle later when a user is needed). |
82 |
} |
83 |
|
84 |
my ( $action, $param ) = ( $q->path_info =~ m!^/(\w+)/?(.*)! ); |
85 |
|
86 |
if ( my $s = $action_map{$action} ) { |
87 |
print dashboard; |
88 |
print qq|<div style='padding-left: 2em'>|; |
89 |
$s->( $q, $params ); |
90 |
print qq|</div>|; |
91 |
} |
92 |
else { |
93 |
div_error("Action unknown"); |
94 |
} |
95 |
|
96 |
# Clear session_key (for if running under FastCGI). |
97 |
$facebook->session_key(undef); |
98 |
} |
99 |
|
100 |
sub index_page { |
101 |
my ( $q, $params ) = @_; |
102 |
|
103 |
form($q); |
104 |
|
105 |
=for later |
106 |
|
107 |
# You could do this easier by using <fb:name>, just showing some API stuff here. |
108 |
my $name = "You"; |
109 |
if ($params) { |
110 |
my $res = |
111 |
$facebook->fql->query( query => |
112 |
"SELECT first_name FROM user WHERE uid=$params->{canvas_user}" ); |
113 |
$name = "Hi $res->[0]->{first_name}, you"; |
114 |
} |
115 |
print "$name ", ( $params ? "have" : "have't" ), |
116 |
" added this application. Some <a href='info'>info</a>."; |
117 |
if ( !$params ) { |
118 |
print "<a href='", $facebook->get_add_url, |
119 |
"'>Add this application</a>."; |
120 |
} |
121 |
|
122 |
=cut |
123 |
|
124 |
|
125 |
} |
126 |
|
127 |
sub info_page { |
128 |
open( my $fh, '<', 'info.html' ); |
129 |
print <$fh>; |
130 |
close($fh); |
131 |
} |
132 |
|
133 |
sub debug_page { |
134 |
my ( $q, $params ) = @_; |
135 |
print "<pre>"; |
136 |
print dump($params); |
137 |
print "</pre>"; |
138 |
} |
139 |
|
140 |
sub search_page { |
141 |
my ( $q, $params ) = @_; |
142 |
|
143 |
my $search = $q->param('search'); |
144 |
|
145 |
my $t = time(); |
146 |
|
147 |
my $feed = XML::FeedPP->new( $rss_url . $search ); |
148 |
|
149 |
my $t = time() - $t; |
150 |
|
151 |
print form($q), qq| |
152 |
<ol> |
153 |
|; |
154 |
|
155 |
foreach my $item ( $feed->get_item ) { |
156 |
print qq|<li><a href="|, $item->link, qq|">|, $item->title, qq|</a><br>|, $item->description; |
157 |
} |
158 |
|
159 |
print qq| |
160 |
</ol> |
161 |
|
162 |
<div style="color: #ccc;">search took $t sec</div> |
163 |
|; |
164 |
|
165 |
} |
166 |
|
167 |
sub redirect { |
168 |
print("Please go <a href='" |
169 |
. $facebook->get_app_url |
170 |
. "'>to facebook</a>" ); |
171 |
exit; |
172 |
} |
173 |
|
174 |
main(); |