ProxyList.pm

filter: Statementstmt 83.7% 139 / 166 Branchbran 95.2% 40 / 42 Conditioncond 95.2% 40 / 42 MC/DCmcdc 92.8% 26 / 28 Subroutinesub 79.4% 27 / 34 Podpod 100% 20 / 20 totaltotal 87.9% 292 / 332 CC 39 SCAR 15.0
CC
39
Cov
88%
set_filter_protocol_list
10.0
set_filter_anonymity_list
10.0
add_proxies
6.0
CRAP 4.5
« Proxy.pm ↑ Summary
1 package Geonode::Free::ProxyList;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use List::Util qw( shuffle );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use List::MoreUtils qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Geonode::Free::Proxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Geonode::Free::ProxyList - Get Free Geonode Proxies by using some filters
18  
19 =head1 VERSION
20  
21 Version 0.0.5
22  
23 =cut
24  
25 2 our $VERSION = '0.0.5';
26  
27 2 my $API_ROOT = 'https://proxylist.geonode.com/api/proxy-list?';
28  
29 =head1 SYNOPSIS
30  
31 Get Geonode's free proxy list and apply some filters. You can later choose them by random.
32  
33 my $proxy_list = Geonode::Free::ProxyList->new();
34  
35 $list->set_filter_google('true');
36 $list->set_filter_port(3128);
37 $list->set_filter_limit(200);
38  
39 $list->add_proxies; # Add proxies to the list for current filters
40  
41 $list->set_filter_google('false');
42 $list->set_filter_port(); # reset filter
43 $list->set_filter_limit(); # reset filter
44 $list->set_filter_protocol_list( [ 'socks4', 'socks5' ] );
45 $list->set_filter_speed('fast');
46  
47 $list->add_proxies; # Add proxies to the list for current filters
48  
49 # List of proxies is shuffled
50  
51 my $some_proxy = $list->get_next; # Repeats when list is exhausted
52 my $other_proxy = $list->get_next; # Repeats when list is exhausted
53  
54 my $random_proxy = $list->get_random_proxy; # Can repeat
55  
56 $some_proxy->get_methods(); # [ 'http', 'socks5' ]
57  
58 Geonode::Free::Proxy::prefer_socks(); # Will use socks for url, if available
59  
60 $some_proxy->get_url(); # 'socks://127.0.0.1:3128';
61  
62 Geonode::Free::Proxy::prefer_http(); # Will use http url, if available
63  
64 $some_proxy->get_url(); # 'http://127.0.0.1:3128';
65  
66 $some_proxy->can_use_http(); # 1
67 $some_proxy->can_use_socks(); # 1
68  
69 $other_proxy->can_use_socks(); # q()
70 $other_proxy->can_use_http(); # 1
71  
72 Geonode::Free::Proxy::prefer_socks(); # Will use socks for url, if available
73  
74 $some_proxy->get_url(); # 'http://foo.bar.proxy:1234';
75  
76 =head1 SUBROUTINES/METHODS
77  
78 =head2 new
79  
80 Instantiate Geonode::Free::ProxyList object
81  
82 =cut
83  
84 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
85 1 my $self = bless {
86 proxy_list => [],
87 index => 0,
88 filters => {},
89 ua => LWP::UserAgent->new()
90 },
91 shift;
92  
93 1 $self->reset_filters();
94  
95 1 return $self;
96 }
97  
98 =head2 reset_proxy_list
99  
100 Clears proxy list
101  
102 =cut
103  
104 0 ▶sub reset_proxy_list {
Subroutine reset_proxy_list: not called CC=1 SCAR=6.9
Pod documented
105 0 my $self = @_;
106  
107 0 $self->{proxy_list} = [];
108  
109 0 return;
110 }
111  
112 =head2 reset_filters
113  
114 Reset filtering options
115  
116 =cut
117  
118 1 ▶sub reset_filters {
Subroutine reset_filters: called CC=1 SCAR=0.0
Pod documented
119 1 my ($self) = @_;
120  
121 $self->{filters} = {
122 1 country => undef,
123 google => undef,
124 filterPort => undef,
125 protocols => undef,
126 anonymityLevel => undef,
127 speed => undef,
128 filterByOrg => undef,
129 filterUpTime => undef,
130 filterLastChecked => undef,
131 limit => undef
132 };
133  
134 1 return;
135 }
136  
137 =head2 set_filter_country
138  
139 Set country filter. Requires a two character uppercase string or undef to reset the filter
140  
141 =cut
142  
143 9 ▶sub set_filter_country {
Subroutine set_filter_country: called CC=3 SCAR=11.0
Pod documented
144 9 my ( $self, $country ) = @_;
145  
146 9 ▶ if ( defined $country && $country !~ m{^[A-Z]{2}$}sxm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $country and not $country =~ /^[A-Z]{2}$/msx) 6 3 9
Condition100%3/3
defined $country and not $country =~ /^[A-Z]{2}$/msx
!ll&&!rl&&r
126
MC/DC100%2/2
defined $country and not $country =~ /^[A-Z]{2}$/msx
defined $country not $country =~ /^[A-Z]{2}$/msx
Truth table100%3/3
A and not(B)
A= defined $countryB= not $country =~ /^[A-Z]{2}$/msx
ABresult
0X0
100
111
147 6 croak q()
148 . "ERROR: '$country' is not a two character uppercase code\n"
149 . "Please, check valid values at following url:\n"
150 . 'https://geonode.com/free-proxy-list';
151 }
152  
153 3 $self->{filters}{country} = $country;
154  
155 3 return;
156 }
157  
158 =head2 set_filter_google
159  
160 Set google filter. Allowed values are 'true'/'false'. You can use undef to reset the filter
161  
162 =cut
163  
164 6 ▶sub set_filter_google {
Subroutine set_filter_google: called CC=3 SCAR=11.0
Pod documented
165 6 my ( $self, $google ) = @_;
166  
167 6 ▶ if ( defined $google && $google !~ m{^(?: true|false )$}sxm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $google and not $google =~ /^(?: true|false )$/msx) 2 4 6
Condition100%3/3
defined $google and not $google =~ /^(?: true|false )$/msx
!ll&&!rl&&r
132
MC/DC100%2/2
defined $google and not $google =~ /^(?: true|false )$/msx
defined $google not $google =~ /^(?: true|false )$/msx
Truth table100%3/3
A and not(B)
A= defined $googleB= not $google =~ /^(?: true|false )$/msx
ABresult
0X0
100
111
168 2 croak q()
169 . "ERROR: '$google' is not a valid value for google filter\n"
170 . 'Valid values are: true/false';
171 }
172  
173 4 $self->{filters}{google} = $google;
174  
175 4 return;
176 }
177  
178 =head2 set_filter_port
179  
180 Set port filter. Allowed values are numbers that does not start by zero. You can use undef to reset the filter
181  
182 =cut
183  
184 7 ▶sub set_filter_port {
Subroutine set_filter_port: called CC=3 SCAR=11.0
Pod documented
185 7 my ( $self, $port ) = @_;
186  
187 7 ▶ if ( defined $port && $port !~ m{^(?: (?!0)[0-9]++ )$}sxm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port and not $port =~ /^(?: (?!0)[0-9]++ )$/msx) 4 3 7
Condition100%3/3
defined $port and not $port =~ /^(?: (?!0)[0-9]++ )$/msx
!ll&&!rl&&r
124
MC/DC100%2/2
defined $port and not $port =~ /^(?: (?!0)[0-9]++ )$/msx
defined $port not $port =~ /^(?: (?!0)[0-9]++ )$/msx
Truth table100%3/3
A and not(B)
A= defined $portB= not $port =~ /^(?: (?!0)[0-9]++ )$/msx
ABresult
0X0
100
111
188 4 croak "ERROR: '$port' is not a valid value for por filter";
189 }
190  
191 3 $self->{filters}{filterPort} = $port;
192  
193 3 return;
194 }
195  
196 =head2 set_filter_protocol_list
197  
198 Set protocol list filter. Allowed values are http, https, socks4, socks5. You can use an scalar or a list of values. By using undef you can reset the filter
199  
200 =cut
201  
202 9 ▶sub set_filter_protocol_list {
Subroutine set_filter_protocol_list: called CC=10 SCAR=23.0
Pod documented
203 9 my ( $self, $protocol_list ) = @_;
204  
205 9 ▶ if ( defined $protocol_list && ref $protocol_list eq q() ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $protocol_list and ref $protocol_list eq '') { } 2 7 9
elsif (defined $protocol_list and ref $protocol_list ne 'ARRAY') { } 1 7 8
Condition100%3/3
defined $protocol_list and ref $protocol_list eq ""
!ll&&!rl&&r
162
MC/DC100%2/2
defined $protocol_list and ref $protocol_list eq ""
defined $protocol_list ref $protocol_list eq ""
Truth table100%3/3
A and B
A= defined $protocol_listB= ref $protocol_list eq ""
ABresult
0X0
100
111
206 2 $protocol_list = [$protocol_list];
207 }
208 ▶ elsif ( defined $protocol_list && ref $protocol_list ne 'ARRAY' ) {
Condition100%3/3
defined $protocol_list and ref $protocol_list ne "ARRAY"
!ll&&!rl&&r
151
MC/DC100%2/2
defined $protocol_list and ref $protocol_list ne "ARRAY"
defined $protocol_list ref $protocol_list ne "ARRAY"
Truth table100%3/3
A and B
A= defined $protocol_listB= ref $protocol_list ne "ARRAY"
ABresult
0X0
100
111
209 1 croak 'ERROR: just a single scalar or an array reference are accepted';
210 }
211  
212 8 ▶ if ( !defined $protocol_list ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $protocol_list) 1 7 8
213 1 $self->{filters}{protocols} = undef;
214 1 return;
215 }
216  
217 7 my @list;
218 7 for my $option ( @{$protocol_list} ) {
219 16 ▶ if ( $option !~ m{ ^(?:https?|socks[45])$ }sxm ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($option =~ / ^(?:https?|socks[45])$ /msx) 1 15 16
220 1 croak "ERROR: '$option' is not a valid value for protocol list";
221 }
222  
223 15 push @list, $option;
224 }
225  
226 6 ▶ if ( defined $protocol_list && @list == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $protocol_list and @list == 0) 1 5 6
Condition66%2/3
defined $protocol_list and @list == 0
!ll&&!rl&&r
051
MC/DC50%1/2
defined $protocol_list and @list == 0
defined $protocol_list @list == 0
Truth table66%2/3
A and B
A= defined $protocol_listB= @list == 0
ABresult
0X0
100
111
227 1 croak 'ERROR: Cannot set empty protocol list';
228 }
229  
230 5 $self->{filters}{protocols} = [ uniq @list ];
231  
232 5 return;
233 }
234  
235 =head2 set_filter_anonymity_list
236  
237 Set anonimity list filter. Allowed values are http, https, socks4, socks5. You can use an scalar or a list of values. By using undef you can reset the filter
238  
239 =cut
240  
241 9 ▶sub set_filter_anonymity_list {
Subroutine set_filter_anonymity_list: called CC=10 SCAR=23.0
Pod documented
242 9 my ( $self, $anonymity_list ) = @_;
243  
244 9 ▶ if ( defined $anonymity_list && ref $anonymity_list eq q() ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $anonymity_list and ref $anonymity_list eq '') { } 2 7 9
elsif (defined $anonymity_list and ref $anonymity_list ne 'ARRAY') { } 1 7 8
Condition100%3/3
defined $anonymity_list and ref $anonymity_list eq ""
!ll&&!rl&&r
162
MC/DC100%2/2
defined $anonymity_list and ref $anonymity_list eq ""
defined $anonymity_list ref $anonymity_list eq ""
Truth table100%3/3
A and B
A= defined $anonymity_listB= ref $anonymity_list eq ""
ABresult
0X0
100
111
245 2 $anonymity_list = [$anonymity_list];
246 }
247 ▶ elsif ( defined $anonymity_list && ref $anonymity_list ne 'ARRAY' ) {
Condition100%3/3
defined $anonymity_list and ref $anonymity_list ne "ARRAY"
!ll&&!rl&&r
151
MC/DC100%2/2
defined $anonymity_list and ref $anonymity_list ne "ARRAY"
defined $anonymity_list ref $anonymity_list ne "ARRAY"
Truth table100%3/3
A and B
A= defined $anonymity_listB= ref $anonymity_list ne "ARRAY"
ABresult
0X0
100
111
248 1 croak 'ERROR: just a single scalar or an array reference are accepted';
249 }
250  
251 8 ▶ if ( !defined $anonymity_list ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $anonymity_list) 1 7 8
252 1 $self->{filters}{anonymityLevel} = undef;
253 1 return;
254 }
255  
256 7 my @list;
257 7 for my $option ( @{$anonymity_list} ) {
258 14 ▶ if ( $option !~ m{ ^(?:elite|anonymous|transparent)$ }sxm ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($option =~ / ^(?:elite|anonymous|transparent)$ /msx) 1 13 14
259 1 croak "ERROR: '$option' is not a valid value for anonymity list";
260 }
261  
262 13 push @list, $option;
263 }
264  
265 6 ▶ if ( defined $anonymity_list && @list == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $anonymity_list and @list == 0) 1 5 6
Condition66%2/3
defined $anonymity_list and @list == 0
!ll&&!rl&&r
051
MC/DC50%1/2
defined $anonymity_list and @list == 0
defined $anonymity_list @list == 0
Truth table66%2/3
A and B
A= defined $anonymity_listB= @list == 0
ABresult
0X0
100
111
266 1 croak 'ERROR: Cannot set empty protocol list';
267 }
268  
269 5 $self->{filters}{anonymityLevel} = [ uniq @list ];
270  
271 5 return;
272 }
273  
274 =head2 set_filter_speed
275  
276 Set speed filter. Allowed values are: fast, medium, slow. You can use undef to reset the filter
277  
278 =cut
279  
280 6 ▶sub set_filter_speed {
Subroutine set_filter_speed: called CC=3 SCAR=11.0
Pod documented
281 6 my ( $self, $speed ) = @_;
282  
283 6 ▶ if ( defined $speed && $speed !~ m{^(?: fast|medium|slow )$}sxm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $speed and not $speed =~ /^(?: fast|medium|slow )$/msx) 1 5 6
Condition100%3/3
defined $speed and not $speed =~ /^(?: fast|medium|slow )$/msx
!ll&&!rl&&r
141
MC/DC100%2/2
defined $speed and not $speed =~ /^(?: fast|medium|slow )$/msx
defined $speed not $speed =~ /^(?: fast|medium|slow )$/msx
Truth table100%3/3
A and not(B)
A= defined $speedB= not $speed =~ /^(?: fast|medium|slow )$/msx
ABresult
0X0
100
111
284 1 croak q()
285 . "ERROR: '$speed' is not a valid value for por speed\n"
286 . 'Valid values are: fast/slow/medium';
287 }
288  
289 5 $self->{filters}{speed} = $speed;
290  
291 5 return;
292 }
293  
294 =head2 set_filter_org
295  
296 Set organization filter. Requires some non empty string. You can use undef to reset the filter
297  
298 =cut
299  
300 3 ▶sub set_filter_org {
Subroutine set_filter_org: called CC=3 SCAR=11.0
Pod documented
301 3 my ( $self, $org ) = @_;
302  
303 3 ▶ if ( defined $org && $org eq q() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $org and $org eq "") 1 2 3
Condition100%3/3
defined $org and $org eq ""
!ll&&!rl&&r
111
MC/DC100%2/2
defined $org and $org eq ""
defined $org $org eq ""
Truth table100%3/3
A and B
A= defined $orgB= $org eq ""
ABresult
0X0
100
111
304 1 croak 'ERROR: Cannot set empty organization filter';
305 }
306  
307 2 $self->{filters}{filterByOrg} = $org;
308  
309 2 return;
310 }
311  
312 =head2 set_filter_uptime
313  
314 Set uptime filter. Allowed values are: 0-100 in 10% increments. You can use undef to reset the filter
315  
316 =cut
317  
318 106 ▶sub set_filter_uptime {
Subroutine set_filter_uptime: called CC=3 SCAR=11.0
Pod documented
319 106 my ( $self, $uptime ) = @_;
320  
321 106 ▶ if ( defined $uptime && $uptime !~ m{^(?: 0 | [1-9]0 | 100 )$}sxm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uptime and not $uptime =~ /^(?: 0 | [1-9]0 | 100 )$/msx) 94 12 106
Condition100%3/3
defined $uptime and not $uptime =~ /^(?: 0 | [1-9]0 | 100 )$/msx
!ll&&!rl&&r
11194
MC/DC100%2/2
defined $uptime and not $uptime =~ /^(?: 0 | [1-9]0 | 100 )$/msx
defined $uptime not $uptime =~ /^(?: 0 | [1-9]0 | 100 )$/msx
Truth table100%3/3
A and not(B)
A= defined $uptimeB= not $uptime =~ /^(?: 0 | [1-9]0 | 100 )$/msx
ABresult
0X0
100
111
322 94 croak q()
323 . "ERROR: '$uptime' is not a valid value for por uptime\n"
324 . 'Valid values are: 0-100% in 10% increments';
325 }
326  
327 12 $self->{filters}{filterUpTime} = $uptime;
328  
329 12 return;
330 }
331  
332 =head2 set_filter_last_checked
333  
334 Set last checked filter. Allowed values are: 1-9 and 20-60 in 10% increments. You can use undef to reset the filter
335  
336 =cut
337  
338 75 ▶sub set_filter_last_checked {
Subroutine set_filter_last_checked: called CC=3 SCAR=11.0
Pod documented
339 75 my ( $self, $last_checked ) = @_;
340  
341 75 ▶ if ( defined $last_checked && $last_checked !~ m{^(?:[1-9]|[1-6]0)$}sxm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_checked and not $last_checked =~ /^(?:[1-9]|[1-6]0)$/msx) 59 16 75
Condition100%3/3
defined $last_checked and not $last_checked =~ /^(?:[1-9]|[1-6]0)$/msx
!ll&&!rl&&r
11559
MC/DC100%2/2
defined $last_checked and not $last_checked =~ /^(?:[1-9]|[1-6]0)$/msx
defined $last_checked not $last_checked =~ /^(?:[1-9]|[1-6]0)$/msx
Truth table100%3/3
A and not(B)
A= defined $last_checkedB= not $last_checked =~ /^(?:[1-9]|[1-6]0)$/msx
ABresult
0X0
100
111
342 59 croak q()
343 . "ERROR: '$last_checked' is not a valid value for por uptime\n"
344 . 'Valid values are: 0-100% in 10% increments';
345 }
346  
347 16 $self->{filters}{filterLastChecked} = $last_checked;
348  
349 16 return;
350 }
351  
352 =head2 set_filter_limit
353  
354 Set speed filter. Allowed values are numbers greater than 0. You can use undef to reset the filter
355  
356 =cut
357  
358 6 ▶sub set_filter_limit {
Subroutine set_filter_limit: called CC=3 SCAR=11.0
Pod documented
359 6 my ( $self, $limit ) = @_;
360  
361 6 ▶ if ( defined $limit && $limit !~ m{^ (?!0)[0-9]++ $}sxm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $limit and not $limit =~ /^ (?!0)[0-9]++ $/msx) 4 2 6
Condition100%3/3
defined $limit and not $limit =~ /^ (?!0)[0-9]++ $/msx
!ll&&!rl&&r
114
MC/DC100%2/2
defined $limit and not $limit =~ /^ (?!0)[0-9]++ $/msx
defined $limit not $limit =~ /^ (?!0)[0-9]++ $/msx
Truth table100%3/3
A and not(B)
A= defined $limitB= not $limit =~ /^ (?!0)[0-9]++ $/msx
ABresult
0X0
100
111
362 4 croak q()
363 . "ERROR: '$limit' is not a valid value for por speed\n"
364 . 'Valid values are: numbers > 0';
365 }
366  
367 2 $self->{filters}{limit} = $limit;
368  
369 2 return;
370 }
371  
372 =head2 set_env_proxy
373  
374 Use proxy based on environment variables
375  
376 See: https://metacpan.org/pod/LWP::UserAgent#env_proxy
377  
378 Example:
379  
380 $proxy_list->set_env_proxy();
381  
382 =cut
383  
384 0 ▶sub set_env_proxy {
Subroutine set_env_proxy: not called CC=1 SCAR=6.9
Pod documented
385 0 my ($self) = @_;
386  
387 0 $self->{ua}->env_proxy;
388  
389 0 return;
390 }
391  
392 =head2 set_proxy
393  
394 Exposes LWP::UserAgent's proxy method to configure proxy server
395  
396 See: https://metacpan.org/pod/LWP::UserAgent#proxy
397  
398 Example:
399  
400 $proxy_list->proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');
401  
402 =cut
403  
404 0 ▶sub set_proxy {
Subroutine set_proxy: not called CC=1 SCAR=6.9
Pod documented
405 0 my ( $self, @params ) = @_;
406  
407 0 $self->{ua}->proxy(@params);
408  
409 0 return;
410 }
411  
412 =head2 set_timeout
413  
414 Set petition timeout. Exposes LWP::UserAgent's timeout method
415  
416 See: https://metacpan.org/pod/LWP::UserAgent#timeout
417  
418 Example:
419  
420 $proxy_list->timeout(10);
421  
422 =cut
423  
424 0 ▶sub set_timeout {
Subroutine set_timeout: not called CC=1 SCAR=6.9
Pod documented
425 0 my ( $self, @params ) = @_;
426  
427 0 $self->{ua}->timeout(@params);
428  
429 0 return;
430 }
431  
432 =head2 add_proxies
433  
434 Add proxy list according to stored filters
435  
436 =cut
437  
438 0 ▶sub add_proxies {
Subroutine add_proxies: not called CC=2 SCAR=17.9
Pod documented
439 0 my ($self) = @_;
440  
441 0 my $response = $self->{ua}->get( $API_ROOT . $self->_calculate_api_url );
442  
443 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
444 0 croak 'ERROR: Could not get url, ' . $response->status_line;
445 }
446  
447 0 ▶ my $data = encode( 'utf-8', $response->decoded_content, sub { q() } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
448  
449 0 $self->{proxy_list} = [ shuffle @{ $self->_create_proxy_list($data) } ];
450 0 $self->{index} = 0;
451  
452 0 return;
453 }
454  
455 1 ▶sub _create_proxy_list {
Subroutine _create_proxy_list: called CC=2 SCAR=7.0
456 1 my ( $self, $struct ) = @_;
457  
458 1 $struct = decode_json $struct;
459  
460 0 my %proxies = map { $_->id => $_ } $self->get_all_proxies;
461  
462 1 for my $item ( @{ $struct->{data} } ) {
463 $proxies{ $item->{_id} } = Geonode::Free::Proxy->new(
464 $item->{_id},
465 $item->{ip},
466 $item->{port},
467 $item->{protocols}
468 10 );
469 }
470  
471 1 return [ values %proxies ];
472 }
473  
474 9 ▶sub _calculate_api_url {
Subroutine _calculate_api_url: called CC=1 SCAR=0.0
475 9 my $self = shift;
476  
477 return join q(&),
478 30 map { $self->_serialize_filter($_) }
479 90 grep { defined $self->{filters}{$_} }
480 9 sort keys %{ $self->{filters} };
481 }
482  
483 30 ▶sub _serialize_filter {
Subroutine _serialize_filter: called CC=2 SCAR=6.9
484 30 my ( $self, $filter ) = @_;
485  
486 30 my $value = $self->{filters}{$filter};
487  
488 return ref $value eq 'ARRAY'
489 30 ▶ ? join q(&), map { "$filter=$_" } sort @{ $value }
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 8 22 30
490 : $filter . q(=) . $value;
491 }
492  
493 =head2 get_all_proxies
494  
495 Return the whole proxy list
496  
497 =cut
498  
499 1 ▶sub get_all_proxies {
Subroutine get_all_proxies: called CC=1 SCAR=0.0
Pod documented
500 1 my ($self) = @_;
501  
502 1 return @{ $self->{proxy_list} };
503 }
504  
505 =head2 get_random_proxy
506  
507 Returns a proxy from the list at random (with repetition)
508  
509 =cut
510  
511 0 ▶sub get_random_proxy {
Subroutine get_random_proxy: not called CC=1 SCAR=6.9
Pod documented
512 0 my ($self) = @_;
513  
514 0 my $rand_index = int rand @{ $self->{proxy_list} };
515  
516 0 return $self->{proxy_list}[$rand_index];
517 }
518  
519 =head2 get_next
520  
521 Returns next proxy from the shuffled list (no repetition until list is exhausted)
522  
523 =cut
524  
525 10 ▶sub get_next {
Subroutine get_next: called CC=2 SCAR=6.9
Pod documented
526 10 my ($self) = @_;
527  
528 10 my $proxy = $self->{proxy_list}[ $self->{index} ];
529  
530 10 $self->{index} = $self->{index} + 1;
531  
532 10 ▶ if ( $self->{index} > @{ $self->{proxy_list} } - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"index"} > @{$self->{"proxy_list"};} - 1) 1 9 10
533 1 $self->{index} = 0;
534 }
535  
536 10 return $proxy;
537 }
538  
539 =head1 AUTHOR
540  
541 Julio de Castro, C<< <julio.dcs at gmail.com> >>
542  
543 =head1 BUGS
544  
545 Please report any bugs or feature requests to C<bug-geonode-free-proxylist at rt.cpan.org>, or through
546 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geonode-Free-ProxyList>.
547  
548 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
549  
550 =head1 SUPPORT
551  
552 You can find documentation for this module with the perldoc command.
553  
554 perldoc Geonode::Free::ProxyList
555  
556 You can also look for information at:
557  
558 =over 4
559  
560 =item * RT: CPAN's request tracker (report bugs here)
561  
562 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Geonode-Free-ProxyList>
563  
564 =item * CPAN Ratings
565  
566 L<https://cpanratings.perl.org/d/Geonode-Free-ProxyList>
567  
568 =item * Search CPAN
569  
570 L<https://metacpan.org/release/Geonode-Free-ProxyList>
571  
572 =back
573  
574  
575 =head1 ACKNOWLEDGEMENTS
576  
577  
578 =head1 LICENSE AND COPYRIGHT
579  
580 This software is Copyright (c) 2021 by Julio de Castro.
581  
582 This is free software, licensed under:
583  
584 The Artistic License 2.0 (GPL Compatible)
585  
586  
587 =cut
588  
589 2 1; # End of Geonode::Free::ProxyList
« Proxy.pm ↑ Summary