All pastes #448678 Raw Edit

Mine

public text v1 · immutable
·published 2007-04-19 20:29 UTC
rendered paste body
#!/usr/bin/perl# $Revision: 1.4 $## tspy.pl is a simple command line interface to http://www.torrentspy.com# Copyright (C) 2007 dougsko## This program is free software; you can redistribute it and/or# modify it under the terms of the GNU General Public License# as published by the Free Software Foundation; either version 2# of the License, or (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.## $Log: tspy.pl,v $# Revision 1.4  2007/03/06 04:15:26  doug# created main(), did some organizing## Revision 1.3  2007/03/06 01:31:03  doug# *** empty log message ***## Revision 1.2  2007/02/21 10:07:02  doug# added log keyword#use WWW::Mechanize;use HTML::TokeParser;use Term::ANSIColor;# Set up a couple global vars$url = "http://torrentspy.com";$num_results = 11;$j = 1;# Check to see if curl exists and read/create config fileif( ! `which curl` ){	print "You need to install wget to run this x_X\n";	exit;}if( -e "$ENV{HOME}/.tspyrc"){	do "$ENV{HOME}/.tspyrc";}else{	open(CONFIG, ">$ENV{HOME}/.tspyrc");	print color("yellow bold on_black"), "\n\t\tTspy: The CLI to Torrentspy.com",color("reset"), "\n\n";	print "Please enter default torrent folder: ";	$download_dir = <>;	chomp $download_dir;	if( ! -e $download_dir ){		print "Directory does not exist.\n";		print "Creating $download_dir\n";		system("mkdir $download_dir");	}	print CONFIG "# Default download directory\n";	print CONFIG "\$download_dir = \"$download_dir\"\;\n\n";		print CONFIG "# WWW::Mechanize user agent\n";	print CONFIG "# Valid choices:\n";	print CONFIG "#\tWindows IE 6\n";	print CONFIG "#\tWindows Mozilla\n";	print CONFIG "#\tMac Safari\n";	print CONFIG "#\tMac Mozilla\n";	print CONFIG "#\tLinux Mozilla\n";	print CONFIG "#\tLinux Konqueror\n";	print CONFIG "\$user_agent = \"Linux Mozilla\"\;\n\n";	print CONFIG "# Here is where you can customize the colors used.\n";	print CONFIG "# You can use any of the attributes valid for the Term::ANSIColor module.\n";	print CONFIG "# Go here to see all your options: http://search.cpan.org/~rra/ANSIColor-1.11/ANSIColor.pm\n";	print CONFIG "\$title_color = \"yellow bold on_black\"\;\n";	print CONFIG "\$bar_color = \"blue\"\;\n";	print CONFIG "\$size_color = \"green\"\;\n";	print CONFIG "\$seeders_color = \"red\"\;\n";	print CONFIG "\$downloads_color = \"cyan\"\;\n";	print CONFIG "\$saved_color = \"red bold blink\"\;\n\n";		close(CONFIG);	do "$ENV{HOME}/.tspyrc";}#### Main stuff starts here ####&main;sub main(){	system("clear");                                        	print color($title_color), "\n\t\tTspy: The CLI to Torrentspy.com",color("reset"), "\n\n";	&search;	&display;}sub search(){	print "\nPlease enter your query: ";	$query = <>;	chomp $query;	@link_array = (0);	@name_array = (0);	@size_array = (0);	@files_array = (0);	@seeders_array = (0);	@downloads_array = (0);	$mech = WWW::Mechanize->new();	#$mech->proxy(['http'], 'http://localhost:8118/');	$mech->agent_alias( $user_agent );	$mech->get( $url );	$mech->submit_form(         	form_name => 'frmSearch',         	fields    => { query  => "$query", },         	button    => 'submit' 	);	$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<51; $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");  # <- short names				#$name = $stream->get_text("/b");				$name = $tag->[1]{title};  # <- long names					#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");			}		}	}}sub display(){	system("clear");	print color($title_color), "\n\t\tTspy: The CLI to Torrentspy.com",color("reset"), "\n";		print "\nName", color($bar_color)," | ",color("reset"),color($size_color),"Size",color("reset"),color($bar_color)," |",color("reset"),color($seeders_color),"Seeders",color("reset"),color($bar_color)," |",color("reset"),color($downloads_color),"Downloads\n\n", color("reset");	for ($j; $j<$num_results; $j++  ){			print "$j. $name_array[$j]", color($bar_color),"| ",color("reset"), color($size_color),"$size_array[$j]",color("reset"),color($bar_color)," | ",color("reset"), color($seeders_color),"$seeders_array[$j]", color("reset"),color($bar_color)," | ",color("reset"), color($downloads_color),"$downloads_array[$j]\n", color("reset");	}	print "\nEnter the number of the torrent you would like to download\n'</>' prev/next 10, 'n' for new search, 'q' to quit: ";	$selection = <>;	chomp $selection;		if( $selection =~ /\d+/ ){ &download; }	elsif( $selection =~ /^n$/ ){ &main; }	elsif( $selection =~ /^q$/ ){  }	elsif( $selection =~ /^<$/ ){  }	elsif( $selection =~ /^>$/ ){  }}sub download {	print "\nDownload Directory [$download_dir]: ";	$download_change = <>;	chomp $download_change;	if( $download_change =~ /[\/\w\d\~]/ ){		$download_dir = $download_change;	}	$torrent_link = "$url$link_array[$selection]";	$mech2 = WWW::Mechanize->new();	#$mech2->proxy(['http'], 'http://localhost:8118/');	$mech2->agent_alias( "$user_agent" );	$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};		}	}	$filename =~ s/ /_/g;	$filename =~ s/&/\+/g;	$filename =~ s/[\(]/[/g;	$filename =~ s/[\)]/]/g;	$filename =~ s/[\']//g;	$filename =~ s/[\"]//g;	#$dl_command = "wget -q -O $download_dir/$filename $url$link2file"; 	$dl_command = "curl -x localhost:8118 -o $download_dir/$filename $url$link2file";	system($dl_command);	print color($saved_color),"\nSaved",color("reset")," $download_dir/$filename\n\n", "Press [ENTER]...\n";	$_ = <STDIN>;	&display;}