App-ManiacDownloader

 view release on metacpan or  search on metacpan

lib/App/ManiacDownloader.pm  view on Meta::CPAN

use App::ManiacDownloader::_SegmentTask;
use App::ManiacDownloader::_BytesDownloaded;
use App::ManiacDownloader::_File;

my $DEFAULT_NUM_CONNECTIONS  = 4;
my $NUM_CONN_BYTES_THRESHOLD = 4_096 * 2;

has '_finished_condvar'      => ( is  => 'rw' );
has '_ranges'                => ( isa => 'ArrayRef', is => 'rw' );
has '_remaining_connections' => ( isa => 'Int',      is => 'rw' );
has '_stats_timer'           => ( is  => 'rw' );
has '_last_timer_time'       => ( is  => 'rw', isa => 'Num' );
has '_len'                   => ( is  => 'rw', isa => 'Int' );
has '_downloaded'            => (
    is      => 'rw',
    isa     => 'App::ManiacDownloader::_BytesDownloaded',
    default => sub { return App::ManiacDownloader::_BytesDownloaded->new; }
);
has '_file' => ( is => 'rw', isa => 'App::ManiacDownloader::_File' );

sub _serialize
{

lib/App/ManiacDownloader.pm  view on Meta::CPAN


            $guard = '';
        }
    }

    return;
}

my $MAX_CHECKS = 6;

sub _handle_stats_timer
{
    my ($self) = @_;

    my ( $num_dloaded, $total_downloaded ) =
        $self->_downloaded->_flush_and_report;

    my $_ranges = $self->_ranges;
    for my $idx ( 0 .. $#$_ranges )
    {
        my $r = $_ranges->[$idx];

        $r->_flush_and_report;
        if ( $r->is_active && $r->_increment_check_count($MAX_CHECKS) )
        {
            $r->_guard('');
            $self->_start_connection($idx);
        }
    }

    my $time      = AnyEvent->now;
    my $last_time = $self->_last_timer_time;

    printf "Downloaded %i%% (Currently: %.2fKB/s)\r",
        int( $total_downloaded * 100 / $self->_len ),
        ( $num_dloaded / ( 1024 * ( $time - $last_time ) ) ),
        ;
    STDOUT->flush;

    $self->_last_timer_time($time);

    return;
}

sub _slurp
{
    my $filename = shift;

    open my $in, '<', $filename
        or die "Cannot open '$filename' for slurping - $!";

lib/App/ManiacDownloader.pm  view on Meta::CPAN

                            $url_basename,
                        )
                    )
                );
            }

            $self->_start_connection($idx);
        }
    }

    my $timer = AnyEvent->timer(
        after    => 3,
        interval => 3,
        cb       => sub {
            $self->_handle_stats_timer;
            return;
        },
    );
    $self->_last_timer_time( AnyEvent->time() );
    $self->_stats_timer($timer);

    {
        no autodie;
        unlink( $self->_file->_resume_info_path() );
    }

    return;
}

sub _abort_signal_handler

lib/App/ManiacDownloader.pm  view on Meta::CPAN

                    $num_connections );
            };
        }
    }

    my $signal_handler = sub { $self->_abort_signal_handler(); };
    local $SIG{INT}  = $signal_handler;
    local $SIG{TERM} = $signal_handler;

    $self->_finished_condvar->recv;
    $self->_stats_timer( undef() );

    if ( !$self->_remaining_connections() )
    {
        rename( $self->_file->_downloading_path(),
            $self->_file->_url_basename() );
    }

    return;
}

lib/App/ManiacDownloader/_BytesDownloaded.pm  view on Meta::CPAN

package App::ManiacDownloader::_BytesDownloaded;
$App::ManiacDownloader::_BytesDownloaded::VERSION = '0.0.13';
use strict;
use warnings;

use MooX qw/late/;

use List::Util qw/min/;

has [ '_bytes_dled', '_bytes_dled_last_timer' ] =>
    ( isa => 'Int', is => 'rw', default => sub { return 0; } );

has '_stale_checkpoints_count' => (
    isa     => 'Int',
    is      => 'rw',
    default => sub { return 0; }
);

sub _add
{

lib/App/ManiacDownloader/_BytesDownloaded.pm  view on Meta::CPAN

{
    my ( $self, $MAX_COUNT ) = @_;

    return ( $self->_stale_checkpoints_count >= $MAX_COUNT );
}

sub _flush_and_report
{
    my $self = shift;

    my $difference = $self->_bytes_dled - $self->_bytes_dled_last_timer;

    if ( $difference > 0 )
    {
        $self->_stale_checkpoints_count(0);
    }
    else
    {
        $self->_stale_checkpoints_count( $self->_stale_checkpoints_count + 1 );
    }

    $self->_bytes_dled_last_timer( $self->_bytes_dled );

    return ( $difference, $self->_bytes_dled );
}

sub _my_init
{
    my ( $self, $num_bytes ) = @_;

    $self->_bytes_dled($num_bytes);
    $self->_bytes_dled_last_timer($num_bytes);

    return;
}

1;

__END__

=pod



( run in 1.037 second using v1.01-cache-2.11-cpan-49f99fa48dc )