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