| 1 | package WWW::Scroogle; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use LWP; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use WWW::Scroogle::Result; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | our $VERSION = '0.0135'; | |||||||||
| 11 | |||||||||||
| 12 | sub new | ||||||||||
| 13 | 3 | ▶ | { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 14 | 3 | my $class = shift; | |||||||||
| 15 | 3 | my $self = { | |||||||||
| 16 | num_results => $class->_default_num_results, | ||||||||||
| 17 | language => $class->_default_language, | ||||||||||
| 18 | }; | ||||||||||
| 19 | 3 | bless $self, $class; | |||||||||
| 20 | 3 | return $self; | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 4 | ▶ | sub _default_num_results { return 100; } | ||||||||
|
Subroutine
_default_num_results: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | sub searchstring | ||||||||||
| 26 | 6 | ▶ | { | ||||||||
|
Subroutine
searchstring: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 27 | 6 | ▶ | ref(my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | or croak 'instance variable needed!'; | ||||||||||
| 29 | defined($self->{searchstring}) | ||||||||||
| 30 | 5 | ▶ | or croak 'no searchstring given yet!'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 3 | return $self->{searchstring}; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | sub set_searchstring | ||||||||||
| 35 | 4 | ▶ | { | ||||||||
|
Subroutine
set_searchstring: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 36 | 4 | ▶ | ref(my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | or croak 'instance variable needed!'; | ||||||||||
| 38 | 3 | ▶ | defined(my $searchstring = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | or croak 'no searchstring given!'; | ||||||||||
| 40 | 2 | ▶ | if ($searchstring eq '') { croak 'nullstring given!' } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 1 | $self->{searchstring} = $searchstring; | |||||||||
| 42 | 1 | return $self; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 5 | ▶ | sub _default_language { return ''; } | ||||||||
|
Subroutine
_default_language: called CC=1 SCAR=0.0
| |||||||||||
| 46 | |||||||||||
| 47 | sub language | ||||||||||
| 48 | 8 | ▶ | { | ||||||||
|
Subroutine
language: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 49 | 8 | ▶ | ref(my $either = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | or croak 'instance variable needed!'; | ||||||||||
| 51 | 7 | ▶ | if ($either->{language} eq '') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 5 | return 'all'; | |||||||||
| 53 | }else { | ||||||||||
| 54 | 2 | return $either->{language}; | |||||||||
| 55 | } | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | sub set_language | ||||||||||
| 59 | 6 | ▶ | { | ||||||||
|
Subroutine
set_language: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 60 | 6 | ▶ | ref(my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | or croak 'instance variable needed!'; | ||||||||||
| 62 | 5 | my $language = shift; | |||||||||
| 63 | 5 | ▶ | if (not defined $language) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 1 | $self->{language} = $self->_default_language; | |||||||||
| 65 | }else { | ||||||||||
| 66 | 4 | ▶ | grep{$language eq $_}$self->languages | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | or croak($language.' is not a valid language! '); | ||||||||||
| 68 | 3 | ▶ | if ($language eq 'all') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 1 | $self->{language} = ''; | |||||||||
| 70 | }else { | ||||||||||
| 71 | 2 | $self->{language} = $language; | |||||||||
| 72 | 2 | return $self; | |||||||||
| 73 | } | ||||||||||
| 74 | } | ||||||||||
| 75 | 2 | return $self; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub languages | ||||||||||
| 79 | 4 | ▶ | { | ||||||||
|
Subroutine
languages: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 80 | 4 | ▶ | ref(my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | or croak 'instance variable needed!'; | ||||||||||
| 82 | 4 | ▶ | if (exists $self->{languages}) { # does $self->{languages} exist? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 3 | return @ {$self->{languages}}; # return the array $self->{languages} | |||||||||
| 84 | } else { # if $self->{languages} does not exist, populate it | ||||||||||
| 85 | 1 | @ {$self->{languages}} = qw( | |||||||||
| 86 | all ar zs zt cs da nl en et fi fr de el iw | ||||||||||
| 87 | hu is it ja ko lv lt no pt pl ro ru es sv tr | ||||||||||
| 88 | ); | ||||||||||
| 89 | 1 | return @ {$self->{languages}}; # and return it | |||||||||
| 90 | } | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | sub num_results | ||||||||||
| 94 | 6 | ▶ | { | ||||||||
|
Subroutine
num_results: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 95 | 6 | ▶ | ref(my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | or croak 'instance variable needed!'; | ||||||||||
| 97 | 5 | return $self->{num_results}; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | sub set_num_results | ||||||||||
| 101 | 7 | ▶ | { | ||||||||
|
Subroutine
set_num_results: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 102 | 7 | ▶ | ref(my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | or croak "instance variable needed!"; | ||||||||||
| 104 | 6 | my $num_results = shift; | |||||||||
| 105 | 6 | ▶ | if (not defined $num_results) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 1 | $self->{num_results} = $self->_default_num_results; | |||||||||
| 107 | 1 | return $self; | |||||||||
| 108 | } | ||||||||||
| 109 | 5 | ▶ | if (not $num_results =~ m/^\d+$/) { croak 'odd number expected!'; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 2 | ▶ | if ($num_results < 1) { croak 'minimum is 1 result!'} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 1 | $self->{num_results} = $num_results; | |||||||||
| 112 | 1 | return $self; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | sub perform_search | ||||||||||
| 116 | 3 | ▶ | { | ||||||||
|
Subroutine
perform_search: called CC=10 SCAR=26.4
Pod
documented
| |||||||||||
| 117 | 3 | ▶ | ref(my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | or croak 'instance variable needed!'; | ||||||||||
| 119 | 2 | my $searchstring = $self->searchstring; | |||||||||
| 120 | 1 | my $language = $self->language; | |||||||||
| 121 | 1 | my $num_results = $self->num_results; | |||||||||
| 122 | 0 | ▶ | if ($self->has_results) { $self->delete_results }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 1 | my $agent = LWP::UserAgent->new; | |||||||||
| 124 | 1 | my $request = HTTP::Request->new(POST => 'http://www.scroogle.org/cgi-bin/nbbw.cgi'); | |||||||||
| 125 | 1 | $request->content_type('application/x-www-form-urlencodde'); | |||||||||
| 126 | 1 | my $postdata; | |||||||||
| 127 | 1 | ▶ | if ($language ne 'all') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 0 | $postdata = 'Gw='.$searchstring.'&n=100&l='.$language.'&z='; | |||||||||
| 129 | } else { | ||||||||||
| 130 | 1 | $postdata = 'Gw='.$searchstring.'&n=100&z='; | |||||||||
| 131 | } | ||||||||||
| 132 | 1 | my $niterate; | |||||||||
| 133 | 1 | ▶ | if ($self->num_results <= 100) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 1 | $niterate = 1; | |||||||||
| 135 | }else { | ||||||||||
| 136 | 0 | $niterate = ($num_results - $num_results%100)/100; | |||||||||
| 137 | 0 | ▶ | if ($num_results%100 == 0) { $niterate--; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | } | ||||||||||
| 139 | 1 | my $results_left = $num_results; | |||||||||
| 140 | 1 | for (0..$niterate) { | |||||||||
| 141 | 2 | $request->content($postdata.$_); | |||||||||
| 142 | 2 | my $result = $agent->request($request); | |||||||||
| 143 | 2 | for (split( '\n', $result->content)) { | |||||||||
| 144 | 0 | ▶ | if ($results_left <= 0) { last; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | 2 | ▶ | if (m/^(\d{1,5})\. <A Href="(.*)">/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | 0 | $self->_add_result({ | |||||||||
| 147 | position => $1, | ||||||||||
| 148 | url => $2 | ||||||||||
| 149 | }); | ||||||||||
| 150 | 0 | $results_left--; | |||||||||
| 151 | } | ||||||||||
| 152 | } | ||||||||||
| 153 | } | ||||||||||
| 154 | 1 | return 1; | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | sub _add_result | ||||||||||
| 158 | 5 | ▶ | { | ||||||||
|
Subroutine
_add_result: called CC=5 SCAR=16.1
| |||||||||||
| 159 | 5 | ▶ | ref (my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | or croak 'instance variable needed!'; | ||||||||||
| 161 | 4 | my $options = shift; | |||||||||
| 162 | 4 | ▶ | if (not ref $options eq "HASH") { croak 'no options hash given!'; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | 3 | ▶ | if (not exists $options->{url}) { croak 'no url given!'; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 164 | 2 | ▶ | if (not exists $options->{position}) { croak 'no position given!'; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | my $result = WWW::Scroogle::Result->new({ | ||||||||||
| 166 | url => $options->{url}, | ||||||||||
| 167 | position => $options->{position}, | ||||||||||
| 168 | 1 | searchstring => $self->searchstring, | |||||||||
| 169 | language => $self->language, | ||||||||||
| 170 | }); | ||||||||||
| 171 | 1 | push @ {$self->{results}}, $result; | |||||||||
| 172 | 1 | return $self; | |||||||||
| 173 | } | ||||||||||
| 174 | |||||||||||
| 175 | sub nresults | ||||||||||
| 176 | 4 | ▶ | { | ||||||||
|
Subroutine
nresults: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 177 | 4 | ▶ | ref (my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 178 | or croak 'instance variable needed!'; | ||||||||||
| 179 | 3 | ▶ | if ($self->has_results) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | 1 | my $nresults = @ {$self->{results}}; | |||||||||
| 181 | 1 | return $nresults; | |||||||||
| 182 | } | ||||||||||
| 183 | 2 | croak 'no results avaible'; | |||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | sub get_results | ||||||||||
| 187 | 0 | ▶ | { | ||||||||
|
Subroutine
get_results: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 188 | 0 | ▶ | ref (my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | or croak 'instance variable needed!'; | ||||||||||
| 190 | 0 | ▶ | if (not $self->has_results) { croak 'no results avaible' } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 191 | 0 | return @ {$self->{results}}; | |||||||||
| 192 | } | ||||||||||
| 193 | |||||||||||
| 194 | sub has_results | ||||||||||
| 195 | 10 | ▶ | { | ||||||||
|
Subroutine
has_results: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 196 | 10 | ▶ | ref (my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 197 | or croak 'instance variable needed!'; | ||||||||||
| 198 | 9 | ▶ | if (exists $self->{results}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 199 | 3 | 1; | |||||||||
| 200 | }else { | ||||||||||
| 201 | 6 | return; | |||||||||
| 202 | } | ||||||||||
| 203 | } | ||||||||||
| 204 | |||||||||||
| 205 | sub delete_results | ||||||||||
| 206 | 3 | ▶ | { | ||||||||
|
Subroutine
delete_results: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 207 | 3 | ▶ | ref (my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 208 | or croak 'instance variable needed!'; | ||||||||||
| 209 | 2 | ▶ | if ($self->has_results) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 210 | 1 | delete $self->{results}; | |||||||||
| 211 | 1 | 1; | |||||||||
| 212 | }else { | ||||||||||
| 213 | 1 | return; | |||||||||
| 214 | } | ||||||||||
| 215 | } | ||||||||||
| 216 | |||||||||||
| 217 | sub get_result | ||||||||||
| 218 | 0 | ▶ | { | ||||||||
|
Subroutine
get_result: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 219 | 0 | ▶ | ref (my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 220 | or croak 'instance variable needed!'; | ||||||||||
| 221 | 0 | ▶ | defined (my $requested_result = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 222 | or croak 'no value given'; | ||||||||||
| 223 | 0 | ▶ | if (not $self->has_results) { croak 'no results avaible'; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 224 | 0 | return $self->{results}[$requested_result - 1]; | |||||||||
| 225 | } | ||||||||||
| 226 | |||||||||||
| 227 | sub position | ||||||||||
| 228 | 0 | ▶ | { | ||||||||
|
Subroutine
position: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 229 | 0 | ▶ | ref (my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 230 | or croak 'instance variable needed!'; | ||||||||||
| 231 | 0 | ▶ | defined (my $string = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 232 | or croak 'no string given!'; | ||||||||||
| 233 | 0 | ▶ | if (not $self->has_results) { croak 'no results avaible'; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 234 | 0 | for (@ {$self->{results}}) { # iterate over all avaible results | |||||||||
| 235 | 0 | ▶ | if ($_->url =~ /$string/) { # does the url match the given pattern? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 236 | 0 | return $_->position # return the position of the result | |||||||||
| 237 | } | ||||||||||
| 238 | } | ||||||||||
| 239 | 0 | return; # return boolean false if no matches were found | |||||||||
| 240 | } | ||||||||||
| 241 | |||||||||||
| 242 | sub positions | ||||||||||
| 243 | 0 | ▶ | { | ||||||||
|
Subroutine
positions: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 244 | 0 | ▶ | ref (my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 245 | or croak 'instance variable needed!'; | ||||||||||
| 246 | 0 | ▶ | defined (my $string = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 247 | or croak 'no string given!'; | ||||||||||
| 248 | 0 | ▶ | if (not $self->has_results) { croak 'no results avaible'; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 249 | 0 | my @results = $self->get_results_matching($string); # get array of result objects | |||||||||
| 250 | 0 | my @return; | |||||||||
| 251 | 0 | for (@results) { # iterate array of result objects | |||||||||
| 252 | 0 | push @return, $_->position; # push the position number of that array to @return | |||||||||
| 253 | } | ||||||||||
| 254 | 0 | ▶ | if (scalar(@return) == 0) { return; } # return boolean false if no matches were found | ||||||||
|
Branch: 1 branch
| |||||||||||
| 255 | 0 | return @return; # return the array of position numbers | |||||||||
| 256 | } | ||||||||||
| 257 | |||||||||||
| 258 | sub get_results_matching | ||||||||||
| 259 | 0 | ▶ | { | ||||||||
|
Subroutine
get_results_matching: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||
| 260 | 0 | ▶ | ref (my $self = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 261 | or croak 'instance variable needed!'; | ||||||||||
| 262 | 0 | ▶ | defined (my $string = shift) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 263 | or croak 'no string given!'; | ||||||||||
| 264 | 0 | ▶ | if (not $self->has_results) { croak 'no results avaible'; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 265 | 0 | my @return; | |||||||||
| 266 | 0 | for (@ {$self->{results}}) { # iterate over all avaible results | |||||||||
| 267 | 0 | ▶ | if ($_->url =~ /$string/) { # does the url match the given pattern? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 268 | 0 | push @return, $_; # push the result object to @return | |||||||||
| 269 | } | ||||||||||
| 270 | } | ||||||||||
| 271 | 0 | ▶ | if (scalar(@return) == 0) { return; } # return boolean false if no matches were found | ||||||||
|
Branch: 1 branch
| |||||||||||
| 272 | 0 | return @return; # return the array of result objects | |||||||||
| 273 | } | ||||||||||
| 274 | 2 | 1; | |||||||||
| 275 | |||||||||||
| 276 | __END__ | ||||||||||