| 1 | package WWW::LargeFileFetcher; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | WWW::LargeFileFetcher - a module used to fetch large files from internet. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Version 0.02 | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use WWW::LargeFileFetcher; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my $fetcher = WWW::LargeFileFetcher->new(); | ||||||||||||||||||||||||||||
| 23 | $fetcher->get('http://www.example.com/video.rm', 'video.rm'); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | C<WWW::LargeFileFetcher> is used to fetch large files (like | ||||||||||||||||||||||||||||
| 28 | videos, audios) from internet. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 METHODS | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =over | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =item $fetcher = WWW::LargeFileFetcher->new(%opinions); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | The opinions hash can be: | ||||||||||||||||||||||||||||
| 37 | agent=>'the agent string', | ||||||||||||||||||||||||||||
| 38 | timeout=>time in seconds, | ||||||||||||||||||||||||||||
| 39 | proxy=>'http proxy to be used' | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =item $fetcher->get($url,$filename); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | The return value can be: | ||||||||||||||||||||||||||||
| 44 | 1 : success | ||||||||||||||||||||||||||||
| 45 | -1 : IO error | ||||||||||||||||||||||||||||
| 46 | -2 : internet access error | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | The detailed error string can be accessed from $fetcher->err_str(); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =item $fetcher->test($url); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | This method is used to test whether the $url is downloadable. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =item $fetcher->err_str(); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Return the detail description of the error occured. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =back | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Zhang Jun, C<< <jzhang533 at gmail.com> >> | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Copyright 2007 Zhang Jun, all rights reserved. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 69 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Constructor new | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 0 | my $proto = shift; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | my $self = {}; | |||||||||||||||||||||||||||
| 83 | 0 | bless($self, $class); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Run initialisation code | ||||||||||||||||||||||||||||
| 86 | 0 | return $self->_init(@_); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub _init{ | ||||||||||||||||||||||||||
|
Subroutine
_init: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 90 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | if (@_ != 0) { # We are expecting our configuration to come as an anonymous hash | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if (ref $_[0] eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | my $hash=$_[0]; | |||||||||||||||||||||||||||
| 94 | 0 | foreach my $key (keys %$hash) { | |||||||||||||||||||||||||||
| 95 | 0 | $self->{lc($key)}=$hash->{$key}; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | }else { # Using a more conventional named args | ||||||||||||||||||||||||||||
| 98 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 99 | 0 | foreach my $key (keys %args) { | |||||||||||||||||||||||||||
| 100 | 0 | $self->{lc($key)}=$args{$key}; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | my $ua = LWP::UserAgent->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | keep_alive => 1 | ||||||||||||||||||||||||||||
| 107 | ) or return undef; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if(exists $self->{'timeout'}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | $ua->timeout($self->{'timeout'}); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if(exists $self->{'proxy'}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | $ua->proxy('http',$self->{'proxy'}); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if(exists $self->{'agent'}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | $ua->agent($self->{'agent'}); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | $self->{'ua'}=$ua; | |||||||||||||||||||||||||||
| 120 | 0 | $self->{err_str}=''; | |||||||||||||||||||||||||||
| 121 | 0 | $self->{err_code}=1; | |||||||||||||||||||||||||||
| 122 | 0 | return $self; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub err_str{ | ||||||||||||||||||||||||||
|
Subroutine
err_str: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | 0 | return $_[0]->{err_str}; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # input: url, file | ||||||||||||||||||||||||||||
| 130 | # return: 1 for success, -1 for IO error, -2 for internet access error | ||||||||||||||||||||||||||||
| 131 | # url is stored into file | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub get{ | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 0 | my ($self,$url,$file)=@_; | |||||||||||||||||||||||||||
| 134 | 0 | $self->{err_code}=1; | |||||||||||||||||||||||||||
| 135 | 0 | my $set=0; | |||||||||||||||||||||||||||
| 136 | 0 | $|++; | |||||||||||||||||||||||||||
| 137 | my $res = $self->{'ua'}->get($url, ':content_cb'=> | ||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | unless ($set){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | unless (open(FILE, ">$file") ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | $self->{err_str} = "error, Can't open $file: $!\n"; | |||||||||||||||||||||||||||
| 142 | 0 | $self->{err_code}=-1; | |||||||||||||||||||||||||||
| 143 | 0 | die; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 0 | binmode FILE; | |||||||||||||||||||||||||||
| 146 | 0 | $set = 1; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | unless( print FILE $_[0] ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | $self->{err_str} = "error, Can't write to $file: $!\n"; | |||||||||||||||||||||||||||
| 150 | 0 | $self->{err_code}=-1; | |||||||||||||||||||||||||||
| 151 | 0 | die; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 0 | ); | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if (fileno(FILE)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | unless( close(FILE) ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | $self->{err_str} = "error, Can't write to $file: $!\n"; | |||||||||||||||||||||||||||
| 159 | 0 | $self->{err_code}=-1; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if($self->{err_code} == -1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | unlink($file); | |||||||||||||||||||||||||||
| 165 | 0 | return -1; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | if( defined($res) && $res->code == 200 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $res and $res->code == 200
MC/DC0%0/2
defined $res and $res->code == 200
defined $res
$res->code == 200
Truth table0%0/3
A and B
A= defined $resB= $res->code == 200
| |||||||||||||||||||||||||||||
| 169 | 0 | return 1; | |||||||||||||||||||||||||||
| 170 | }else{ | ||||||||||||||||||||||||||||
| 171 | 0 | $self->{err_str} = "error, ". $res->status_line()."\n"; | |||||||||||||||||||||||||||
| 172 | 0 | $self->{err_code}=-2; | |||||||||||||||||||||||||||
| 173 | 0 | return -2; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # test if the url can be downloaded, not really download it | ||||||||||||||||||||||||||||
| 179 | # input: url | ||||||||||||||||||||||||||||
| 180 | # return: 1 for success, -1 for IO error, -2 for internet access error | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub test{ | ||||||||||||||||||||||||||
|
Subroutine
test: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 182 | 0 | my ($self,$url)=@_; | |||||||||||||||||||||||||||
| 183 | 0 | $self->{err_code}=1; | |||||||||||||||||||||||||||
| 184 | my $res = $self->{'ua'}->get($url, ':content_cb'=> | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 186 | 0 | die; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 0 | ); | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | if( defined($res) && $res->code == 200 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $res and $res->code == 200
MC/DC0%0/2
defined $res and $res->code == 200
defined $res
$res->code == 200
Truth table0%0/3
A and B
A= defined $resB= $res->code == 200
| |||||||||||||||||||||||||||||
| 191 | 0 | return 1; | |||||||||||||||||||||||||||
| 192 | }else{ | ||||||||||||||||||||||||||||
| 193 | 0 | $self->{err_str} = "error, ". $res->status_line()."\n"; | |||||||||||||||||||||||||||
| 194 | 0 | $self->{err_code}=-2; | |||||||||||||||||||||||||||
| 195 | 0 | return -2; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 2 | 1; # End of WWW::LargeFileFetcher | |||||||||||||||||||||||||||