From 53f13c7d8bc638de5b500d8e0d523b40cce59652 Mon Sep 17 00:00:00 2001 From: Kovensky Date: Mon, 8 Mar 2010 16:36:41 -0300 Subject: [PATCH] Run through perltidy --- emiya.pl | 446 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 259 insertions(+), 187 deletions(-) diff --git a/emiya.pl b/emiya.pl index 7657bd8..b10b004 100755 --- a/emiya.pl +++ b/emiya.pl @@ -22,41 +22,41 @@ printout("Emiya $VERSION - <3 Saber\n"); printout("Loading settings... "); -if (!file_exists("emiya.conf")) { +if ( !file_exists("emiya.conf") ) { my $settings = { username => "PR", password => "", - proxy => "", - move_to => "Done", - allow => { + proxy => "", + move_to => "Done", + allow => { title => [ '\[Group\] Series - \d{1,3}v?\d? \([0-9A-Fa-f]{8}\)\.ext', '\[Group\] Series - EpNNNvN \(CRC32\)\.ext', 'Batch_Folder_Name', ], }, - deny => { - title => [], - }, + deny => { title => [], }, max_display_length => "80", - DELETE_ME => "DELETE ME" + DELETE_ME => "DELETE ME" }; - DumpFile("emiya.conf", $settings); + DumpFile( "emiya.conf", $settings ); printout("Sample configuration saved on 'emiya.conf'.\n"); printout("Edit it and then re-run this script.\n"); exit 5; } -my $settings = LoadFile("emiya.conf") || die "emiya.conf is invalid, delete it and re-run $0"; +my $settings = LoadFile("emiya.conf") + || die "emiya.conf is invalid, delete it and re-run $0"; -die "Default configuration detected. Edit emiya.conf and re-run this script" if $settings->{DELETE_ME}; +die "Default configuration detected. Edit emiya.conf and re-run this script" + if $settings->{DELETE_ME}; my $username = $settings->{username}; my $password = $settings->{password}; my $proxy = $settings->{proxy} || $ENV{http_proxy}; my $maxlen = $settings->{max_display_length} || 80; -my $move_to = $settings->{move_to}; +my $move_to = $settings->{move_to}; # FILTERS @@ -68,19 +68,19 @@ my $move_to = $settings->{move_to}; # Use \d{1,3}v?\d? for episode numbers (catches v2s and v3s as well) # Remember to escape all '.', '[', ']', '(' and ')' on filenames with a '\'. -my %file_allow_filters = %{$settings->{allow}}; -my %file_deny_filters = %{$settings->{deny}}; +my %file_allow_filters = %{ $settings->{allow} }; +my %file_deny_filters = %{ $settings->{deny} }; printout("Compiling regexps... "); # Compile regexps -foreach (keys %file_allow_filters) { - foreach (@{$file_allow_filters{$_}}) { +foreach ( keys %file_allow_filters ) { + foreach ( @{ $file_allow_filters{$_} } ) { $_ = qr/$_/; } } -foreach (keys %file_deny_filters) { - foreach (@{$file_deny_filters{$_}}) { +foreach ( keys %file_deny_filters ) { + foreach ( @{ $file_deny_filters{$_} } ) { $_ = qr/$_/; } } @@ -88,11 +88,11 @@ foreach (keys %file_deny_filters) { printout("OK\n"); # HTTP Digest auth hack -my $realm = "DeathWolf's Auto-Torrent Loving Realm"; -my $A1 = md5_hex("$username:$realm:$password"); -my $A2 = md5_hex("GET:/~DeathWolf/Recent/Latest-Files.html"); -my $cnonce = int(time()); -my $reqid = 1; +my $realm = "DeathWolf's Auto-Torrent Loving Realm"; +my $A1 = md5_hex("$username:$realm:$password"); +my $A2 = md5_hex("GET:/~DeathWolf/Recent/Latest-Files.html"); +my $cnonce = int( time() ); +my $reqid = 1; sub make_digest { my $www_auth = shift; @@ -113,7 +113,7 @@ use constant CHUNK_SIZE => 16384; { my $p = $proxy; - $p = undef if (!defined($proxy) or $proxy =~ m!^socks://!); + $p = undef if ( !defined($proxy) or $proxy =~ m!^socks://! ); POE::Component::Client::HTTP->spawn( Agent => 'Emiya/0.2', Alias => 'ua', @@ -131,20 +131,20 @@ sub entity_clean { } sub shorten { - my ($str, $maxlen) = @_; + my ( $str, $maxlen ) = @_; return $str if length($str) <= $maxlen; return undef if $maxlen < 3; return "..." if $maxlen == 3; - return substr($str, 0, 1) . "..." if $maxlen == 4; + return substr( $str, 0, 1 ) . "..." if $maxlen == 4; - my $prelen = ($maxlen-3)/2; - my $postlen = ($maxlen-3) - $prelen; + my $prelen = ( $maxlen - 3 )/2; + my $postlen = ( $maxlen - 3 ) - $prelen; my $toggle = 0; - while($prelen > int($prelen)) { - if (($toggle = !$toggle)) { + while ( $prelen > int($prelen) ) { + if ( ( $toggle = !$toggle ) ) { $prelen -= 1/2; $prelen++; } else { @@ -153,82 +153,98 @@ sub shorten { } } - return substr($str, 0, $prelen) . "..." . substr($str, -$postlen); + return substr( $str, 0, $prelen ) . "..." . substr( $str, -$postlen ); } sub shorten_path { - my ($path, $maxlen) = @_; - if (length($path) > $maxlen) { - my @paths = split('/', $path); - if (scalar(@paths) < 2) { - $path = shorten($path, $maxlen); + my ( $path, $maxlen ) = @_; + if ( length($path) > $maxlen ) { + my @paths = split( '/', $path ); + if ( scalar(@paths) < 2 ) { + $path = shorten( $path, $maxlen ); } else { $path = ""; - $maxlen -= length($path = shift(@paths) . "/") if length($paths[0]) < $maxlen/2; + $maxlen -= length( $path = shift(@paths) . "/" ) + if length( $paths[0] ) < $maxlen/2; my $count = scalar(@paths); - my $len = int($maxlen / $count); + my $len = int( $maxlen/$count ); my $extra = int($maxlen) % $count; - my $last = pop @paths; + my $last = pop @paths; foreach (@paths) { - $path .= shorten("$_", $len - 1) . '/'; + $path .= shorten( "$_", $len - 1 ) . '/'; } - $path .= shorten($last, $len + $extra); + $path .= shorten( $last, $len + $extra ); } } - return $path + return $path; } POE::Session->create( inline_states => { + # Makes initial request for the Digest authentication _start => sub { - my ($kernel, $heap) = @_[KERNEL, HEAP]; - - my $req = HTTP::Request->new(GET => 'http://saber.kawaii-shoujo.net/~DeathWolf/Recent/Latest-Files.html'); + my ( $kernel, $heap ) = @_[ KERNEL, HEAP ]; + + my $req + = HTTP::Request->new( GET => + 'http://saber.kawaii-shoujo.net/~DeathWolf/Recent/Latest-Files.html' + ); $heap->{progress_message} = "Requesting List..."; - printout($heap->{progress_message} . " "); - $kernel->post('ua', 'request', 'authenticate', $req, 0); + printout( $heap->{progress_message} . " " ); + $kernel->post( 'ua', 'request', 'authenticate', $req, 0 ); }, _stop => sub { my ($kernel) = $_[KERNEL]; - $kernel->post('ua', 'shutdown'); + $kernel->post( 'ua', 'shutdown' ); }, + # Authenticates and then actually fetch the list authenticate => sub { my $res = $_[ARG1]->[0]; - return if defined $_[ARG1]->[1]; # Wait until the error page finishes downloading because of streaming - my $req = HTTP::Request->new(GET => 'http://saber.kawaii-shoujo.net/~DeathWolf/Recent/Latest-Files.html'); - die $res->status_line unless ($res->status_line =~ /^(401|2)/); - unless ($res->is_success) { - my $header = make_digest($res->header('WWW-Authenticate')); - $req->header('Authorization', $header); + return + if defined $_[ARG1]->[1] + ; # Wait until the error page finishes downloading because of streaming + my $req + = HTTP::Request->new( GET => + 'http://saber.kawaii-shoujo.net/~DeathWolf/Recent/Latest-Files.html' + ); + die $res->status_line unless ( $res->status_line =~ /^(401|2)/ ); + unless ( $res->is_success ) { + my $header = make_digest( $res->header('WWW-Authenticate') ); + $req->header( 'Authorization', $header ); } - $_[KERNEL]->post('ua', 'request', 'stream_latest_files', $req, $reqid++, 'progress'); + $_[KERNEL]->post( 'ua', 'request', 'stream_latest_files', $req, + $reqid++, 'progress' ); }, progress => sub { - my ($heap, $done, $left) = ($_[HEAP], @{$_[ARG1]}[0,1]); + my ( $heap, $done, $left ) = ( $_[HEAP], @{ $_[ARG1] }[ 0, 1 ] ); my $msg = $heap->{progress_message}; - printout(sprintf("%3.2f%%\r$msg ", $done * 100 / $left)); + printout( sprintf( "%3.2f%%\r$msg ", $done*100/$left ) ); printout("\n") if $done == $left; }, + # Puts the HTML in a temporary var stream_latest_files => sub { - my ($kernel, $session, $heap, $res, $data) = (@_[KERNEL, SESSION, HEAP], @{$_[ARG1]}); + my ( $kernel, $session, $heap, $res, $data ) + = ( @_[ KERNEL, SESSION, HEAP ], @{ $_[ARG1] } ); - die $res->status_line unless ($res->is_success); + die $res->status_line unless ( $res->is_success ); - if (defined $data) { - $heap->{latest_files_data} = "" unless exists $heap->{latest_files_data}; + if ( defined $data ) { + $heap->{latest_files_data} = "" + unless exists $heap->{latest_files_data}; $heap->{latest_files_data} .= $data; } else { - $kernel->call($session, 'parse_latest_files'); + $kernel->call( $session, 'parse_latest_files' ); $kernel->yield('load_completed_files'); $kernel->yield('filter_files'); } }, + # Parses the HTML, puts hashrefs in $_[HEAP]->{latest_files} parse_latest_files => sub { my $heap = $_[HEAP]; @@ -236,12 +252,12 @@ POE::Session->create( my $content = $heap->{latest_files_data}; delete $heap->{latest_files_data}; my @files = (); - my $info = {}; + my $info = {}; my @lines = split m/\n/, $content; foreach (@lines) { - if(m/^\s*((.+?)!) { $info->{date} = $1; @@ -250,10 +266,10 @@ POE::Session->create( } elsif (m!class="size".*?>(.+?)!) { $info->{size} = $1; } elsif (m!(.+?)!) { - $info->{url} = entity_clean($1); + $info->{url} = entity_clean($1); $info->{title} = entity_clean($2); } - } elsif (m!^\s*! and $info->{url}) { + } elsif ( m!^\s*! and $info->{url} ) { push @files, $info; $info = {}; } @@ -261,46 +277,48 @@ POE::Session->create( $heap->{latest_files} = \@files; }, + # Loads the list of completed files load_completed_files => sub { my $heap = $_[HEAP]; $heap->{completed_files} = []; - if (file_exists("completed_files.txt")) { - my $in = file_open("<", "completed_files.txt"); + if ( file_exists("completed_files.txt") ) { + my $in = file_open( "<", "completed_files.txt" ); foreach (<$in>) { chomp; s/\r$//; - push @{$heap->{completed_files}}, decode_utf8($_); + push @{ $heap->{completed_files} }, decode_utf8($_); } close $in; } }, mark_as_completed => sub { - my $completed_files = file_open(">>", "completed_files.txt"); - print $completed_files encode_utf8($_[ARG0] . "\n"); + my $completed_files = file_open( ">>", "completed_files.txt" ); + print $completed_files encode_utf8( $_[ARG0] . "\n" ); close $completed_files; }, + # Filters the file list filter_files => sub { - my ($kernel, $heap) = @_[KERNEL, HEAP]; + my ( $kernel, $heap ) = @_[ KERNEL, HEAP ]; $heap->{allowed_files} = []; my $skip; - my $filecount = scalar(@{$heap->{latest_files}}); - my $i = 1; - foreach my $info (@{$heap->{latest_files}}) { - printout("\rFiltering " . $i++ . "/$filecount... "); + my $filecount = scalar( @{ $heap->{latest_files} } ); + my $i = 1; + foreach my $info ( @{ $heap->{latest_files} } ) { + printout( "\rFiltering " . $i++ . "/$filecount... " ); $skip = 0; - foreach (@{$heap->{completed_files}}) { - if ($info->{title} eq $_) { + foreach ( @{ $heap->{completed_files} } ) { + if ( $info->{title} eq $_ ) { $skip = 1; last; } } next if $skip; my $allowed = 0; - foreach my $group (keys %file_allow_filters) { - foreach my $filter (@{$file_allow_filters{$group}}) { - if ($info->{$group} =~ /$filter/) { + foreach my $group ( keys %file_allow_filters ) { + foreach my $filter ( @{ $file_allow_filters{$group} } ) { + if ( $info->{$group} =~ /$filter/ ) { $allowed = 1; last; } @@ -308,9 +326,9 @@ POE::Session->create( last if $allowed; } - foreach my $group (keys %file_deny_filters) { - foreach my $filter (@{$file_deny_filters{$group}}) { - if ($info->{$group} =~ /$filter/) { + foreach my $group ( keys %file_deny_filters ) { + foreach my $filter ( @{ $file_deny_filters{$group} } ) { + if ( $info->{$group} =~ /$filter/ ) { $allowed = 0; last; } @@ -318,7 +336,7 @@ POE::Session->create( last unless $allowed; } - push @{$heap->{allowed_files}}, $info if $allowed; + push @{ $heap->{allowed_files} }, $info if $allowed; } delete $heap->{latest_files}; $kernel->yield('sort_allowed_files'); @@ -326,216 +344,264 @@ POE::Session->create( $kernel->yield('download_entries'); }, sort_allowed_files => sub { + # TODO: actually sort them # NOTE: the list is used in reverse order }, print_allowed_files => sub { my $heap = $_[HEAP]; - my $a = @{$heap->{allowed_files}} > 0 ? ":" : "."; - printout(@{$heap->{allowed_files}} . " entries accepted$a\n"); - foreach (@{$heap->{allowed_files}}) { - printout(" \"" . $_->{title} . ($_->{url} =~ m!/$! ? "/" : "") . "\" (" . $_->{size} . ")\n"); + my $a = @{ $heap->{allowed_files} } > 0 ? ":" : "."; + printout( @{ $heap->{allowed_files} } . " entries accepted$a\n" ); + foreach ( @{ $heap->{allowed_files} } ) { + printout( " \"" + . $_->{title} + . ( $_->{url} =~ m!/$! ? "/" : "" ) . "\" (" + . $_->{size} + . ")\n" ); } }, + # This state is called after each file finishes downloading download_entries => sub { - my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; - my $info = pop @{$heap->{allowed_files}}; + my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP ]; + my $info = pop @{ $heap->{allowed_files} }; if ($info) { - if ($info->{url} =~ /\/$/) { - $kernel->call($session, 'subfolder_recurse', $info); + if ( $info->{url} =~ /\/$/ ) { + $kernel->call( $session, 'subfolder_recurse', $info ); } else { - $kernel->call($session, 'download_file', $info); + $kernel->call( $session, 'download_file', $info ); } } }, + # Downloads folder listings subfolder_recurse => sub { - my ($kernel, $heap, $info) = @_[KERNEL, HEAP, ARG0]; + my ( $kernel, $heap, $info ) = @_[ KERNEL, HEAP, ARG0 ]; - my $req = HTTP::Request->new(GET => $info->{url}); + my $req = HTTP::Request->new( GET => $info->{url} ); $heap->{info} = $info; - $heap->{progress_message} = "Listing \"". $info->{title} ."/\":"; - $kernel->call('ua', 'request', 'stream_subfolder', $req, $reqid++, 'progress'); - while (defined $heap->{info}) { $kernel->run_one_timeslice(); } + $heap->{progress_message} + = "Listing \"" . $info->{title} . "/\":"; + $kernel->call( 'ua', 'request', 'stream_subfolder', $req, + $reqid++, 'progress' ); + while ( defined $heap->{info} ) { $kernel->run_one_timeslice(); } $kernel->yield('download_entries'); }, stream_subfolder => sub { - my ($kernel, $heap, $res, $data) = (@_[KERNEL, HEAP], @{$_[ARG1]}); + my ( $kernel, $heap, $res, $data ) + = ( @_[ KERNEL, HEAP ], @{ $_[ARG1] } ); my $info = $heap->{info}; - die $res->status_line unless ($res->is_success); + die $res->status_line unless ( $res->is_success ); # HACK: I dunno why I use md5_hex, but it looked like a good idea at the time # HACK: A very bizarre and most certainly broken filename made perl whine, so I added encode_entities - if (defined $data) { - $heap->{"folder-" . md5_hex(encode_entities($info->{title}))} = "" unless exists $heap->{"folder-" . md5_hex(encode_entities($info->{title}))}; - $heap->{"folder-" . md5_hex(encode_entities($info->{title}))} .= $data; + if ( defined $data ) { + $heap->{ "folder-" + . md5_hex( encode_entities( $info->{title} ) ) } = "" + unless exists $heap->{ + "folder-" + . md5_hex( encode_entities( $info->{title} ) ) + }; + $heap->{ "folder-" + . md5_hex( encode_entities( $info->{title} ) ) } + .= $data; } else { $kernel->yield('subfolder_parse'); } }, + # Parses folder listings, pushes subitems to allowed_files, folders last # This makes subfolders be checked before files subfolder_parse => sub { my $heap = $_[HEAP]; - my $info = $heap->{info}; - my @lines = split m/\n/, $heap->{"folder-" . md5_hex(encode_entities($info->{title}))}; - my @files = (); + my $info = $heap->{info}; + my @lines = split m/\n/, + $heap->{ "folder-" + . md5_hex( encode_entities( $info->{title} ) ) }; + my @files = (); my @folders = (); foreach (@lines) { - if (m!.*Directory!) { + if (m!.*Directory! + ) { next if $1 eq "../"; push @folders, $1; - } elsif (m!(.*?).*(.*?)!) { - push @files, [$1, $2]; + } elsif ( + m!(.*?).*(.*?)! + ) { + push @files, [ $1, $2 ]; } } - while ($_ = pop @files) { - my $finfo = {%{$info}}; - $finfo->{title} = $info->{title}; - $finfo->{title} .= '/' unless $finfo->{title} =~ m!/$!; - $finfo->{title} .= entity_clean($_->[0]); - $finfo->{url} = $info->{url} . entity_clean($_->[0]), - $finfo->{size} = $_->[1]; + while ( $_ = pop @files ) { + my $finfo = { %{$info} }; + $finfo->{title} = $info->{title}; + $finfo->{title} .= '/' unless $finfo->{title} =~ m!/$!; + $finfo->{title} .= entity_clean( $_->[0] ); + $finfo->{url} = $info->{url} . entity_clean( $_->[0] ), + $finfo->{size} = $_->[1]; my $skip = 0; - foreach (@{$heap->{completed_files}}) { - if ($finfo->{title} eq $_) { + foreach ( @{ $heap->{completed_files} } ) { + if ( $finfo->{title} eq $_ ) { $skip = 1; last; } } - push @{$heap->{allowed_files}}, $finfo unless $skip; + push @{ $heap->{allowed_files} }, $finfo unless $skip; } + # There is no trailing slash on the title # There IS a trailing slash on folder URLs foreach (@folders) { - my $subinfo = {%{$info}}; # make a copy - $subinfo->{title} = $info->{title}; + my $subinfo = { %{$info} }; # make a copy + $subinfo->{title} = $info->{title}; $subinfo->{title} .= '/' unless $subinfo->{title} =~ m!/$!; $subinfo->{title} .= entity_clean($_); $subinfo->{title} .= '/' unless $subinfo->{title} =~ m!/$!; - $subinfo->{url} = $info->{url} . entity_clean($_); + $subinfo->{url} = $info->{url} . entity_clean($_); my $skip = 0; - foreach (@{$heap->{completed_files}}) { - if ($subinfo->{title} eq $_) { + foreach ( @{ $heap->{completed_files} } ) { + if ( $subinfo->{title} eq $_ ) { $skip = 1; last; } } - push @{$heap->{allowed_files}}, $subinfo unless $skip; + push @{ $heap->{allowed_files} }, $subinfo unless $skip; } - delete $heap->{"folder-" . md5_hex(encode_entities($info->{title}))}; + delete $heap->{ "folder-" + . md5_hex( encode_entities( $info->{title} ) ) }; $heap->{info} = undef; delete $heap->{info}; }, + # Actually downloads files download_file => sub { - my ($kernel, $heap, $info) = @_[KERNEL, HEAP, ARG0]; + my ( $kernel, $heap, $info ) = @_[ KERNEL, HEAP, ARG0 ]; - my $req = HTTP::Request->new(GET => $info->{url}); - $req->header (Accept_Ranges => "bytes"); - $heap->{have_size} = file_size($info->{title}); - if ($heap->{have_size}) { - $req->header (Range => "bytes=". $heap->{have_size} ."-"); + my $req = HTTP::Request->new( GET => $info->{url} ); + $req->header( Accept_Ranges => "bytes" ); + $heap->{have_size} = file_size( $info->{title} ); + if ( $heap->{have_size} ) { + $req->header( Range => "bytes=" . $heap->{have_size} . "-" ); } else { $heap->{have_size} = 0; } - my $sizelen = length($info->{size}) + 2; + my $sizelen = length( $info->{size} ) + 2; my $proglen = 23; - $heap->{progress_message} = "\"". - shorten_path($info->{title}, $maxlen - $sizelen - $proglen - 3) ."\" (". $info->{size} ."): "; - printout($heap->{progress_message}); + $heap->{progress_message} = "\"" + . shorten_path( $info->{title}, + $maxlen - $sizelen - $proglen - 3 ) + . "\" (" + . $info->{size} . "): "; + printout( $heap->{progress_message} ); $heap->{filename} = $info->{title}; - $heap->{last_time} = time; + $heap->{last_time} = time; $heap->{start_time} = $heap->{last_time}; - $heap->{len} = 0; - $heap->{oct} = 0; - $kernel->delay('calculate_speed' => 2); - $kernel->call('ua', 'request', 'stream_file', $req, $reqid++, 'file_progress'); + $heap->{len} = 0; + $heap->{oct} = 0; + $kernel->delay( 'calculate_speed' => 2 ); + $kernel->call( 'ua', 'request', 'stream_file', $req, $reqid++, + 'file_progress' ); }, + # Does the actual file downloading stream_file => sub { - my ($kernel, $session, $heap, $req, $res, $data) = - (@_[KERNEL, SESSION, HEAP, ARG0], @{$_[ARG1]}); + my ( $kernel, $session, $heap, $req, $res, $data ) + = ( @_[ KERNEL, SESSION, HEAP, ARG0 ], @{ $_[ARG1] } ); - die $res->status_line unless $res->is_success or $res->status_line =~ /^416/; + die $res->status_line + unless $res->is_success + or $res->status_line =~ /^416/; - if (defined($data)) { - unless ($heap->{outfh} and $heap->{content_length}) { + if ( defined($data) ) { + unless ( $heap->{outfh} and $heap->{content_length} ) { $heap->{filename} =~ m!(.*)/!; mkpath($1) if $1; - $heap->{outfh} = file_open('>>',$heap->{filename}) or die "$^E"; + $heap->{outfh} = file_open( '>>', $heap->{filename} ) + or die "$^E"; $heap->{content_length} = $res->header('Content-Length'); + # This is probably wrong - if ($heap->{content_length} == $heap->{have_size}) { - close($heap->{outfh}); - $kernel->call($session, 'mark_as_completed', $heap->{filename}); - $kernel->post('ua', 'cancel', $req); + if ( $heap->{content_length} == $heap->{have_size} ) { + close( $heap->{outfh} ); + $kernel->call( $session, 'mark_as_completed', + $heap->{filename} ); + $kernel->post( 'ua', 'cancel', $req ); $kernel->yield('download_entries'); } } - unless ($heap->{outfh} && syswrite($heap->{outfh}, $data)) { + unless ( $heap->{outfh} && syswrite( $heap->{outfh}, $data ) ) + { die "$^E"; } - } else { # Finished - unless ($res->is_success or $res->status_line =~ /^416/) { # Requested Range not satisfiable + } else { # Finished + unless ( $res->is_success or $res->status_line =~ /^416/ ) + { # Requested Range not satisfiable die $res->status_line; } - close($heap->{outfh}); + close( $heap->{outfh} ); delete $heap->{outfh}; - $kernel->call($session, 'mark_as_completed', $heap->{filename}); - $kernel->call($session, 'move_completed', $heap->{filename}); + $kernel->call( $session, 'mark_as_completed', + $heap->{filename} ); + $kernel->call( $session, 'move_completed', + $heap->{filename} ); $kernel->yield('download_entries'); } }, calculate_speed => sub { - my ($kernel, $heap) = @_[KERNEL, HEAP]; + my ( $kernel, $heap ) = @_[ KERNEL, HEAP ]; my $now = time; - if ($now == $heap->{last_time}) { - $kernel->delay('calculate_speed' => 0.5); + if ( $now == $heap->{last_time} ) { + $kernel->delay( 'calculate_speed' => 0.5 ); return; } - my $spd = $heap->{oct} / 1024 / ($now - $heap->{last_time}); - unless (defined $heap->{speeds}) { - $heap->{speeds} = []; + my $spd = $heap->{oct}/1024/( $now - $heap->{last_time} ); + unless ( defined $heap->{speeds} ) { + $heap->{speeds} = []; $heap->{speeds_i} = 0; - for(my $i = 0; $i < 8; $i++) { $heap->{speeds}->[$i] = $spd; } + for ( my $i = 0; $i < 8; $i++ ) { + $heap->{speeds}->[$i] = $spd; + } } - $heap->{speeds}->[$heap->{speeds_i}++] = $spd; - $heap->{speeds_i} %= @{$heap->{speeds}}; + $heap->{speeds}->[ $heap->{speeds_i}++ ] = $spd; + $heap->{speeds_i} %= @{ $heap->{speeds} }; $heap->{oct} = 0; $heap->{spd} = 0; - foreach (@{$heap->{speeds}}) { $heap->{spd} += $_; } - $heap->{spd} /= @{$heap->{speeds}}; + foreach ( @{ $heap->{speeds} } ) { $heap->{spd} += $_; } + $heap->{spd} /= @{ $heap->{speeds} }; $heap->{last_time} = time; - $kernel->delay('calculate_speed' => 0.5); + $kernel->delay( 'calculate_speed' => 0.5 ); }, calculate_avg_speed => sub { my $heap = $_[HEAP]; my $now = time; - $heap->{spd} = $heap->{len} / 1024 / ($now - $heap->{start_time}); + $heap->{spd} = $heap->{len}/1024/( $now - $heap->{start_time} ); }, + # Prints the progress report file_progress => sub { - my ($kernel, $heap, $pos, $len, $oct) = (@_[KERNEL, HEAP], @{$_[ARG1]}); + my ( $kernel, $heap, $pos, $len, $oct ) + = ( @_[ KERNEL, HEAP ], @{ $_[ARG1] } ); - if ($pos eq $len) { - $kernel->call($_[SESSION], 'calculate_avg_speed'); - my $line = sprintf("100.00%% (avg. %4.2f kB/s)", $heap->{spd}); + if ( $pos eq $len ) { + $kernel->call( $_[SESSION], 'calculate_avg_speed' ); + my $line + = sprintf( "100.00%% (avg. %4.2f kB/s)", $heap->{spd} ); $heap->{prevlen} = 0 unless $heap->{prevlen}; - printout("\r" . $heap->{progress_message} . "$line" . (" " x ($heap->{prevlen} - length($line))) . "\n"); + printout( "\r" + . $heap->{progress_message} . "$line" + . ( " " x ( $heap->{prevlen} - length($line) ) ) + . "\n" ); delete $heap->{have_size}; delete $heap->{prev_time}; delete $heap->{prevlen}; @@ -544,7 +610,8 @@ POE::Session->create( $heap->{speeds_i} = 0; $kernel->delay('calculate_speed'); } else { - my $have_size = exists $heap->{have_size} ? $heap->{have_size} : 0; + my $have_size + = exists $heap->{have_size} ? $heap->{have_size} : 0; my $spd = $heap->{spd} ? $heap->{spd} : undef; @@ -553,16 +620,22 @@ POE::Session->create( $heap->{len} += $octlen; # $have_size may be a BigInt, use fixed point math / formatters - my $pct = ($pos + $have_size)*10000/($len + $have_size); + my $pct = ( $pos + $have_size )*10000/( $len + $have_size ); my $line; - if (defined($spd)) { - $line = sprintf("%2d.%02d%% (%4.2f kB/s)", $pct / 100, $pct % 100, $spd); + if ( defined($spd) ) { + $line = sprintf( "%2d.%02d%% (%4.2f kB/s)", + $pct/100, $pct % 100, $spd ); } else { - $line = sprintf("%2d.%02d%%", $pct / 100, $pct % 100); + $line = sprintf( "%2d.%02d%%", $pct/100, $pct % 100 ); } - $heap->{prevlen} = length($line) unless defined $heap->{prevlen} and $heap->{prevlen} > length($line); - printout("$line" . (" " x ($heap->{prevlen} - length($line))) . "\r" . $heap->{progress_message}); + $heap->{prevlen} = length($line) + unless defined $heap->{prevlen} + and $heap->{prevlen} > length($line); + printout( "$line" + . ( " " x ( $heap->{prevlen} - length($line) ) ) + . "\r" + . $heap->{progress_message} ); $heap->{prevlen} = length($line); } }, @@ -580,15 +653,14 @@ POE::Session->create( } mkpath("$dest"); printout("Moving \"$filename\" to \"$dest/$fname\"... "); - if (move("$filename", "$dest/$fname")) { + if ( move( "$filename", "$dest/$fname" ) ) { printout("OK\n"); } else { printout("$^E\n"); } } - } - } -); + } + } ); POE::Kernel->run(); -- 2.11.4.GIT