#!/usr/bin/perl

require "functions.pl";

use IO::Handle;
use DBI();
$drh			= DBI->install_driver("mysql");

my $timeout		= "300";
my $ua			= "Mozilla/5.0 (Windows; U; Windows NT 5.0; de-DE; rv:1.6) Gecko/20040206 Firefox/1.0.1";

my $DBHOST		= "localhost";
my $DBNAME		= "zoozle";
my $DBUSER		= "zoozle";	
my $DBPASS		= "zoozle23!99";	

my $dbh			= DBI->connect("DBI:mysql:database=$DBNAME;host=$DBHOST", "$DBUSER", "$DBPASS", {'RaiseError' => 0});

unlink "torrent_news.txt";
open(WH,">torrent_news.txt");
WH->autoflush(1);
# delete from `torrent3` where `LINK` like('http://speedtorrent.to/v2/download.php%')

#######################
#### english sites here
#######################

&getNewsMininova();			# working @ 01.10.2006
&getNewsTorrentreactor();	# working @ 01.10.2006
&getNewsBtjunkie();			# working @ 01.10.2006
&getNewsMeganova();			# working @ 01.10.2006
&getNewsTorrentbox();		# working @ 01.10.2006
#	&getNewsMetrotorrents();	# offline
&getNewsIsoHunt();			# working @ 01.10.2006
&getNewsDemonoid();			# working @ 01.10.2006
&getNewsPiratebay();		# working @ 01.10.2006
&getNewsMyBittorrent();		# working @ 01.10.2006

#######################
#### german sites here
#######################

&getBitbullTo();			# gogog
&getNewsSpeedtorrent();		# working 
&getNewsTorrentsMore();		# working 
&getNewsBitreactor();		# working 
&getNewsDeluxbits();		# funzt nicht richtig -> später löschen
&getNewsTorrentTo();		# working 

###################
###################
###################

close WH;

print "Done\n";

&add_content_toSQLDB("torrent_news.txt", "ALL");
&add_content_toREALSQLDB("torrent_news.txt", "ALL");

$dbh->disconnect;


#
#sub getNewsTorrentTo(){
#	
#	my $page = &http_get("http://torrent.to/torrent/", $timeout, $ua, '');
#	@CONTENT = split('\'', $page);
#	foreach $line ( @CONTENT ) {
#		$count++;
#		if ( ($line =~ /popup.php\?Active=/i)) {
#			$line =~ s/amp;//i;
#			my ( $temp, undef) = split('</b>', @CONTENT[$count+2] );
#			my (undef, $desc, undef ) = split('<b>', $temp);
#			chomp($desc);
#			if ( ($desc =~ /(\w)/i) && ( $line =~ /(\w)/ ) ) {
#				print WH "CATE=de#DESC=$desc#LINK=http://torrent.to/torrent/$line\n";
#			};
#		};
#	};
#}; # sub getNewsTorrentTo(){


sub getNewsBitreactor(){

	my $page = &http_get("http://bitreactor.to/index", $timeout, $ua);
	foreach $line ( split('href="',$page ) ) {
	
		if ( $line =~ /\/torrent\//i ) {
			# print "t: $line\n";
			my ( undef, $link )	= split('>', $line);
			my ( $desc, undef )		= split('<', $link);
			my ( $link, undef ) = split('"', $line);

			$count++;
			print WH "CATE=de#DESC=$desc#LINK=http://bitreactor.to$link\n";
		};
	};
}; # &getNewsBitreactor();


sub getNewsDeluxbits(){

	my $page = &http_get("http://deluxebits.to/deluxebits/index.php", $timeout, $ua, '');
	
	@CONTENT = split('"', $page);
	foreach $line ( @CONTENT ) {
		$count++;
		if ( ($line =~ /details\.php\?id=/i)) {
			$line =~ s/&amp;/&/i;
		
			my ( $desc, undef ) = split('<', @CONTENT[$count] );
			$desc =~ s/>//ig;
			$desc =~ s/\./ /;
			$desc =~ s/_/ /;
			$desc =~ s/-/ /;

			chomp($desc);
			
			if ( ($desc =~ /(\w)/i) && ( $line =~ /(\w)/) && (length($desc) > 5) && ($desc !~ /\.php/i) ) {
				print WH "CATE=de#DESC=$desc#LINK=http://deluxebits.to/deluxebits/$line\n";
			};
		};
	};

}; # sub getNewsDeluxbits(){


sub getNewsTorrentsMore(){

	my $page = &http_get("http://www.torrents-and-more.to/pml/index.php", $timeout, $ua, '');
	@CONTENT = split('\'', $page);
	foreach $line ( @CONTENT ) {
		$count++;
		if ( ($line =~ /popup\.php\?Active=/i)) {
			# $line =~ s/amp;//i;
			
			my ( undef, $link ) = split('"', $line);
			my ( $link, undef ) = split(/&pml=/i, $link);

			my ( undef, $desc, undef ) = split('<', @CONTENT[$count+3]);
			my ( undef, $desc) = split('>', $desc );
	
			$desc =~ s/^\s//;
			# print "LINK=$link\n";
			# print "DESC=$desc\n";

			chomp($desc);
			if ( ($desc =~ /(\w)/i) && ( $line =~ /(\w)/ ) ) {
				print WH "CATE=de#DESC=$desc#LINK=http://www.torrents-and-more.to/pml/$link\n";
			};
		};
	};

}; # sub getNewsTorrentsMore(){



sub getNewsSpeedtorrent(){

	my $page = &http_get("http://speedtorrent.to/index.php", $timeout, $ua);
	my @CONTENT = split('"', $page);
	foreach $line ( @CONTENT ) {
		$count++;
		if ( ($line =~ /popup.php\?Active=/i)) {
			$line =~ s/amp;//i;
			my ( undef, $link ) = split('\'', $line);
			if ( $link !~ /popup.php\?Active=/ ) {
				my ( undef, $link ) = split('"', $line);
			};
			my ( $desc, undef) = split('</a>', @CONTENT[$count-2] );
			$desc =~ s/ Informationen//i;
			if ( ($desc =~ /(\w)/i) && ( $link =~ /popup.php\?Active=/i ) ) {
				print WH "CATE=de#DESC=$desc#LINK=http://speedtorrent.to/$link\n";
			};
		};
	};

}; #http://v37-d.speedtorrent.to/popup.php?Active=filme&ID=853



sub getBitbullTo(){
	
	my $page = &http_get("http://www.bitbull.to/bitbull/", $timeout, $ua, '');
	my @CONTENT = split('onclick="', $page);
	foreach $line ( @CONTENT ) {
		$count++;
		if ( ($line =~ /popup/i)) {
			$line =~ s/amp;//i;
			my ( $temp, undef) = split('</a>', $line );
			my ( $stuff, $desc ) = split('"', $temp);	
			$desc =~ s/>//;
			$desc =~ s/^\s//;
			my ( undef, $cate, $nr ) = split('\'', $stuff);
			$nr =~ s/,//; $nr =~ s/\)//;
			chomp($desc); chomp($nr); chomp($cate);
			next if ( $desc =~ /DTD HTML 4/ );
			if ( ($desc =~ /(\w)/i) && ( length($desc) >= 3 ) ) {
				print WH "CATE=de#DESC=$desc#LINK=http://www.bitbull.to/bitbull/files/popup.php?table=$cate&id=$nr&jump=1\n";
			};
		};
	};
}; # &getBitbullTo();

#######################
#### english sites here
#######################


sub getNewsMyBittorrent(){

	my $page = &http_get("http://www.mybittorrent.com/", $timeout, $ua, '');
	@CONTENT = split('"', $page);
	foreach $line ( @CONTENT ) {
		$count++; 
		if ( ($line =~ /bittorrent_+(\d)+\.html/i)) {
			# $line =~ s/amp;//i;
			
		#	my ( undef, $link ) = split('"', $line);
		#	my ( $link, undef ) = split(/&pml=/i, $link);

			my ( $desc, undef ) = split('<', @CONTENT[$count+2]);
			my ( undef, $desc) = split('>', $desc );
	
			$desc =~ s/^\s//;
		#	print "LINK=$line\n";
		#	print "DESC=$desc\n";

			chomp($desc);
			if ( ($desc =~ /(\w)/i) && ( $line =~ /(\w)/ ) ) {
				print WH "CATE=en#DESC=$desc#LINK=$line\n";
			};
		};
	};

}; # sub getNewsMyBittorrent(){



sub getNewsPiratebay(){
	
	for ( $i=0; $i<=9 ; $i++ ) {
		my $page = &http_get("http://thepiratebay.org/recent.php?orderby=&page=$i", $timeout, $ua, '');
		
		foreach my $link ( split('="', $page ) ) {
			if ( $link =~ /\/tor\/+\d/i ) {
				($rlink, undef) = split('"', $link);
			} elsif ( $link =~ /Detail/i ) {
				my ( $desc) = split('</a></td>', $link);
				my ( undef, $desc ) = split('">', $desc);
				next if ( $desc =~ /^</);
				$desc =~ s/<\/a><\/td>//i;
				$desc =~ s/Details für//i;
				$desc =~ s/Details for//i;
				
				$count++;
				print WH "CATE=en#DESC=$desc#LINK=http://thepiratebay.org$rlink\n";
			};
		};
	}; 
}; # &get1NewsPiratebay();


sub getNewsDemonoid(){
		
	for ($i=1; $i<=5; $i++ ) {
	
		my $page = &http_get("http://www.demonoid.com/files/?uid=0&category=0&subcategory=0&language=0&seeded=0&quality=0&external=2&query=&page=$i", $timeout, $ua, '');
	
		my @CONTENT = split('a href=', $page);
		
		foreach $line ( @CONTENT ) {
			$count++;
			if ( ($line =~ /\/files\/details\//i)) {
				$line =~ s/amp;//i;
				my ( undef, $tmp_path, $tmp) = split('"', $line);
				my ( $desc, undef ) = split('</a>', $tmp);
				$desc =~ s/>//;
				chomp($desc); chomp($tmp_path);
				if ( ($desc =~ /(\w)/i) && ( $tmp_path =~ /(\w)/i ) && ( length($desc) >= 3 ) ) {
					print WH "CATE=en#DESC=$desc#LINK=http://www.demonoid.com$tmp_path\n";
				};
			};
		}; # foreach $line ( @CONTENT ) {

	}; # for ($i=1; $i<=5; $i++ ) {
		
}; # sub getNewsDemonoid(){




sub getNewsIsoHunt(){

	my $webpage = "http://isohunt.com/latest.php?mode=bt";
	
	my $page = &http_get("$webpage", $timeout, $ua);
	foreach $line ( split('"', $page ) ) {
		
		if ( $line =~ /\/download\/+(\d)+\/+(\w)+/i ) {
			my ( undef, undef, undef, $desc ) = split('/', $line);
			$desc =~ s/(\.|\+|\-|\,|\;)/ /ig;
			chomp($desc);
			if ( ($desc =~ /(\w)/i) ) {
				$count++;
				print WH "CATE=en#DESC=$desc#LINK=http://isohunt.com$line\n";
			};
		};
	};
}; # &getNewsIsoHunt();



sub getNewsTorrentbox(){

	my $page = &http_get("http://www.torrentbox.com/torrents-browse.php?news", $timeout, $ua);
	foreach $line ( split(/<a href=/i,$page ) ) {
		if ( $line =~ /torrent_details\?id=/ ) {
			my ( undef, $url ) = split('"', $line);
			$url =~ s/amp;//g;
			my (undef,undef, undef, $temp) = split('>', $line);
			$temp =~ s/<\/b//g;
			my (undef, $desc) = split('&nbsp;', $temp);
			my ( $url, undef ) = split(/\&hit=1/ig, $url);
			# print "URL=$url und DESC=$desc\n";
			chomp($desc);
			if ( ($desc =~ /(\w)/i) && ( $url =~ /(\w)/ ) ) {
				$count++; # http://www.torrentbox.com/torrent_details?id=66731&hit=1
				print WH "CATE=en#DESC=$desc#LINK=http://www.torrentbox.com$url\n";
			};
		};
	};
}; # &getNewsTorrentbox();



sub getNewsMeganova(){

	my $page = &http_get("http://www.meganova.org/order-seeds.html", $timeout, $ua);
	foreach $line ( split(/<a href=/i,$page ) ) {
		if ( $line =~ /details/i ) {
			my ( undef, $url ) = split('"', $line);
 			my ($temp, undef) = split('<', $line);
			my (undef, $desc) = split('>', $temp);
			chomp($desc);
			if ( ($desc =~ /(\w)/i) && ( $url =~ /(\w)/ ) ) {
				$count++;
				print WH "CATE=en#DESC=$desc#LINK=http://www.meganova.org$url\n";
			};
		};
	};
}; # &getNewsMeganova();


sub getNewsBtjunkie(){
	
	my $page = &http_get("http://btjunkie.org/?do=latest", $timeout, $ua, '');
	my @CONTENT = split('<a href=', $page);
	foreach $line ( @CONTENT ) {
		$count++;
		if ( ($line =~ /\/torrent\?do=stat/i)) {
			$line =~ s/amp;//i;
			my ( undef, $url, undef, undef, $desc) = split('"', $line );
			my ( undef, $desc ) = split('<', $desc);
			$desc =~ s/>//ig;
			$desc =~ s/^b//ig;
			chomp($desc);
			# print "$desc und $url\n";
			if ( ($desc =~ /(\w)/i) && ( $url =~ /(\w)/ ) ) {
				$count++;
				print WH "CATE=en#DESC=$desc#LINK=http://www.btjunkie.org$url\n";
			};
		};
	};
}; # &getNewsBtjunkie();


sub getNewsTorrentreactor(){
	
	my $page = &http_get("http://www.torrentreactor.to/index.php", $timeout, $ua, '');
	my @CONTENT = split(/<a href=/i, $page);
	foreach $line ( @CONTENT ) {
		$count++;
		if ( ($line =~ /torrents/i) && ($line =~ /view/i) ) {
			$line =~ s/amp;//i;
			my ( undef, $url, undef, $desc) = split('"', $line );
			chomp($desc);
			if ( ($desc =~ /(\w)/i) && ( $url =~ /(\w)/ ) ) {
				$count++;
				print WH "CATE=en#DESC=$desc#LINK=$url\n";
			};
		};
	};
}; # &getNewsTorrentreactor();


sub getNewsMininova(){

	my $mininova = "http://www.mininova.org/today/";
	my $mininova1 = "http://www.mininova.org";
	my $page = &http_get($mininova, $timeout, $ua, ''); 
	my @CONTENT = split('<a href="', $page);

	foreach $line (@CONTENT) {
		if ($line =~ /^\/tor\//i) {
			my ($pre_tor, $tmp) = split('"', $line);
			my ($pre_desc, undef) = split('<', $tmp);
			$pre_desc =~ s/>//g;
			# DEBUG: # print "$mininova/$pre_tor und $pre_desc\n";
			$count++;
			print WH "CATE=en#DESC=$pre_desc#LINK=$mininova1$pre_tor\n";
		};
	};
}; # &getNewsMininova();


##########################
####### opsolate
#########################

# server temp down @ 1.5.2006
# &getNewsSeedler();
sub getNewsSeedler(){
	my $page = &http_get("http://www.seedler.org/de/", $timeout, $ua, '');
	my @CONTENT = split('"', $page);
	my $count;
	foreach $line ( @CONTENT ) {
		$count++;
		if ($line =~ /\/de\/html\/info\/+(\d)/) {
			my ($desc,undef) = split('<',@CONTENT[$count+6]);
			$desc =~ s/>//g;
			print WH "CATE=en#DESC=$desc#LINK=http://www.seedler.org$line\n";
		};
	};
}; # sub getNewsSeedler(){


# &getNewsSnarf();
sub getNewsSnarf(){
	
	&SnarfParser("http://www.snarf-it.org/view/today.html?cat=Anime");
	&SnarfParser("http://www.snarf-it.org/view/today.html?cat=Games");
	&SnarfParser("http://www.snarf-it.org/view/today.html?cat=Hentai");
	&SnarfParser("http://www.snarf-it.org/view/today.html?cat=Misc");
	&SnarfParser("http://www.snarf-it.org/view/today.html?cat=Movies");
	&SnarfParser("http://www.snarf-it.org/view/today.html?cat=Music");
	&SnarfParser("http://www.snarf-it.org/view/today.html?cat=Software");
	&SnarfParser("http://www.snarf-it.org/view/today.html?cat=Television");
	&SnarfParser("http://www.snarf-it.org/view/today.html?cat=Adult");
	
	sub SnarfParser(){
		my $webpage = shift;

		my $page = &http_get("$webpage", $timeout, $ua);
		foreach $line ( split('<a href=',$page ) ) {
		
			if ( $line =~ /viewTorrent/i ) {
				my (undef, $link) = split('"', $line);
				$temp = $link;
				$temp =~ s/-/#/;
				my ( undef,$desc ) = split('#', $temp);
				$desc =~ s/\.html//;
				$desc =~ s/\+/ /;
				$count++;
				print WH "CATE=en#DESC=$desc#LINK=http://www.snarf-it.org$link\n";
			};
		};
	}; # sub SnarfParser(){

};

sub getNewsMetrotorrents(){

	my $page = &http_get("http://www.metrotorrents.info/", $timeout, $ua);
	foreach $line ( split(/<a href=/i,$page ) ) {
		if ( $line =~ /details.php\?id=/ ) {
			my ( undef, $url, $temp ) = split('"', $line);
			$url =~ s/amp;//g;
			my ($desc) = split('<', $temp);
			$desc =~ s/>//g;
			chomp($desc);
			if ( ($desc =~ /(\w)/i) && ( $url =~ /(\w)/ ) ) {
				$count++;
				print WH "CATE=en#DESC=$desc#LINK=http://www.metrotorrents.info$url\n";
			};
		};
	};
}; # &getNewsMetrotorrents();


sub OLDgetNewsIsoHunt(){

	&parser("http://isohunt.com/release.php?ihq=&poster=&cat=-1&ihp=1");
#	&parser("http://isohunt.com/release.php?ihq=&poster=&cat=-1&ihp=2");
#	&parser("http://isohunt.com/release.php?ihq=&poster=&cat=-1&ihp=3");
#	&parser("http://isohunt.com/release.php?ihq=&poster=&cat=-1&ihp=4");
#	&parser("http://isohunt.com/release.php?ihq=&poster=&cat=-1&ihp=5");
#	&parser("http://isohunt.com/release.php?ihq=&poster=&cat=-1&ihp=6");

	sub parser(){
		my $webpage = shift;
		
		my $page = &http_get("$webpage", $timeout, $ua);
		foreach $line ( split(/<a href=/i, $page ) ) {
			$line =~ s/amp;//g;
			#if ( $line =~ /release\/+(\d)+\/\?poster=&cat=/i ) {
			# print "$line\n";
			if ( $line =~ /\/torrents\/+(\w)/i ) {
			
				my ( undef, $url, $temp ) = split('"', $line);
				my (undef, $desc) = split('>', $temp);
				my ( $desc, undef) = split('<', $desc);
				chomp($desc);
				if ( ($desc =~ /(\w)/i) && ( $url =~ /(\w)/ ) ) {
					$count++;
					print WH "CATE=en#DESC=$desc#LINK=$url\n";
				};
			};
		};
	}; # parser()
}; # &getNewsIsoHunt();

# system("perl /root/zoozle/bots/speedtorrenttest.pl");


###### WICHTIG ####### 
# Für das Verzeichnis weiterhin die alten Tabellen 
# define('EMULE_NEWS', 'emule_news3');
# define('TORRENT_NEWS', 'torrent_news3');
# verwenden, weil oft sonst keine Results für einen Tag kommen
# In den normalen Tabellen gilt ab jetz Version NAME6
#######################


sub add_content_toSQLDB() {

	my $filename	= shift;
	my $maincat		= shift;
	my $date		= &getDATE();

	open(RH1,"<$filename") or die "$! - $filename \n";

	foreach (<RH1>) {
		
		my ( $cat, $desc, $link ) = split('#', $_);
		$link =~ s/LINK=//;
		$desc =~ s/DESC=//;
		$cat =~ s/CATE=//;
		
		next if ( $_ !~ /^CATE=/ );
		next if ( $_ =~ /Torrent does not exist/ig );
		next if ( $link =~ /megawerbung/);
		next if ( $desc =~ /<img/ig );
		
		$cat	= &deleteSpecialChars($cat);
		$desc	= &deleteSpecialChars($desc);
		$count++;

		$desc =~ s/&+(\w)+;/ /i;
		$desc =~ s/&#+(\w)+;/ /i;
		$desc =~ s/&+(\d)+;/ /i;
		$desc =~ s/&#+(\d)+;/ /i;

		chomp($cat, $desc, $link);
				
		$dbh->do( 
			qq {
				INSERT DELAYED INTO `torrent_news3` ( `UNID`, `DATE` , `CATG` , `DESC` , `LINK` )
					VALUES (
						'', '$date', '$cat', '$desc', '$link' );
			});

		$dbh->do( 
			qq {
				INSERT DELAYED INTO `torrent_news7` ( `DATE` , `CATG` , `DESC` , `LINK` )
					VALUES (
						'$date', '$cat', '$desc', '$link' );
			});


	};# foreach(){}

	print "Done reading $filename with $count Entries\n";
	close RH1;

};

sub add_content_toREALSQLDB() {

	my $filename = shift;
	my $maincat = shift;
	my $date = &getDATE();

	open(RH1,"<$filename") or die "$! - $filename \n";

	foreach (<RH1>) {
		
		my ( $cat, $desc, $link ) = split('#', $_);
		$link =~ s/LINK=//;
		$desc =~ s/DESC=//;
		$cat =~ s/CATE=//;
		
		next if ( $_ !~ /^CATE=/ );
		next if ( $_ =~ /Torrent does not exist/ig );
		next if ( $link =~ /megawerbung/);
		
		$cat	= &deleteSpecialChars($cat);
		$desc	= &deleteSpecialChars($desc);
		$count++;

		$desc =~ s/&+(\w)+;/ /i;
		$desc =~ s/&#+(\w)+;/ /i;
		$desc =~ s/&+(\d)+;/ /i;
		$desc =~ s/&#+(\d)+;/ /i;

		chomp($cat, $desc, $link);
				
		$dbh->do( 
			qq {
				INSERT DELAYED INTO `torrent7` ( `DATE` , `CATG` , `DESC` , `LINK` )
					VALUES (
						'$date', '$cat', '$desc', '$link' );
			});

	};# foreach(){}

	print "Done reading $filename with $count Entries\n";
	close RH1;

};


sub getDATE() {

	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
	$year = $year+1900;
	$mon = $mon + 1;

	if ( length($mon) == 1 ) {
		$mon = "0". $mon;
	};
	if ( length($mday) == 1 ) {
		$mday = "0". $mday;
	};


	my $date = $year ."-". $mon ."-". $mday;

	return $date;

};

1;
