| 1 | package App::ManiacDownloader; | ||||||||||||||||||||||||||||
| 2 | 2 | $App::ManiacDownloader::VERSION = '0.0.13'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use 5.012; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use MooX qw/late/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use AnyEvent::HTTP qw/http_head http_get/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use AnyEvent::FTP::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Getopt::Long qw/GetOptionsFromArray/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Fcntl qw( SEEK_SET ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use List::UtilsBy qw(max_by); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use JSON::MaybeXS qw(decode_json encode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use App::ManiacDownloader::_SegmentTask; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use App::ManiacDownloader::_BytesDownloaded; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use App::ManiacDownloader::_File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | my $DEFAULT_NUM_CONNECTIONS = 4; | |||||||||||||||||||||||||||
| 24 | 2 | my $NUM_CONN_BYTES_THRESHOLD = 4_096 * 2; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | has '_finished_condvar' => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 27 | 2 | has '_ranges' => ( isa => 'ArrayRef', is => 'rw' ); | |||||||||||||||||||||||||||
| 28 | 2 | has '_remaining_connections' => ( isa => 'Int', is => 'rw' ); | |||||||||||||||||||||||||||
| 29 | 2 | has '_stats_timer' => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 30 | 2 | has '_last_timer_time' => ( is => 'rw', isa => 'Num' ); | |||||||||||||||||||||||||||
| 31 | 2 | has '_len' => ( is => 'rw', isa => 'Int' ); | |||||||||||||||||||||||||||
| 32 | has '_downloaded' => ( | ||||||||||||||||||||||||||||
| 33 | is => 'rw', | ||||||||||||||||||||||||||||
| 34 | isa => 'App::ManiacDownloader::_BytesDownloaded', | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | default => sub { return App::ManiacDownloader::_BytesDownloaded->new; } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 36 | 2 | ); | |||||||||||||||||||||||||||
| 37 | 2 | has '_file' => ( is => 'rw', isa => 'App::ManiacDownloader::_File' ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub _serialize | ||||||||||||||||||||||||||||
| 40 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_serialize: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 41 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | return +{ | ||||||||||||||||||||||||||||
| 44 | 0 | _ranges => [ map { $_->_serialize() } @{ $self->_ranges } ], | |||||||||||||||||||||||||||
| 45 | _remaining_connections => $self->_remaining_connections, | ||||||||||||||||||||||||||||
| 46 | _bytes_dled => $self->_downloaded->_total_downloaded, | ||||||||||||||||||||||||||||
| 47 | _len => $self->_len, | ||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub _start_connection | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_start_connection: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 53 | 0 | my ( $self, $idx ) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | my $r = $self->_ranges->[$idx]; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | sysseek( $r->_fh, $r->_start, SEEK_SET ); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | my $is_ftp = $self->_file->_is_ftp; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # We do these to make sure the cancellation guard does not get | ||||||||||||||||||||||||||||
| 62 | # preserved because it's in the context of the closures. | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | my $on_body = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 64 | 0 | my ( $active_seq, $data, $hdr ) = @_; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Stale or wrong connection - probably AnyEvent::FTP::Client after a | ||||||||||||||||||||||||||||
| 67 | # quit. | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( ( !$r->_is_right_active_seq($active_seq) ) or ( !$r->is_active ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$r->_is_right_active_seq($active_seq) and $r->is_active
MC/DC0%0/2
$r->_is_right_active_seq($active_seq) and $r->is_active
$r->_is_right_active_seq($active_seq)
$r->is_active
Truth table0%0/3
A and B
A= $r->_is_right_active_seq($active_seq)B= $r->is_active
| |||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | 0 | return; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my $ret = $r->_write_data( \$data ); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | $self->_downloaded->_add( $ret->{num_written} ); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $cont = $ret->{should_continue}; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ( !$cont ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | { | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($is_ftp) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | 0 | $r->_guard->quit; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $largest_r = max_by { $r->_num_remaining } @{ $self->_ranges }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ( $largest_r->_num_remaining < $NUM_CONN_BYTES_THRESHOLD ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 0 | $r->_close; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | not $self->_remaining_connections( | ||||||||||||||||||||||||||||
| 90 | $self->_remaining_connections() - 1 | ||||||||||||||||||||||||||||
| 91 | ) | ||||||||||||||||||||||||||||
| 92 | ) | ||||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||
| 94 | 0 | $self->_finished_condvar->send; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else | ||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 0 | $largest_r->_split_into($r); | |||||||||||||||||||||||||||
| 100 | 0 | $self->_start_connection($idx); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 0 | return $cont; | |||||||||||||||||||||||||||
| 104 | 0 | }; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my $final_cb = sub { return; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my $url = $self->_file->_url; | |||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 0 | my $active_seq = $r->_get_next_active_seq; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $seq_on_body = sub { return $on_body->( $active_seq, @_ ); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ($is_ftp) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||
| 116 | 0 | my $ftp = AnyEvent::FTP::Client->new( passive => 1 ); | |||||||||||||||||||||||||||
| 117 | 0 | $r->_guard($ftp); | |||||||||||||||||||||||||||
| 118 | $ftp->connect( $url->host, $url->port )->cb( | ||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 120 | $ftp->login( $url->user, $url->password )->cb( | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 122 | $ftp->type('I')->cb( | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 124 | 0 | $ftp->retr( $self->_file->_url_path, | |||||||||||||||||||||||||||
| 125 | $seq_on_body, restart => $r->_start, ); | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | ); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 0 | ); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 0 | ); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | else | ||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 0 | my $guard = http_get $url, | |||||||||||||||||||||||||||
| 136 | headers => | ||||||||||||||||||||||||||||
| 137 | { 'Range' => sprintf( "bytes=%d-%d", $r->_start, $r->_end - 1 ) | ||||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||
| 139 | on_body => $seq_on_body, | ||||||||||||||||||||||||||||
| 140 | $final_cb; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | $r->_guard($guard); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | $guard = ''; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | return; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 2 | my $MAX_CHECKS = 6; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | sub _handle_stats_timer | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_handle_stats_timer: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 155 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | my ( $num_dloaded, $total_downloaded ) = | |||||||||||||||||||||||||||
| 158 | $self->_downloaded->_flush_and_report; | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | my $_ranges = $self->_ranges; | |||||||||||||||||||||||||||
| 161 | 0 | for my $idx ( 0 .. $#$_ranges ) | |||||||||||||||||||||||||||
| 162 | { | ||||||||||||||||||||||||||||
| 163 | 0 | my $r = $_ranges->[$idx]; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | $r->_flush_and_report; | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ( $r->is_active && $r->_increment_check_count($MAX_CHECKS) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$r->is_active and $r->_increment_check_count($MAX_CHECKS)
MC/DC0%0/2
$r->is_active and $r->_increment_check_count($MAX_CHECKS)
$r->is_active
$r->_increment_check_count($MAX_CHECKS)
Truth table0%0/3
A and B
A= $r->is_activeB= $r->_increment_check_count($MAX_CHECKS)
| |||||||||||||||||||||||||||||
| 167 | { | ||||||||||||||||||||||||||||
| 168 | 0 | $r->_guard(''); | |||||||||||||||||||||||||||
| 169 | 0 | $self->_start_connection($idx); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | my $time = AnyEvent->now; | |||||||||||||||||||||||||||
| 174 | 0 | my $last_time = $self->_last_timer_time; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | printf "Downloaded %i%% (Currently: %.2fKB/s)\r", | |||||||||||||||||||||||||||
| 177 | int( $total_downloaded * 100 / $self->_len ), | ||||||||||||||||||||||||||||
| 178 | ( $num_dloaded / ( 1024 * ( $time - $last_time ) ) ), | ||||||||||||||||||||||||||||
| 179 | ; | ||||||||||||||||||||||||||||
| 180 | 0 | STDOUT->flush; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | $self->_last_timer_time($time); | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | return; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | sub _slurp | ||||||||||||||||||||||||||||
| 188 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_slurp: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 189 | 0 | my $filename = shift; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | ▶ | open my $in, '<', $filename | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | or die "Cannot open '$filename' for slurping - $!"; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | local $/; | |||||||||||||||||||||||||||
| 195 | 0 | my $contents = <$in>; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | close($in); | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | return $contents; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 2 | ▶ | use Fcntl qw( O_CREAT O_RDWR ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | sub _open_fh_for_read_write_without_clobbering | ||||||||||||||||||||||||||||
| 205 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_open_fh_for_read_write_without_clobbering: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 206 | 0 | my ( $path, $url_basename ) = @_; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | # open with '+>:raw' will clobber the file. | ||||||||||||||||||||||||||||
| 209 | # On the other hand, open with '+<:raw' won't create a new file if it | ||||||||||||||||||||||||||||
| 210 | # does not exist. | ||||||||||||||||||||||||||||
| 211 | # So we have to restort to this. | ||||||||||||||||||||||||||||
| 212 | # | ||||||||||||||||||||||||||||
| 213 | # For more information, see: http://perldoc.perl.org/perlopentut.html | ||||||||||||||||||||||||||||
| 214 | # | ||||||||||||||||||||||||||||
| 215 | # And: | ||||||||||||||||||||||||||||
| 216 | # | ||||||||||||||||||||||||||||
| 217 | # http://blogs.perl.org/users/shlomi_fish/2014/01/tech-tip-opening-a-file-for-readwrite-without-clobbering-it.html | ||||||||||||||||||||||||||||
| 218 | # | ||||||||||||||||||||||||||||
| 219 | # Thanks to Steven Haryanto for the better tip. | ||||||||||||||||||||||||||||
| 220 | # | ||||||||||||||||||||||||||||
| 221 | 0 | my $fh; | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | sysopen( $fh, $path, O_RDWR | O_CREAT ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | or die "${url_basename}: $!"; | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | return $fh; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | sub _with_len_and_num_connections | ||||||||||||||||||||||||||||
| 229 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_with_len_and_num_connections: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 230 | 0 | my ( $self, $len, $num_connections ) = @_; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | ▶ | if ( !defined($len) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | { | ||||||||||||||||||||||||||||
| 234 | 0 | die "Cannot find a content-length header."; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 0 | $self->_len($len); | |||||||||||||||||||||||||||
| 238 | 0 | $self->_remaining_connections($num_connections); | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 0 | return $self->_init_from_len( | |||||||||||||||||||||||||||
| 241 | { | ||||||||||||||||||||||||||||
| 242 | num_connections => $num_connections, | ||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | ); | ||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | sub _init_from_len | ||||||||||||||||||||||||||||
| 248 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_init_from_len: not called CC=4 SCAR=27.8
| |||||||||||||||||||||||||||||
| 249 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | my $num_connections = $args->{num_connections}; | |||||||||||||||||||||||||||
| 252 | 0 | my $len = $self->_len; | |||||||||||||||||||||||||||
| 253 | 0 | my $url_basename = $self->_file->_url_basename; | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 0 | my @stops = ( map { int( ( $len * $_ ) / $num_connections ) } | |||||||||||||||||||||||||||
| 256 | 0 .. ( $num_connections - 1 ) ); | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 0 | push @stops, $len; | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | my @ranges = ( | ||||||||||||||||||||||||||||
| 261 | map { | ||||||||||||||||||||||||||||
| 262 | 0 | App::ManiacDownloader::_SegmentTask->new( | |||||||||||||||||||||||||||
| 263 | _start => $stops[$_], | ||||||||||||||||||||||||||||
| 264 | _end => $stops[ $_ + 1 ], | ||||||||||||||||||||||||||||
| 265 | ) | ||||||||||||||||||||||||||||
| 266 | } 0 .. ( $num_connections - 1 ) | ||||||||||||||||||||||||||||
| 267 | ); | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 0 | $self->_ranges( \@ranges ); | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 0 | my $ranges_ref = $args->{ranges}; | |||||||||||||||||||||||||||
| 272 | 0 | foreach my $idx ( 0 .. $num_connections - 1 ) | |||||||||||||||||||||||||||
| 273 | { | ||||||||||||||||||||||||||||
| 274 | 0 | my $r = $ranges[$idx]; | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | ▶ | if ( defined($ranges_ref) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | { | ||||||||||||||||||||||||||||
| 278 | 0 | $r->_deserialize( $ranges_ref->[$idx] ); | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 0 | ▶ | if ( $r->is_active ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | { | ||||||||||||||||||||||||||||
| 283 | { | ||||||||||||||||||||||||||||
| 284 | 0 | $r->_fh( | |||||||||||||||||||||||||||
| 285 | scalar( | ||||||||||||||||||||||||||||
| 286 | _open_fh_for_read_write_without_clobbering( | ||||||||||||||||||||||||||||
| 287 | $self->_file->_downloading_path(), | ||||||||||||||||||||||||||||
| 288 | $url_basename, | ||||||||||||||||||||||||||||
| 289 | ) | ||||||||||||||||||||||||||||
| 290 | ) | ||||||||||||||||||||||||||||
| 291 | ); | ||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 0 | $self->_start_connection($idx); | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | my $timer = AnyEvent->timer( | ||||||||||||||||||||||||||||
| 299 | after => 3, | ||||||||||||||||||||||||||||
| 300 | interval => 3, | ||||||||||||||||||||||||||||
| 301 | 0 | ▶ | cb => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 302 | 0 | $self->_handle_stats_timer; | |||||||||||||||||||||||||||
| 303 | 0 | return; | |||||||||||||||||||||||||||
| 304 | }, | ||||||||||||||||||||||||||||
| 305 | 0 | ); | |||||||||||||||||||||||||||
| 306 | 0 | $self->_last_timer_time( AnyEvent->time() ); | |||||||||||||||||||||||||||
| 307 | 0 | $self->_stats_timer($timer); | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | { | ||||||||||||||||||||||||||||
| 310 | 0 | ▶ | no autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 311 | 0 | unlink( $self->_file->_resume_info_path() ); | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 0 | return; | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | sub _abort_signal_handler | ||||||||||||||||||||||||||||
| 318 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_abort_signal_handler: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 319 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 0 | open my $json_out_fh, '>:encoding(utf8)', $self->_file->_resume_info_path(); | |||||||||||||||||||||||||||
| 322 | 0 | print {$json_out_fh} encode_json( $self->_serialize ); | |||||||||||||||||||||||||||
| 323 | 0 | close($json_out_fh); | |||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 0 | exit(2); | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | sub run | ||||||||||||||||||||||||||||
| 329 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 330 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | my $num_connections = $DEFAULT_NUM_CONNECTIONS; | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 0 | my @argv = @{ $args->{argv} }; | |||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | !GetOptionsFromArray( | ||||||||||||||||||||||||||||
| 338 | \@argv, 'k|num-connections=i' => \$num_connections, | ||||||||||||||||||||||||||||
| 339 | ) | ||||||||||||||||||||||||||||
| 340 | ) | ||||||||||||||||||||||||||||
| 341 | { | ||||||||||||||||||||||||||||
| 342 | 0 | die "Cannot parse argv - $!"; | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 0 | ▶ | my $url_s = shift(@argv) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | or die "No url given."; | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 0 | $self->_file( App::ManiacDownloader::_File->new ); | |||||||||||||||||||||||||||
| 349 | 0 | $self->_file->_set_url($url_s); | |||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 0 | ▶ | if ( -e $self->_file->_url_basename ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | { | ||||||||||||||||||||||||||||
| 353 | 0 | print STDERR | |||||||||||||||||||||||||||
| 354 | "File appears to have already been downloaded. Quitting.\n"; | ||||||||||||||||||||||||||||
| 355 | 0 | return; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 0 | $self->_finished_condvar( scalar( AnyEvent->condvar ) ); | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 0 | ▶ | if ( -e $self->_file->_resume_info_path ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | { | ||||||||||||||||||||||||||||
| 362 | 0 | my $record = decode_json( _slurp( $self->_file->_resume_info_path ) ); | |||||||||||||||||||||||||||
| 363 | 0 | $self->_len( $record->{_len} ); | |||||||||||||||||||||||||||
| 364 | 0 | $self->_downloaded->_my_init( $record->{_bytes_dled} ); | |||||||||||||||||||||||||||
| 365 | 0 | $self->_remaining_connections( $record->{_remaining_connections} ); | |||||||||||||||||||||||||||
| 366 | 0 | my $ranges_ref = $record->{_ranges}; | |||||||||||||||||||||||||||
| 367 | 0 | $self->_init_from_len( | |||||||||||||||||||||||||||
| 368 | { | ||||||||||||||||||||||||||||
| 369 | ranges => $ranges_ref, | ||||||||||||||||||||||||||||
| 370 | num_connections => scalar(@$ranges_ref), | ||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | ); | ||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | else | ||||||||||||||||||||||||||||
| 375 | { | ||||||||||||||||||||||||||||
| 376 | 0 | my $url = $self->_file->_url; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 0 | ▶ | if ( $self->_file->_is_ftp ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | { | ||||||||||||||||||||||||||||
| 380 | 0 | my $ftp = AnyEvent::FTP::Client->new( passive => 1 ); | |||||||||||||||||||||||||||
| 381 | 0 | $ftp->connect( $url->host, $url->port )->recv; | |||||||||||||||||||||||||||
| 382 | 0 | $ftp->login( $url->user, $url->password )->recv; | |||||||||||||||||||||||||||
| 383 | 0 | $ftp->type('I')->recv; | |||||||||||||||||||||||||||
| 384 | $ftp->size( $self->_file->_url_path )->cb( | ||||||||||||||||||||||||||||
| 385 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 386 | 0 | my $len = shift->recv; | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 0 | $ftp->quit; | |||||||||||||||||||||||||||
| 389 | 0 | undef($ftp); | |||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 0 | return $self->_with_len_and_num_connections( $len, | |||||||||||||||||||||||||||
| 392 | $num_connections ); | ||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | 0 | ); | |||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | else | ||||||||||||||||||||||||||||
| 397 | { | ||||||||||||||||||||||||||||
| 398 | 0 | ▶ | http_head $url, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 399 | 0 | my ( undef, $headers ) = @_; | |||||||||||||||||||||||||||
| 400 | 0 | my $len = $headers->{'content-length'}; | |||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 0 | return $self->_with_len_and_num_connections( $len, | |||||||||||||||||||||||||||
| 403 | $num_connections ); | ||||||||||||||||||||||||||||
| 404 | 0 | }; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 0 | ▶ | my $signal_handler = sub { $self->_abort_signal_handler(); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 409 | 0 | local $SIG{INT} = $signal_handler; | |||||||||||||||||||||||||||
| 410 | 0 | local $SIG{TERM} = $signal_handler; | |||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 0 | $self->_finished_condvar->recv; | |||||||||||||||||||||||||||
| 413 | 0 | $self->_stats_timer( undef() ); | |||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 0 | ▶ | if ( !$self->_remaining_connections() ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 416 | { | ||||||||||||||||||||||||||||
| 417 | 0 | rename( $self->_file->_downloading_path(), | |||||||||||||||||||||||||||
| 418 | $self->_file->_url_basename() ); | ||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 0 | return; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 2 | 1; | |||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | __END__ | ||||||||||||||||||||||||||||