#!/usr/bin/perl -w
use strict;
$|++;

use File::Basename;
use WWW::Mechanize 0.48;

my $mech = WWW::Mechanize->new(  );

# Zaczynamy od strony wyszukiwania
$mech->get( "http://search.cpan.org" );
$mech->success or die $mech->response->status_line;

# Wybierz formularz, wypenij pola, wylij
$mech->form_number( 1 );
$mech->field( query => "Lester" );
$mech->field( mode => "author" );
$mech->submit(  );

$mech->success or die "wysanie nie powiodo si: ",
   $mech->response->status_line;

# Znajd hipercze "Andy"
$mech->follow_link( text_regex => qr/Andy/ );
$mech->success or die "wysyanie nie powiodo si: ", $mech->response->status_line;

# Pobieranie wszystkich plikw
my @links = $mech->find_all_links( url_regex => qr/\.tar\.gz$/ );
my @urls = map { $_->[0] } @links;

print "Znaleziono ", scalar @urls, " plikw do pobrania\n";

for my $url ( @urls ) {
    my $filename = basename( $url );
    print "$filename --> ";
    $mech->get( $url, ':content_file'=>$filename );
    print -s $filename, " bajtw\n";
}

