App-Caoliu

 view release on metacpan or  search on metacpan

lib/App/Caoliu/Downloader.pm  view on Meta::CPAN

package App::Caoliu::Downloader;

# ABSTRACT: caoliu download tool
use Mojo::Base 'Mojo';
use Carp;
use Mojo::UserAgent;
use Mojo::Util;
use File::Spec;
use File::Basename;
use File::Path qw(make_path remove_tree);
use Mojo::IOLoop;
use Mojo::Collection;
use Mojo::Log;
use Time::HiRes qw(sleep);
use App::Caoliu::Utils qw(abs_file dumper);

# defined constant var
sub RM_DOWNLOAD_PHP () { 'http://www.rmdown.com/download.php' }

has ua => sub { Mojo::UserAgent->new->max_redirects(5) };
has timeout => 60;
has rmdown  => 'http://www.rmdown.com';
has proxy   => '127.0.0.1:8087';
has agent =>
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.802.30 Safari/535.1 SE 2.X MetaSr 1.0';
has log => sub { Mojo::Log->new };

sub download_torrent {
    my ( $self, $url, $path, $attr ) = @_;
    my $require_md5_path = $attr->{md5_dir};

    Carp::croak("please input the torrent url link") unless $url;
    Carp::croak("path not exists: $path") unless -e $path;

    return unless $url =~ m{rmdown};

    my $headers = {
        User_Agent   => $self->agent,
        Referer      => $url,
        Origin       => $self->rmdown,
        Content_Type => 'form-data',
    };
    my $post_form = {};

    # get refvalue and reffvalue for post_form
    my $ua = Mojo::UserAgent->new( max_redirects => 5 );
    my $tx = $ua->get( $url => $headers );
    if ( my $res = $tx->success ) {
        my $html = $res->body;
        if ( $html =~ m/(<INPUT.+?name=['"]?ref['"]?.*?>)/gi ) {
            my $tmp = $1;
            $post_form->{ref} = $1
              if ( $tmp =~ m/(?<=value=)["']?([^\s>'"]+)/gi );
        }
        if ( $html =~ m/(<INPUT.+?name=['"]?ref['"]?.*?>)/gi ) {
            my $tmp = $1;
            $post_form->{reff} = $1
              if ( $tmp =~ m/(?<=value=)["']?([^\s>'"]+)/gi );
        }
    }
    else {
        $self->log->error("get reffvalue failed,check ....");
        return;
    }

    # construct post_from and submit post request to rmdownload
    # download file here, and return filename md5
    $post_form->{submit} = 'download';
    $self->log->debug(
        "send http_reqeust to rmdown with form" . dumper($post_form) );
    $tx = $ua->post( +RM_DOWNLOAD_PHP, $headers => form => $post_form );
    if ( $tx->success ) {
        $self->log->debug("post rmdownload link successful!");

        #if ( $tx->res->headers->content_disposition =~
        my $cd = $tx->res->headers->content_disposition;
        return unless $cd;
        if ( $cd =~ m/(?<=filename=)["']?([^\s>'"]+)/gi ) {
            my $tmpfile = $1;
            $self->log->debug("get tmpfile name => $tmpfile");
            my ($hash_md5) = fileparse( $tmpfile, qr/\.[^.]*/ );
            my $file_path =
              $require_md5_path
              ? File::Spec->catfile( $path, $hash_md5, $tmpfile )
              : File::Spec->catfile( $path, $tmpfile );
            if ( -e $file_path ) {
                $self->log->debug("this path:$file_path is exists ,next....");
                return $file_path;
            }
            else {
                make_path dirname($file_path);
                $self->log->debug( "make_path => " . dirname($file_path) );
                return
                  unless $tx->res->content->asset->move_to($file_path);
                return $file_path;
            }
        }
    }
    else {
        $self->log->error( "download failed,return response" . $tx->res->body );
    }

    return;
}

sub move_file {
    my ( $self, $ua, $url, $target ) = @_;

    my $retry_times = 3;
    while ($retry_times) {
        my $tx = $ua->get($url);
        if ( $tx->success ) {
            $tx->res->content->asset->move_to($target);
            last;
        }



( run in 2.350 seconds using v1.01-cache-2.11-cpan-13bb782fe5a )