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 |
my $facebook = WWW::Facebook::API->new( |
24 |
api_key => $api_key, |
25 |
secret => $secret, |
26 |
app_path => $app_path, |
27 |
parse => 1, |
28 |
); |
29 |
|
30 |
warn dump($facebook); |
31 |
|
32 |
sub dashboard { |
33 |
print qq| |
34 |
<fb:dashboard> |
35 |
<fb:action href=/$app_path/>Search</fb:action> |
36 |
</fb:dashboard> |
37 |
|; |
38 |
''; |
39 |
} |
40 |
|
41 |
sub form { |
42 |
my $q = shift; |
43 |
my $v = $q->param('search'); |
44 |
$v = qq| value="$v"| if defined $v; |
45 |
|
46 |
print qq| |
47 |
<form method="post" action="search"> |
48 |
<input name="search" $v> |
49 |
<input type="submit"> |
50 |
</form> |
51 |
|; |
52 |
''; |
53 |
} |
54 |
|
55 |
# dispatcher |
56 |
|
57 |
our %action_map = ( |
58 |
'' => \&index_page, |
59 |
info => \&info_page, |
60 |
search => \&search_page, |
61 |
debug => \&debug_page, |
62 |
); |
63 |
|
64 |
sub main { |
65 |
|
66 |
# Should also work with FastCGI (via CGI::Fast). |
67 |
my $q = new CGI; |
68 |
print "Content-type: text/html; encoding=utf-8\r\n\r\n"; |
69 |
|
70 |
# redirect("Must be called in facebook canvas") |
71 |
# unless $facebook->canvas->in_fb_canvas($q); |
72 |
|
73 |
my $params = $facebook->canvas->validate_sig($q); |
74 |
if ( $params->{user} ) { |
75 |
|
76 |
# Canvas takes care of setting up a session for us, no need to call the |
77 |
# auth methods. |
78 |
$facebook->session_key( $params->{session_key} ); |
79 |
} |
80 |
else { |
81 |
|
82 |
# User hasn't added app (could reject/display info/whatever) |
83 |
# (Or handle later when a user is needed). |
84 |
} |
85 |
|
86 |
my ( $action, $param ) = ( $q->path_info =~ m!^/(\w+)/?(.*)! ); |
87 |
|
88 |
print dashboard; |
89 |
|
90 |
if ( my $s = $action_map{$action} ) { |
91 |
print qq|<div style='padding-left: 2em'>|; |
92 |
$s->( $q, $params ); |
93 |
print qq|</div>|; |
94 |
} |
95 |
else { |
96 |
print "Action <tt>$action</tt> unknown"; |
97 |
warn "unknown action $action"; |
98 |
} |
99 |
|
100 |
# Clear session_key (for if running under FastCGI). |
101 |
$facebook->session_key(undef); |
102 |
} |
103 |
|
104 |
sub index_page { |
105 |
my ( $q, $params ) = @_; |
106 |
|
107 |
warn dump($params); |
108 |
|
109 |
form($q); |
110 |
|
111 |
# You could do this easier by using <fb:name>, just showing some API stuff here. |
112 |
# http://wiki.developers.facebook.com/index.php/User_(FQL) |
113 |
my $name = "You"; |
114 |
if ( my $uid = $params->{user} ) { |
115 |
my $res = $facebook->fql->query( query => qq| |
116 |
select |
117 |
first_name, last_name, |
118 |
is_app_user, |
119 |
books, |
120 |
locale |
121 |
from user |
122 |
where uid=$uid |
123 |
| ); |
124 |
warn "# $uid res = ",dump(@{$res}); |
125 |
$name = "Hello $res->[0]->{first_name}, you"; |
126 |
} |
127 |
print "$name ", ( $params ? "have" : "have't" ), |
128 |
" added this application."; |
129 |
|
130 |
if ( !$params ) { |
131 |
print "<a href='", $facebook->get_add_url, |
132 |
"'>Add this application</a>."; |
133 |
} |
134 |
|
135 |
|
136 |
|
137 |
} |
138 |
|
139 |
sub info_page { |
140 |
open( my $fh, '<', 'info.html' ); |
141 |
print <$fh>; |
142 |
close($fh); |
143 |
} |
144 |
|
145 |
sub debug_page { |
146 |
my ( $q, $params ) = @_; |
147 |
print "<pre>"; |
148 |
print dump($params); |
149 |
print "</pre>"; |
150 |
} |
151 |
|
152 |
sub search_page { |
153 |
my ( $q, $params ) = @_; |
154 |
|
155 |
my $search = $q->param('search'); |
156 |
|
157 |
my $t = time(); |
158 |
|
159 |
my $feed = XML::FeedPP->new( $rss_url . $search ); |
160 |
|
161 |
my $t = time() - $t; |
162 |
|
163 |
print form($q), qq| |
164 |
<ol> |
165 |
|; |
166 |
|
167 |
foreach my $item ( $feed->get_item ) { |
168 |
print qq|<li><a href="|, $item->link, qq|">|, $item->title, qq|</a><br>|, $item->description; |
169 |
} |
170 |
|
171 |
print qq| |
172 |
</ol> |
173 |
|
174 |
<div style="color: #ccc;">search took $t sec</div> |
175 |
|; |
176 |
|
177 |
} |
178 |
|
179 |
sub redirect { |
180 |
print("Please go <a href='" |
181 |
. $facebook->get_app_url |
182 |
. "'>to facebook</a>" ); |
183 |
exit; |
184 |
} |
185 |
|
186 |
main(); |