blib/lib/WWW/Facebook/FQL.pm

filter: Statementstmt 56.0% 65 / 116 Branchbran 10.0% 3 / 30 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 66.6% 16 / 24 Podpod 70.0% 7 / 10 totaltotal 47.6% 93 / 195 CC 24 SCAR 22.7
CC
24
Cov
45%
login
42.0
query
12.0
new
11.9
CRAP 9.6
↑ Summary
1 package WWW::Facebook::FQL;
2  
3 =head1 NAME
4  
5 WWW::Facebook::FQL - Simple interface to Facebook's FQL query language
6  
7 =head1 SYNOPSIS
8  
9 use WWW::Facebook::FQL;
10  
11 ## Connect and log in:
12 my $fb = new WWW::Facebook::FQL key => $public_key, private => $private_key;
13 $fb->login($email, $password);
14  
15 ## Get your own name and pic back:
16 $fb->query("SELECT name, pic FROM user WHERE uid=$fb->{uid}");
17  
18 ## Get your friends' names and pics:
19 $fb->query("SELECT name, pic FROM user WHERE uid IN "
20 . "(SELECT uid2 FROM friend WHERE uid1 = $fb->{uid})");
21  
22 ## Get results in manageable form:
23 use JSON::Syck; # or whatever...
24 $fb->format = 'JSON';
25 my $arrayref = JSON::Syck::Load $fb->query("...");
26  
27 =head1 DESCRIPTION
28  
29 WWW::Facebook::FQL aims to make it easy to perform Facebook Query
30 Language (FQL) queries from a Perl program, rather than to reflect the
31 whole PHP Facebook API. For those comfortable with SQL, this may be a
32 more comfortable interface. Results are currently returned in the raw
33 JSON or XML format, but more palatable options may be available in the
34 future.
35  
36 =cut
37  
38 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 require Exporter;
42 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 1 ▶use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 $VERSION = '0.03';
46  
47 1 @EXPORT_OK = qw(%FIELDS %IXFIELDS);
48 1 %EXPORT_TAGS = (all => \@EXPORT_OK);
49 1 @ISA = qw(Exporter);
50  
51 1 ▶use vars qw($rest %FIELDS %IXFIELDS);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 $rest = 'http://api.facebook.com/restserver.php';
53  
54 sub dprint
55 1 ▶{
Subroutine dprint: called CC=2 SCAR=7.6
Pod undocumented
56 1 my $self = shift;
57 1 my $lev = shift;
58 1 ▶ if ($lev <= $self->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lev <= $self->{"verbose"}) 0 1 1
59 0 print STDERR @_;
60 }
61 }
62  
63 sub _sig
64 1 ▶{
Subroutine _sig: called CC=1 SCAR=0.0
65 1 my $secret = shift;
66 1 md5_hex uri_unescape(join '', sort(@_), $secret);
67 }
68  
69 sub get
70 1 ▶{
Subroutine get: called CC=2 SCAR=8.1
Pod undocumented
71 1 my $self = shift;
72 1 ▶ ($self->{mech} ||= new WWW::Mechanize)->get(@_);
Condition33%1/3
$self->{"mech"} ||= "WWW::Mechanize"->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"mech"} ||= "WWW::Mechanize"->new
$self->{"mech"} "WWW::Mechanize"->new
Truth table33%1/3
A ||= B
A= $self->{"mech"}B= "WWW::Mechanize"->new
ABresult
1X1
011
000
73 }
74  
75 sub _request_nofail
76 1 ▶{
Subroutine _request_nofail: called CC=2 SCAR=11.6
77 1 my $self = shift;
78 1 my $resp = $self->_request(@_);
79 0 ▶ die "Request failed:\n", $resp->decoded_content unless $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
80 0 $resp;
81 }
82  
83 sub _request
84 1 ▶{
Subroutine _request: called CC=5 SCAR=21.5
85 1 my ($self, $method, %o) = @_;
86 1 ▶ $o{format} ||= $self->{format};
Condition33%1/3
$o{"format"} ||= $self->{"format"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$o{"format"} ||= $self->{"format"}
$o{"format"} $self->{"format"}
Truth table33%1/3
A ||= B
A= $o{"format"}B= $self->{"format"}
ABresult
1X1
011
000
87 1 $method = "facebook.$method";
88 my @params = ("api_key=$self->{key}",
89 "method=$method",
90 'v=1.0',
91 $self->{session_key} ? ('session_key='.$self->{session_key},
92 'call_id='.(++$self->{callid})) : (),
93 1 ▶ map { "$_=".uri_escape($o{$_}) } keys %o);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'session_key'} ? : 0 1 1
94 1 my $sig = _sig($self->{secret}, @params);
95 1 my $url = "$rest?".join '&', @params, "sig=$sig\n";
96 1 $self->dprint(1, $url);
97 1 my $resp = $self->get("$rest?".join '&', @params, "sig=$sig");
98 0 ▶ if (!$resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
99 0 $self->dprint(0, "Request '$url' failed.\n");
100 }
101 ## avoid decoding content unless printed
102 0 ▶ if ($self->{verbose} > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"} > 2) 0 0 0
103 0 $self->dprint(2, "RESPONSE ", '=' x 50, "\n", $resp->decoded_content,
104 "\n", '=' x 70, "\n");
105 }
106 0 $resp;
107 }
108  
109 sub _get_auth_token
110 1 ▶{
Subroutine _get_auth_token: called CC=1 SCAR=0.2
111 1 my ($self) = @_;
112 1 $self->{secret} = $self->{private};
113 1 my $resp = $self->_request_nofail('auth.createToken', format => 'JSON');
114 0 $self->{auth_token} = eval $resp->decoded_content;
115 }
116  
117 sub _get_session
118 0 ▶{
Subroutine _get_session: not called CC=2 SCAR=17.9
119 0 my $self = shift;
120 0 my $resp;
121 {
122 0 local $rest = $rest;
123 0 $rest =~ s/^http/https/;
124 $resp = $self->_request_nofail('auth.getSession', format => 'XML',
125 0 auth_token => $self->{auth_token});
126 }
127 0 local $_ = $resp->decoded_content;
128 0 for my $word (qw(uid session_key expires secret)) {
129 0 ($self->{$word}) = /<$word>(.*?)<\/$word>/;
130 }
131 $self->dprint(1, "Session expires at ",
132 0 scalar localtime($self->{expires}), "\n");
133 }
134  
135 =head2 C<$fb = new WWW::Facebook::FQL key =E<gt> value, ...>
136  
137 Create a new Facebook FQL session for user $EMAIL with password $PASS.
138 Keyword arguments include
139  
140 =over 4
141  
142 =item email -- the email address of your Facebook account.
143  
144 =item pass -- your password.
145  
146 =item verbose -- A number controlling debugging information.
147  
148 =item key -- The public part of your API key.
149  
150 You need to sign up for this on Facebook by joining the "Developers"
151 group and requesting an API key.
152  
153 =item private -- The private part of your API key.
154  
155 =item format -- Data return format, either 'XML' (the default) or 'JSON'.
156  
157 =back
158  
159 WWW::Facebook::FQL reads default values from the file $HOME/.fqlrc if
160 it exists. It should contain the innards of an argument list, and
161 will be evaluated like C<@args = eval "($FILE_CONTENTS)">. The
162 constructor will I<not> prompt for any parameters; it is the calling
163 program's responsibility to get sensitive information from the user in
164 an appropriate way.
165  
166 =cut
167  
168 sub new
169 1 ▶{
Subroutine new: called CC=5 SCAR=24.7
Pod documented
170 1 my $class = shift;
171 1 my @def = (format => 'XML', verbose => 0);
172 1 ▶ if (-f "$ENV{HOME}/.fqlrc") {
Branch: 1 branch
BranchTrueFalseTotal
if (-f qq[$ENV{"HOME"}/.fqlrc]) 0 1 1
173 0 local $/;
174 0 ▶ if (open IN, "$ENV{HOME}/.fqlrc") {
Branch: 1 branch
BranchTrueFalseTotal
if (open IN, qq[$ENV{"HOME"}/.fqlrc]) 0 0 0
175 0 my @tmp = eval '('.<IN>.')';
176 0 ▶ push @def, @tmp unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
177 0 close IN;
178 }
179 }
180 1 my %o = (@def, @_);
181 1 my $self = bless \%o, $class;
182 1 ▶ return undef unless $self->_get_auth_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_get_auth_token 0 0 0
183 0 $self
184 }
185  
186 sub login
187 0 ▶{
Subroutine login: not called CC=6 SCAR=37.4
Pod undocumented
188 0 my $self = shift;
189 0 ▶ ($self->{email}, $self->{pass}) = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
190 0 my $mech = $self->{mech};
191 0 $mech->get("http://www.facebook.com/login.php?api_key=$self->{key}&v=1.0&auth_token=$self->{auth_token}&hide_checkbox=1&skipcookie=1");
192 0 ▶ die "Can't access login form:\n", $mech->res->decoded_content
Branch: 1 branch
BranchTrueFalseTotal
unless $mech->success 0 0 0
193 unless $mech->success;
194  
195 my $resp = $mech->submit_form(with_fields => {
196 email => $self->{email},
197 pass => $self->{pass}
198 0 });
199 0 ▶ die "Login failed:\n", $resp->decoded_content
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
200 unless $resp->is_success;
201 0 $self->dprint(2, "Logged in as $self->{email}\n");
202 ## XXX check response
203 0 ▶ if ($mech->content =~ /Terms of Service/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->content =~ /Terms of Service/) 0 0 0
204 0 $mech->submit_form(form_name => 'confirm_grant_form');
205 0 ▶ die "TOS failed:\n", $mech->res->decoded_content
Branch: 1 branch
BranchTrueFalseTotal
unless $mech->res->is_success 0 0 0
206 unless $mech->res->is_success;
207 0 $self->dprint(2, "Agreed to terms of service.");
208 }
209 ## Get session key
210 0 $self->_get_session;
211 0 $self;
212 }
213  
214 =head2 C<$fb-E<gt>logout>
215  
216 Log the current user out.
217  
218 =cut
219  
220 sub logout
221 0 ▶{
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
222 0 my $self = shift;
223 0 $self->{mech}->get("http://www.facebook.com/logout.php?api_key=$self->{key}&v=1.0&auth_token=$self->{auth_token}&confirm=1");
224 0 delete $self->{secret};
225 }
226  
227 =head2 C<$result = $fb-E<gt>query($QUERY)
228  
229 Perform FQL query $QUERY, returning the result in format $FORMAT
230 (either XML or JSON, JSON by default). FQL is a lot like SQL, but
231 with its own set of weird and privacy-related restrictions; for a
232 description, see
233 L<http://developers.facebook.com/documentation.php?v=1.0&doc=fql>.
234  
235 =cut
236  
237 sub query
238 0 ▶{
Subroutine query: not called CC=3 SCAR=24.8
Pod documented
239 0 my ($self, $q) = @_;
240 0 ▶ if (!$self->{secret} || $self->{private} eq $self->{secret}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"secret"} or $self->{"private"} eq $self->{"secret"}) 0 0 0
Condition0%0/3
not $self->{"secret"} or $self->{"private"} eq $self->{"secret"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"secret"} or $self->{"private"} eq $self->{"secret"}
not $self->{"secret"} $self->{"private"} eq $self->{"secret"}
Truth table0%0/3
not(A) or B
A= not $self->{"secret"}B= $self->{"private"} eq $self->{"secret"}
ABresult
1X1
011
000
241 0 print STDERR "Must log in before querying.\n";
242 0 return;
243 }
244 0 $self->_request('fql.query', query => $q)->decoded_content;
245 }
246  
247 =head2 ACCESSORS
248  
249 =over
250  
251 =item C<$fb-E<gt>uid> (read-only)
252  
253 =item C<$fb-E<gt>email> (read-only)
254  
255 =item C<$fb-E<gt>verbose> (read-write)
256  
257 =item C<$fb-E<gt>format> (read-write)
258  
259 =back
260  
261 =cut
262  
263 1 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.3
264 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
265 1 for (qw(uid email)) {
266 0 ▶ eval "sub $_\n{ shift->{$_} }";
Subroutine email: not called CC=1 SCAR=2.6
Subroutine uid: not called CC=1 SCAR=2.6
Pod documented
Pod documented
267 }
268 1 for (qw(verbose format)) {
269 0 ▶ eval "sub $_ :lvalue { shift->{$_} }";
Subroutine format: not called CC=1 SCAR=2.6
Subroutine verbose: not called CC=1 SCAR=2.6
Pod documented
Pod documented
270 }
271 }
272  
273 1 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
274 1 %FIELDS = (
275 user => [qw(uid* first_name last_name name* pic_small pic_big
276 pic_square pic affiliations profile_update_time timezone religion
277 birthday sex hometown_location meeting_sex meeting_for
278 relationship_status significant_other_id political
279 current_location activities interests is_app_user music tv movies
280 books quotes about_me hs_info education_history work_history
281 notes_count wall_count status has_added_app)],
282  
283 friend => [qw(uid1* uid2*)],
284  
285 group => [qw(gid* name nid pic_small pic_big pic description
286 group_type group_subtype recent_news creator update_time office
287 website venue)],
288  
289 group_member => [qw(uid* gid* positions)],
290  
291 event => [qw(eid* name tagline nid pic_small pic_big pic host
292 description event_type event_subtype start_time end_time creator
293 update_time location venue)],
294  
295 event_member => [qw(uid* eid* rsvp_status)],
296  
297 photo => [qw(pid* aid* owner src_small src_big src link caption
298 created)],
299  
300 album => [qw(aid* cover_pid* owner* name created modified
301 description location size)],
302  
303 photo_tag => [qw(pid* subject* xcoord ycoord)],
304 );
305  
306 1 for (keys %FIELDS) {
307 9 $IXFIELDS{$_} = [grep /\*$/, @{$FIELDS{$_}}];
308 9 s/\*$// for @{$FIELDS{$_}};
309 }
310  
311 } ## END BEGIN
312  
313 1 1;
314 __END__
↑ Summary