#!/usr/bin/perl -I/root/zoozle/torrentindexer/modules

# sql initialiseren und datei erstellen

# /server/mysqld/bin/mysql -u root -p zoozle
# SELECT `LINK` FROM `torrent7` INTO OUTFILE "/tmp/torrent.txt"; 

# with a lil changes this is a valid torrentindexer

my $VERION = "2";

# module laden
use strict;
use Data::Dumper;
use Digest::MD5 qw( md5_hex );
use TorrentIndexer::HTTP::Get;
use TorrentIndexer::HTTP::Head;
use TorrentIndexer::LINK::ExtractorMem;

# objekte + variablen initialisieren
my $HEAD				= TorrentIndexer::HTTP::Head->new();
my $GET					= TorrentIndexer::HTTP::Get->new();
my $LINK				= TorrentIndexer::LINK::ExtractorMem->new();

my $TorrentDB			= "/root/zoozle/deadlinkremover/torrent.txt";
my $TempStorePath		= "/root/zoozle/deadlinkremover/temp-torrents";
my $BadUrls				= "/root/zoozle/deadlinkremover/badurls.txt";
my $BadUrlCount			= 0;

my $HashLinks;
my %HashLinks			= ();
my $ScanUntilDept		= 5;
my $CurrentDept		    = 0;

my $ScanDeptType		= 0;	# 0=CurrentDept | 1=diredept
my $StayOnSameHost		= 1;
my $AlreadyScannedFlag	= 0;

my $CurrentUrlsChecked	= 0;

# löschen etc
mkdir $TempStorePath;

####
# &Main("http://torrent.to/torrent/upload/moviez/663/169663/555-www.torrent.to...Jeepers.Creepers.German.DVDRIP.XviD.torrent");
# exit;
####


open(RH,"<$BadUrls") or die;
	while(<RH>){
		my ($timestamp,$link) = split('#', $_);
		chomp($link);
		$link =~ s/^\s+//;
		$link =~ s/\s+$//;
		$HashLinks->{$link} = 0;
	};
close RH;


open(RH,"<$TorrentDB") or die;
	flock(RH,2);
	
	while (<RH>) {
		
		chomp;
		# überspringe bereits gescannte elemente
		next if ( exists $HashLinks->{$_} );
	
		if ( $_ !~ /^http/i ) {
		
			&WriteBadFile($_);
			
		} else { 
			
			&Main( $_ );
			print "CHECKED: [$CurrentUrlsChecked] - BAD: [$BadUrlCount] - $_ \n";
			
		}; # if ( $UrlToScan !~ /^http/i ) {

	}; # while (<RH>) {

close RH;


sub Main() {

	my $UrlToScan			= shift || return;

	#############
	### hole aus einer seite alle torrents raus und scanne sie
	#############

	my $GetHashResultRef	= $GET->Get( $UrlToScan );
	my $ResponseObj			= $GetHashResultRef->{ 'OBJ' };
	my $ResponseContent		= $GetHashResultRef->{ 'CNT' };
	my $CurrentCurrentDept	= 0;

	my $ScanLinkConfig = {
		"OBJ" => $ResponseObj,
		"CON" => $ResponseContent,
		"CDP" => $CurrentDept,
		"SUD" => $ScanUntilDept,
		"SDT" => $ScanDeptType,
		"URL" => $UrlToScan,
	};

	my $ResultLinksContent = $LINK->HtmlLinkExtractor( $ScanLinkConfig );

	foreach my $content ( @{$ResultLinksContent} ) {
		
		my ($linktext, $parrenturl, $UrlToScan ) = split(' ### ', $content );
		
		my $HeadHashResultRef	= $HEAD->Head($UrlToScan);
		my $HeadStatusCode		= $HeadHashResultRef->{ 'STA' };
		my $ContentType			= $HeadHashResultRef->{ 'TYP' };
		my $ContentLength		= $HeadHashResultRef->{ 'LEN' };
		
		# splitte den status code auf, um später das erste zeichen davon herrauszuholen
		my @StatusCode			= split('', $HeadStatusCode);

		# nur status codes mit 2xx sind valide, 3xx sind redirects
		if ( ($StatusCode[0] eq "2" || $StatusCode[0] eq "3") && $ContentLength > 1000 ) {

			if ( $ContentType =~ /bittorrent/i  ) {
				
				# print "DEBUG: DESC $linktext: TORRENT: $urltoscan\n";
				$CurrentUrlsChecked++;

				foreach my $try (1..5) {
				
					# stelle einen GET Request
					my $GetHashResultRef	= $GET->Get( $UrlToScan );

					# print "DEBUG: $UrlToScan is a torrent\n";
					my $TorrentMD5 = md5_hex( $UrlToScan );
					
					unlink "$TempStorePath/$TorrentMD5";
					open(WH,">$TempStorePath/$TorrentMD5") or warn;
						flock(WH,2);
						binmode(WH);
						print WH $GetHashResultRef->{ 'CNT' };	# schreibe den inhalt des torrent files nach $TempStorePath/$TorrentMD5
					close WH;

					# call php script and get the seeder and leecher infos
					my $returnVal		= `/bin/php /root/zoozle/deadlinkremover/torrent-checker.php $TempStorePath/$TorrentMD5`;
					my @SeederLeecher	= split('#', $returnVal);
					my $Seeder			= $SeederLeecher[0];
					my $Leecher			= $SeederLeecher[1];
					
					$Seeder =~ s/^\s+//;
					$Seeder =~ s/\s+$//;
					$Leecher =~ s/^\s+//;
					$Leecher =~ s/\s+$//;

					print "\t [S:$Seeder][L:$Leecher] $UrlToScan \n";
					
					# wenn nach 5 mal probieren immer noch
					if ( ( $Seeder == 0 && $Leecher == 0 ) || $Seeder == 0 || $Seeder eq "" || $Leecher eq "" || length $Seeder == 0 || length $Leecher == 0){  
						
						$BadUrlCount++ if ( $try == 5 );
						&WriteBadFile($UrlToScan) if ( $try == 5 );
						return 1 if ( $try == 5 );

					} else { 
						
						# print "[S:$Seeder][L:$Leecher] [B:$BadUrlCount] $UrlToScan \n";
						return 1;

					}; # if ( $Seeder =! 0 && $Leecher != 0 ) {

					# warte 0.1 sekunden
					select(undef, undef, undef, 0.1 );

				}; # foreach $try (1..5) {

			}; # if ( $ContentType =~ /bittorrent/i  ) {

		# if ( ($StatusCode[0] eq "2" || $StatusCode[0] eq "3") && $ContentLength > 1000 ) { # kein gültiger statuscode -> url ist bad 
		} else { 

			&WriteBadFile($UrlToScan);
			return 1;

		}; # if ( $StatusCode[0] eq "2" || $StatusCode[0] eq "3" ) {
	
	}; # foreach my $content ( @{$ResultLinksContent} ) {

	return 1;

}; # sub Main() {


sub WriteBadFile(){

	my $UrlToScan	= shift;
	my $time		= time();

	# print "DEBUG Negative Torrent Check $UrlToScan\n";
	open(BAD,">>$BadUrls") or warn;
		flock(BAD,2);
		print BAD "$time#$UrlToScan\n";
	close BAD;

	$BadUrlCount++;
	
	return 1;

}; # sub WriteBadFile(){
