Query.pm

filter: Statementstmt 72.4% 50 / 69 Branchbran 35.0% 7 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 3 totaltotal 65.4% 70 / 107 CC 13 SCAR 20.0
CC
13
Cov
64%
begin
17.6
callmeback
12.0
new
5.7
CRAP 7.4
« WebBuilder.pm ↑ Summary Template.pm »
1 package OurNet::Query;
2 1 require 5.005;
3  
4 1 $OurNet::Query::VERSION = '1.22';
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use OurNet::Site;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use LWP::Parallel::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 OurNet::Query - Perform scriptable queries via LWP
15  
16 =head1 SYNOPSIS
17  
18 use OurNet::Query;
19  
20 # Set query parameters
21 my ($query, $hits) = ('autrijus', 10);
22 my @sites = ('Altavista', 'InfoSeek', 'Yahoo', 'Excite');
23 my %found;
24  
25 # Generate a new Query object
26 my $bot = OurNet::Query->new($query, $hits, @sites);
27  
28 # Perform a query
29 my $found = $bot->begin(\&callback, 30); # Timeout after 30 seconds
30  
31 print '*** ' . ($found ? $found : 'No') . ' match(es) found.';
32  
33 sub callback {
34 my %entry = @_;
35 my $entry = \%entry;
36  
37 unless ($found{$entry{'url'}}) {
38 print "*** [$entry->{'title'}]" .
39 " ($entry->{'score'})" .
40 " - [$entry->{'id'}]\n" .
41 " URL: [$entry->{'url'}]\n";
42 }
43  
44 $found{$entry{'url'}}++;
45 }
46  
47 =head1 DESCRIPTION
48  
49 OurNet::Query provides an easy interface to perform multiple queries
50 to internet services, and "wrap" them into your own format at once.
51 The results are processed on-the-fly and are returned via callback
52 functions.
53  
54 =cut
55  
56 # ---------------
57 # Variable Fields
58 # ---------------
59 1 ▶use fields qw/callback pua timeout query sites bots hits found/;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 # -----------------
62 # Package Constants
63 # -----------------
64 1 ▶use constant ERROR_QUERY_NEEDED => __PACKAGE__ . ' needs a query';
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use constant ERROR_HITS_NEEDED => __PACKAGE__ . ' needs sufficient hits';
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use constant ERROR_SITES_NEEDED => __PACKAGE__ . ' needs one or more sites';
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use constant ERROR_CALLBACK_NEEDED => __PACKAGE__ . ' needs a callback function';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use constant ERROR_PROTOCOL_UNDEF => __PACKAGE__ . ' cannot use the protocol';
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 # -------------------------------------
71 # Subroutine new($query, $hits, @sites)
72 # -------------------------------------
73 1 ▶sub new {
Subroutine new: called CC=5 SCAR=17.4
Pod undocumented
74 1 my $class = shift;
75 my $self = ($] > 5.00562) ? fields::new($class)
76 1 ▶ : do { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$] > 5.00562 ? : 1 0 1
77 0 bless [\%{"$class\::FIELDS"}], $class };
78  
79 1 ▶ $self->{'query'} = shift or (warn(ERROR_QUERY_NEEDED), return);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"query"} = shift() or warn("OurNet::Query needs a query"), return 1 0 1
80 1 ▶ $self->{'hits'} = shift or (warn(ERROR_HITS_NEEDED), return);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"hits"} = shift() or warn("OurNet::Query needs sufficient hits"), return 1 0 1
81 1 ▶ $self->{'sites'} = [ @_ ] or (warn(ERROR_SITES_NEEDED), return);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sites"} = [@_] or warn("OurNet::Query needs one or more sites"), return 1 0 1
82 1 $self->{'pua'} = LWP::Parallel::UserAgent->new();
83  
84 1 return $self;
85 }
86  
87 # ---------------------------------------------
88 # Subroutine begin($self, \&callback, $timeout)
89 # ---------------------------------------------
90 1 ▶sub begin {
Subroutine begin: called CC=7 SCAR=28.7
Pod undocumented
91 1 my $self = shift;
92  
93 1 ▶ $self->{'callback'} = ($_[0] ? $_[0] : $self->{'callback'})
Branch: 2 branches
BranchTrueFalseTotal
$self->{"callback"} = $_[0] ? $_[0] : $self->{"callback"} or warn("OurNet::Query needs a callback function"), return 1 0 1
$_[0] ? : 1 0 1
94 or (warn(ERROR_CALLBACK_NEEDED), return);
95 1 ▶ $self->{'timeout'} = ($_[1] ? $_[1] : $self->{'timeout'});
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 1 0 1
96 1 $self->{'pua'}->initialize();
97  
98 1 foreach my $count (0 .. $#{$self->{'sites'}}) {
99 1 $self->{'bots'}[$count] = OurNet::Site->new($self->{'sites'}[$count]);
100  
101 0 my $siteurl = $self->{'bots'}[$count]->geturl($self->{'query'}, $self->{'hits'});
102  
103 0 ▶ my $request = ($siteurl =~ m|^post:(.+?)\?(.+)|)
Branch: 2 branches
BranchTrueFalseTotal
my $request = $siteurl =~ /^post:(.+?)\?(.+)/ ? POST("http:$1", [split(/[&;=]/, $2, 0)]) : GET($siteurl) or warn("OurNet::Query cannot use the protocol"), return 0 0 0
$siteurl =~ /^post:(.+?)\?(.+)/ ? : 0 0 0
104 ? POST("http:$1", [split('[&;=]', $2)])
105 : GET($siteurl)
106 or (warn(ERROR_PROTOCOL_UNDEF), return);
107  
108 # Closure is not something that most Perl programmers need
109 # trouble themselves about to begin with. (perlref.pod)
110 0 ▶ $self->{'pua'}->register($request, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 0 $self->{'bots'}[$count]->callme($self, $count,
112 $_[0], \&callmeback);
113 0 return;
114 0 });
115 }
116  
117 0 $self->{'found'} = 0;
118 0 $self->{'pua'}->wait($self->{'timeout'});
119  
120 0 return $self->{'found'};
121 }
122  
123 # --------------------------------------
124 # Subroutine callmeback($self, $himself)
125 # --------------------------------------
126 0 ▶sub callmeback {
Subroutine callmeback: not called CC=3 SCAR=24.8
Pod undocumented
127 0 my ($self, $himself) = @_;
128  
129 0 foreach my $entry (@{$himself->{'response'}}) {
130 0 ▶ if (exists($entry->{'url'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"url"}) 0 0 0
131 0 &{$self->{'callback'}}(%{$entry});
132 0 delete($entry->{'url'});
133  
134 0 $self->{'found'}++;
135 }
136 }
137 }
138  
139 1 1;
140  
141 =head1 SEE ALSO
142  
143 L<OurNet::Site>
144  
145 =head1 AUTHORS
146  
147 Autrijus Tang E<lt>autrijus@autrijus.org>
148  
149 =head1 COPYRIGHT
150  
151 Copyright 2001 by Autrijus Tang E<lt>autrijus@autrijus.org>.
152  
153 All rights reserved. You can redistribute and/or modify
154 this module under the same terms as Perl itself.
155  
156 =cut
« WebBuilder.pm ↑ Summary Template.pm »