Scrappy/Action/Download.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 122 CC 20 SCAR 58.6
CC
20
Cov
0%
page
420.0
BEGIN
2.0
BEGIN
2.0
CRAP 350.3
1 package Scrappy::Action::Download;
2  
3 BEGIN {
4 $Scrappy::Action::Download::VERSION = '0.94112090';
5 }
6  
7 use URI;
8 use Moose::Role;
9 use Scrappy;
10 with 'Scrappy::Action::Help';
11  
12 sub page {
13 my ($self, @options) = @_;
14  
15 my $url = $options[0];
16 die "Can't download a page without a proper URL"
17 unless $url;
18 $url = URI->new($url)->as_string;
19  
20 my $scraper = Scrappy->new;
21 $scraper->debug(1);
22 $scraper->logger->write('download.log');
23  
24 my $downloader = {
25 '//link[@href]' => sub {
26 my ($self, $item, $params) = @_;
27 my $link =
28 ref $item->{href} ? $item->{href}->as_string : $item->{href};
29 if ($link) {
30 if ($link =~ m{^$url} || $link !~ m/^http(s)?\:\/\//) {
31  
32 $link = URI->new_abs($link, $url)->as_string
33 if $link !~ m/^http(s)?\:\/\//;
34  
35 $self->download($link);
36  
37 # assuming its a css stylesheet, lets see if we find
38 # any images that need downloading
39 # YES, ITS A HACK ... and a bad one, AHHHHHHHHHHHHHHH !!!!!!
40  
41 if ($self->get($link)->page_loaded) {
42  
43 if ( $self->worker->content_type =~ /css/
44 || $self->worker->response->filename
45 =~ /\.css(\?.*)?$/)
46 {
47  
48 if ($self->content) {
49  
50 $self->content->decode;
51 my @urls = $self->content->as_string
52 =~ /url\s{0,}?\([\'\"\s]{0,}?([^\)]+)?[\'\"\s]{0,}?\)/g;
53  
54 if (@urls) {
55  
56 # download any found urls (probably images)
57 foreach my $url (@urls) {
58 $url =~ s/^\s+//g;
59 $url =~ s/\s+$//g;
60 $url =~ s/[\'\"]//g;
61 $url !~ m/^http(s)?\:\/\//
62 ? $self->download(
63 URI->new_abs($url, $link))
64 : $self->download($url);
65 }
66 }
67 }
68 }
69 }
70 }
71 }
72 },
73 '//script[@src]' => sub {
74 my ($self, $item, $params) = @_;
75 my $script =
76 ref $item->{src} ? $item->{src}->as_string : $item->{src};
77 if ($script) {
78  
79 $script = URI->new_abs($script, $url)->as_string
80 if $script !~ m/^http(s)?\:\/\//;
81  
82 $self->download($script)
83 if $script =~ m{^$url}
84 || $script !~ m/^http(s)?\:\/\//;
85 }
86 },
87 '//img[@src]' => sub {
88 my ($self, $item, $params) = @_;
89 my $image =
90 ref $item->{src} ? $item->{src}->as_string : $item->{src};
91 if ($image) {
92  
93 $image = URI->new_abs($image, $url)->as_string
94 if $image !~ m/^http(s)?\:\/\//;
95  
96 $self->download($image)
97 if $image =~ m{^$url}
98 || $image !~ m/^http(s)?\:\/\//;
99 }
100 },
101 };
102  
103 $scraper->crawl(
104 $url,
105 '/' => $downloader,
106 '/*' => $downloader
107 );
108  
109 if ($scraper->get($url)->page_loaded) {
110  
111 my $filename = $scraper->worker->response->filename || 'index.html';
112 $scraper->store($filename);
113 return "\n... successfully downloaded $filename and it's assets\n";
114  
115 }
116  
117 return "\n... downloading may have had some trouble, see download.log\n";
118  
119 }
120  
121 1;
122  
123 __DATA__