All pastes #333699 Raw Edit

Someone

public text v1 · immutable
#333699 ·published 2007-01-31 01:38 UTC
rendered paste body
# This is to be a simple cli to torrentspy.com
#
#



use WWW::Mechanize;
use HTML::TokeParser;
use Term::ANSIColor;

# Defaults
$url = "http://torrentspy.com";
$num_results = 10;

print "Please enter your query: ";
$query = <>;
chomp $query;
&search;

# Here is where the search results are scraped
sub search(){
        $mech = WWW::Mechanize->new();
        $mech->get( $url );

        $mech->submit_form(
                form_name => 'frmSearch',
                fields    => { query  => "$query", },
                button    => 'submit'
        );

        # $c = $mech->content();
        # print $c;

        $stream = HTML::TokeParser->new(\$mech->{content});
        while ( $tag = $stream->get_tag("table") ) {
                if ($tag->[1]{class} and $tag->[1]{class} eq 'list') {
                        for ($i=0; $i<$num_results; $i++) {
                                $tag = $stream->get_tag("a");
                                $tag = $stream->get_tag("a");
                                $tag = $stream->get_tag("a");
                                $link2torrent = $tag->[1]{href};
                                #print "$link2torrent\t";
                                push ( @link_array, "$link2torrent" );
                        
                                $tag = $stream->get_tag("b");
                                $name = $stream->get_text("/b");
                                #print "$name\t";
                                push (@name_array, "$name");
                        
                                $tag = $stream->get_tag("a");
                                $tag = $stream->get_tag("a");
                                $tag = $stream->get_tag("td");
                                $size = $stream->get_text("/td");
                                #print "$size\t";
                                push (@size_array, "$size");
                                
                                $tag = $stream->get_tag("td");
                                $files = $stream->get_text("/td");
                                #print "Num of files: $files\n";
                                push (@files_array, "$files");
                                
                                $tag = $stream->get_tag("td");
                                $seeders = $stream->get_text("/td");
                                #print "Seeders: $seeders\n";
                                push (@seeders_array, "$seeders");
                                
                                $tag = $stream->get_tag("td");
                                $downloads = $stream->get_text("/td");
                                #print "Downloaded $downloads times\n";
                                push (@downloads_array, "$downloads");
                        }
                }
        }
        &display;
}

# Here is where the results are displayed
sub display(){
        print "\nName", color("blue")," | ",color("reset"),color("green"),"Size",color("reset"),color("blue")," | ",color("reset"),color("red"),"Seeders",color("reset"),color("blue")," | ",color("reset"),color("cyan"),"Downloads\n\n", color("reset");

        for ($i=0; $i<$num_results; $i++){
                print "$i. $name_array[$i]", color("blue"),"| ",color("reset"), color("green"),"$size_array[$i]",color("reset"),color("blue")," | ",color("reset"), color("red"),"$seeders_array[$i]", color("reset"),color("blue")," | ",color("reset"), color("cyan"),"$downloads_array[$i]\n", color("reset");
        }

# Make your decision to dl a torrent, do a new search, or quit
        print "\nEnter the number of the torrent you would like to download, or 'n' for a new search, or 'q' to quit\n";
        $selection = <>;
        chomp $selection;

        if( $selection =~ /^n$/ ){
                print "Please enter your query: ";
                $query = <>;
                chomp $query;
                &search;
        }
        elsif( $selection =~ /^q$/ ){
                        exit;
        }
        else{
                &download();
        }
}

# Get the torrent
sub download {
        $torrent_link = "$url$link_array[$selection]";
        $mech2 = WWW::Mechanize->new();
        $mech2->get( $torrent_link );
        $stream2 = HTML::TokeParser->new(\$mech2->{content});
        while( $tag2 = $stream2->get_tag("div") ) {
                if ($tag2->[1]{class} and $tag2->[1]{class} eq 't-info') {
                        $tag2 = $stream2->get_tag("a");
                        $link2file = $tag2->[1]{href};

                        $tag2 = $stream2->get_tag("span");
                        $filename = $tag2->[1]{title};

                }
        }

        print "Download Directory [~/hdb/torrents2]: ";
        $download_dir = <>;
        chomp $download_dir;
        if( $download_dir !~ /[\/\~\w\d]+/ ){
                $download_dir = "~/hdb/torrents2";      
        }

        $dl_command = "wget  -O $download_dir/$filename  http://torrentspy.com$link2file";
        print "Saving $download_dir/$filename...\n";
        system($dl_command);
        
                
}