App-Caoliu
view release on metacpan or search on metacpan
lib/App/Caoliu.pm view on Meta::CPAN
my $category = shift || $self->category;
return if not scalar @{ $self->category };
my @download_links;
my @feeds = map { FEEDS->{ uc $_ } } @{ $self->category };
$self->log->debug( "show feeds: " . dumper \@feeds );
# fetch all rmdown_link
# parallel get post page and download bt files
$self->_non_blocking_get_torrent(@feeds);
unless ( Mojo::IOLoop->is_running ) {
$self->loop->wait;
$self->log->debug(
"Downloaded files list:" . dumper [@downloaded_files] );
return wantarray ? @downloaded_files : scalar(@downloaded_files);
}
}
sub _non_blocking_get_torrent {
my ( $self, @feeds ) = @_;
for my $feed (@feeds) {
$self->loop->begin;
$self->downloader->ua->get(
$feed => sub {
$self->_process_feed(@_);
}
);
}
}
sub _process_feed {
my ( $self, $ua, $tx ) = @_;
my @posts;
my $processer;
if ( $tx->success ) {
my $xml = $tx->res->body;
my $post_collection = $self->parser->parse_rss($xml);
$processer = sub {
for (@_) {
$ua->get( $_->{link} => sub { $self->_process_posts(@_) } );
}
};
$processer->( @{$post_collection} );
}
}
sub _process_posts {
my ( $self, $ua, $tx ) = @_;
my $post_hashref;
if ( $tx->success ) {
$post_hashref = $self->parser->parse_post( $tx->res->body );
$post_hashref->{source} = $tx->req->url->to_string;
if ( my $download_link = $post_hashref->{rmdown_link} ) {
# set a alarm clock,when async download,perhaps program will block
# here,and every thread will block...
eval {
local $SIG{ALRM} = sub { die "TIMEOUT" };
alarm DOWNLOAD_FILE_TIME;
my $retry_times = 3;
while ($retry_times) {
my $file =
$self->downloader->download_torrent( $download_link,
$self->target, { md5_dir => 0 } );
$post_hashref->{bt} = $file;
last if $file;
if ( $retry_times == 1 ) {
sleep 3;
}
if ( $retry_times == 2 ) {
sleep 1;
}
$retry_times--;
}
alarm 0;
};
if ( $@ =~ m/TIMEOUT/ ) {
$self->log->error( "Download file timeout ..... in "
. $post_hashref->{rmdown_link} );
}
if ($@) { $self->log->error($@); }
}
push @downloaded_files, $post_hashref;
}
}
1;
__END__
=pod
=encoding utf8
=head1 NAME App::Caoliu
=head1 DESCRIPTION
If you are the fans of 1024 bbs,you should know what I did for it.After you learn
this module,you will feel very happy to make communication to 1024 bbs.
OK,I don't want to see any more,just follow me step by step.
Let's rock!!!
=head1 SYNOPSIS
use App::Caoliu;
use 5.010;
# reap the torrent from a category
my $c = App::Caoliu->new( category => ['wuma'],target => '/tmp');
# set proxy,if you have installed go-agent or some other proxy softwares
# because the gfw often suck 1024 bbs
$c->proxy('127.0.0.1:8087');
# when in scalar env ,return the count number of downloaded files
say "total downloaded ".scalar($c->reap)." torrent files";
use App::Caoliu::Utils 'dumper';
( run in 0.634 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )