Scrappy/Plugin/RandomProxy.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 53 CC 3 SCAR 19.5
CC
3
Cov
0%
get_proxy_list
12.0
BEGIN
2.0
use_random_proxy
2.0
CRAP 7.0
1 package Scrappy::Plugin::RandomProxy;
2  
3 BEGIN {
4 $Scrappy::Plugin::RandomProxy::VERSION = '0.94112090';
5 }
6  
7 use Moose::Role;
8  
9 has proxy_address => (
10 is => 'rw',
11 isa => 'Str'
12 );
13  
14 has proxy_protocol => (
15 is => 'rw',
16 isa => 'Str',
17 default => 'HTTP'
18 );
19  
20 has proxy_list => (
21 is => 'rw',
22 isa => 'ArrayRef',
23 default => sub { [] }
24 );
25  
26 sub get_proxy_list {
27 my $self = shift;
28  
29 # fetch list of HTTP proxies unless already gathered
30 unless ($self->proxy_list) {
31  
32 # get a proxy from http://www.hidemyass.com/proxy-list/
33 $self->user_agent->random_user_agent;
34  
35 # goto hidemyass.com
36 $self->get('http://www.hidemyass.com/proxy-list/');
37  
38 # special post to get HTTP-80 proxies only
39 my %headers = (
40 'Content-Type' => 'application/x-www-form-urlencoded',
41 'Referer' => 'http://www.hidemyass.com/proxy-list/'
42 );
43 my $form = {
44 'c[]' => [
45 'United States', 'Indonesia',
46 'China', 'Brazil',
47 'Russian Federation', 'Iran, Islamic Republic of',
48 'Germany', 'India',
49 'Korea, Republic of', 'Kazakhstan',
50 'Ukraine', 'Japan',
51 'Colombia', 'Thailand',
52 'Egypt', 'Taiwan',
53 'Spain', 'Canada',
54 'Argentina', 'Poland',
55 'Czech Republic', 'Turkey',
56 'South Africa', 'Venezuela',
57 'France', 'Netherlands',
58 'Peru', 'Vietnam',
59 'Mexico', 'Switzerland',
60 'Bulgaria', 'Kenya',
61 'Ecuador', 'Latvia',
62 'Portugal', 'Slovakia',
63 'Chile', 'Italy',
64 'Denmark', 'Moldova, Republic of',
65 'United Kingdom', 'Greece',
66 'Norway', 'Israel',
67 'Hungary', 'Nigeria',
68 'Ireland', 'Australia',
69 'Hong Kong', 'Finland',
70 'Philippines', 'United Arab Emirates',
71 'Ghana', 'Bangladesh',
72 'Lebanon', 'Romania',
73 'Syrian Arab Republic', 'Austria',
74 'Sri Lanka', 'Serbia',
75 'Sweden', 'Macau',
76 'Iraq', 'Malaysia',
77 'Macedonia', 'Malta',
78 'Jamaica', 'Niger',
79 'Belarus', 'Bahamas',
80 'Antarctica', 'Bosnia and Herzegovina',
81 'Mozambique', 'Benin',
82 'Grenada', "Cote D'Ivoire",
83 'Mauritania', 'Brunei Darussalam',
84 'Ethiopia', 'Pakistan',
85 'Chad', 'Paraguay',
86 'Puerto Rico', 'Cambodia',
87 'Singapore', 'Guatemala',
88 'Fiji', 'Croatia',
89 'Kuwait', 'Palestinian Territory',
90 'Albania', 'Zimbabwe',
91 'Bolivia', 'Maldives',
92 'Luxembourg'
93 ],
94 'pr[]' => ['0'],
95 'a[]' => ['2', '3', '4'],
96 p => '80',
97 pl => 'on',
98 s => '0',
99 o => '0',
100 pp => '3',
101 ac => 'on',
102 sortBy => 'date'
103 };
104 $self->post('http://www.hidemyass.com/proxy-list/', $form, %headers);
105  
106 my @proxies = ();
107 my $rows = $self->select('#proxylist-table tr.row')->data;
108  
109 foreach my $row (@{$rows}) {
110 my $cols = $self->select('td', $row->{html})->data;
111 my ($server, $port, $type) =
112 ($cols->[1]->{text}, 80, $cols->[6]->{text});
113 if ($type eq 'HTTP') {
114 push @proxies, "$server:$port";
115 }
116 }
117  
118 $self->proxy_list([@proxies]);
119 $self->log(
120 "info",
121 "Proxy list created from hidemyass.com",
122 proxy_list => [@proxies]
123 );
124 }
125  
126 return $self;
127 }
128  
129 sub use_random_proxy {
130 my $self = shift;
131  
132 $self->get_proxy_list;
133  
134 # select and use a random a proxy
135 my @proxies = @{$self->proxy_list};
136 my $proxy = $proxies[rand(@proxies)];
137  
138 $self->proxy_address($proxy);
139 $self->proxy('http', 'http://' . $proxy . '/');
140 $self->log("info", "Proxy has been set to $proxy using the HTTP protocol");
141  
142 return $self;
143 }
144  
145 1;