1 |
#!/usr/bin/perl |
#!/usr/bin/perl |
2 |
|
# |
3 |
|
# Facebook appliaction which use Koha's RSS search results |
4 |
|
# 2009-06-27 Dobrica Pavlinusic <dpavlin@rot13.org> |
5 |
|
# |
6 |
|
# Based on WWW::Facebook::API example |
7 |
# Copyright David Leadbeater 2007 <http://dgl.cx>. |
# Copyright David Leadbeater 2007 <http://dgl.cx>. |
8 |
# Licensed under the same terms as Perl itself. |
# Licensed under the same terms as Perl itself. |
9 |
|
|
|
# A simple example of using WWW::Facebook::API within a facebook canvas. |
|
|
# You will need to change the api_key, secret and app_path below to match your |
|
|
# account. To get an api_key and secret, go to |
|
|
# http://www.facebook.com/developers/editapp.php?new, choose "Use FBML" and |
|
|
# enter a unique name for the canvas which you should put into app_path. |
|
|
|
|
10 |
use strict; |
use strict; |
11 |
use CGI; |
use CGI; |
12 |
use WWW::Facebook::API; |
use WWW::Facebook::API; |
13 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
14 |
use XML::FeedPP; |
use XML::FeedPP; |
15 |
|
use Time::HiRes qw/time/; |
16 |
|
|
17 |
our ($api_key,$secret,$rss_url); # XXX configure this in config.pl |
our ($api_key,$secret,$rss_url,$app_path); # XXX configure this in config.pl |
18 |
require 'config.pl'; |
require 'config.pl'; |
19 |
|
|
20 |
|
# UI elements |
21 |
|
|
22 |
my $facebook = WWW::Facebook::API->new( |
my $facebook = WWW::Facebook::API->new( |
23 |
api_key => $api_key, |
api_key => $api_key, |
24 |
secret => $secret, |
secret => $secret, |
25 |
app_path => "ffzg-koha", |
app_path => $app_path, |
26 |
parse => 1, |
parse => 1, |
27 |
); |
); |
28 |
|
|
29 |
|
warn dump($facebook); |
30 |
|
|
31 |
|
sub dashboard { |
32 |
|
print qq| |
33 |
|
<fb:dashboard> |
34 |
|
<fb:action href=/$app_path/>Search</fb:action> |
35 |
|
</fb:dashboard> |
36 |
|
|; |
37 |
|
''; |
38 |
|
} |
39 |
|
|
40 |
|
sub form { |
41 |
|
my $q = shift; |
42 |
|
my $v = $q->param('search'); |
43 |
|
$v = qq| value="$v"| if defined $v; |
44 |
|
|
45 |
|
print qq| |
46 |
|
<form method="post" action="search"> |
47 |
|
<input name="search" $v> |
48 |
|
<input type="submit"> |
49 |
|
</form> |
50 |
|
|; |
51 |
|
''; |
52 |
|
} |
53 |
|
|
54 |
|
# dispatcher |
55 |
|
|
56 |
our %action_map = ( |
our %action_map = ( |
57 |
'' => \&index_page, |
'' => \&index_page, |
58 |
info => \&info_page, |
info => \&info_page, |
59 |
search => \&search_page, |
search => \&search_page, |
60 |
|
debug => \&debug_page, |
61 |
); |
); |
62 |
|
|
63 |
sub main { |
sub main { |
83 |
} |
} |
84 |
|
|
85 |
my ( $action, $param ) = ( $q->path_info =~ m!^/(\w+)/?(.*)! ); |
my ( $action, $param ) = ( $q->path_info =~ m!^/(\w+)/?(.*)! ); |
86 |
|
|
87 |
|
print dashboard; |
88 |
|
|
89 |
if ( my $s = $action_map{$action} ) { |
if ( my $s = $action_map{$action} ) { |
90 |
|
print qq|<div style='padding-left: 2em'>|; |
91 |
$s->( $q, $params ); |
$s->( $q, $params ); |
92 |
|
print qq|</div>|; |
93 |
} |
} |
94 |
else { |
else { |
95 |
div_error("Action unknown"); |
print "Action <tt>$action</tt> unknown"; |
96 |
|
warn "unknown action $action"; |
97 |
} |
} |
98 |
|
|
99 |
# Clear session_key (for if running under FastCGI). |
# Clear session_key (for if running under FastCGI). |
101 |
} |
} |
102 |
|
|
103 |
sub index_page { |
sub index_page { |
104 |
my ( $q, $params ) = @_; |
my ( $q, $params ) = @_; |
105 |
|
|
106 |
print "<fb:header/><div style='padding: 20px'>"; |
warn dump($params); |
107 |
|
|
108 |
|
form($q); |
109 |
|
|
110 |
# You could do this easier by using <fb:name>, just showing some API stuff here. |
# You could do this easier by using <fb:name>, just showing some API stuff here. |
111 |
|
# http://wiki.developers.facebook.com/index.php/User_(FQL) |
112 |
my $name = "You"; |
my $name = "You"; |
113 |
if ($params) { |
if ( my $uid = $params->{user} ) { |
114 |
my $res = |
my $res = $facebook->fql->query( query => qq| |
115 |
$facebook->fql->query( query => |
select |
116 |
"SELECT first_name FROM user WHERE uid=$params->{canvas_user}" ); |
first_name, last_name, |
117 |
$name = "Hi $res->[0]->{first_name}, you"; |
is_app_user, |
118 |
|
books, |
119 |
|
locale |
120 |
|
from user |
121 |
|
where uid=$uid |
122 |
|
| ); |
123 |
|
warn "# $uid res = ",dump(@{$res}); |
124 |
|
$name = "Hello $res->[0]->{first_name}, you"; |
125 |
} |
} |
126 |
print "$name ", ( $params ? "have" : "have't" ), |
print "$name ", ( $params ? "have" : "have't" ), |
127 |
" added this application. Some <a href='info'>info</a>."; |
" added this application."; |
128 |
|
|
129 |
if ( !$params ) { |
if ( !$params ) { |
130 |
print "<a href='", $facebook->get_add_url, |
print "<a href='", $facebook->get_add_url, |
131 |
"'>Add this application</a>."; |
"'>Add this application</a>."; |
132 |
} |
} |
|
print "</div>"; |
|
|
|
|
|
print qq{ |
|
|
|
|
|
<form method="post" action="search"> |
|
|
<input name="search"> |
|
|
<input type="submit"> |
|
|
</form> |
|
133 |
|
|
|
}; |
|
134 |
|
|
135 |
|
|
136 |
} |
} |
137 |
|
|
138 |
sub info_page { |
sub info_page { |
139 |
my ( $q, $params ) = @_; |
open( my $fh, '<', 'info.html' ); |
140 |
|
print <$fh>; |
141 |
|
close($fh); |
142 |
|
} |
143 |
|
|
144 |
print "<fb:header/><div style='padding: 20px'><pre>"; |
sub debug_page { |
145 |
|
my ( $q, $params ) = @_; |
146 |
|
print "<pre>"; |
147 |
print dump($params); |
print dump($params); |
148 |
print "</pre></div>"; |
print "</pre>"; |
149 |
} |
} |
150 |
|
|
151 |
sub search_page { |
sub search_page { |
152 |
my ( $q, $params ) = @_; |
my ( $q, $params ) = @_; |
153 |
|
|
154 |
my $search = $q->param('search'); |
my $search = $q->param('search'); |
155 |
|
|
156 |
|
my $t = time(); |
157 |
|
|
158 |
|
my $feed = XML::FeedPP->new( $rss_url . $search ); |
159 |
|
|
160 |
|
my $t = time() - $t; |
161 |
|
|
162 |
print qq| |
print form($q), qq| |
|
<fb:header/> |
|
|
<div style='padding: 20px'> |
|
|
Search results for <em>$search</em> |
|
163 |
<ol> |
<ol> |
164 |
|; |
|; |
165 |
|
|
|
my $feed = XML::FeedPP->new( $rss_url . $search ); |
|
|
|
|
166 |
foreach my $item ( $feed->get_item ) { |
foreach my $item ( $feed->get_item ) { |
167 |
print qq|<li><a href="|, $item->link, qq|">|, $item->title, qq|</a><br>|, $item->description; |
print qq|<li><a href="|, $item->link, qq|">|, $item->title, qq|</a><br>|, $item->description; |
168 |
} |
} |
169 |
|
|
170 |
print qq| |
print qq| |
171 |
</ol> |
</ol> |
172 |
</div> |
|
173 |
|
<div style="color: #ccc;">search took $t sec</div> |
174 |
|; |
|; |
175 |
|
|
176 |
} |
} |
177 |
|
|
178 |
sub redirect { |
sub redirect { |
179 |
div_error("Please go <a href='" |
print("Please go <a href='" |
180 |
. $facebook->get_app_url |
. $facebook->get_app_url |
181 |
. "'>to facebook</a>" ); |
. "'>to facebook</a>" ); |
182 |
exit; |
exit; |
183 |
} |
} |
184 |
|
|
|
sub div_error { |
|
|
print "<div style='padding: 20px'>", join( "", @_ ), "</div>"; |
|
|
} |
|
|
|
|
185 |
main(); |
main(); |