#!/usr/bin/perl -I/root/zoozle/torrentbot/modules

my $VERION = "0.2";

# renicing to 20 !
system("renice 20 $$");
system("clear");

require "/root/zoozle/new-bots-X/functions.pl";

# module laden
use strict;
no strict "refs";
use TorrentBot::HTTP::Get;
use TorrentBot::HTTP::Head;
use TorrentBot::LINK::ExtractorMem;
use Data::Dumper;

# objekte initialisieren
my $HEAD		= TorrentBot::HTTP::Head->new();
my $GET			= TorrentBot::HTTP::Get->new();
my $LINK		= TorrentBot::LINK::ExtractorMem->new();


# speicherschnittstellen initialisieren
my $AlreadSeenLinks;
my %AlreadSeenLinks		= ();
my @StatusCode			= ();
my @HOSTNAME			= ();
my @LinksToScan			= ();

my $StatusCode;
my $HOSTNAME;
my $TorrentDB;
my $TempStorePath;
my $BadUrls;
my $StatusFile;
my $pid;

my $StayOnSameHost		= 1;
my $AlreadyScannedLinks	= 0;
my $BadUrlCount			= 0;


my $torrentflatfile		= "/root/zoozle/new-bots-X/emule_news.txt";
unlink $torrentflatfile;




my %LinksToScanDE = (

	"http://www.goldesel.to/index.php"								=> 'index\.php\?module=',			# http://www.goldesel.to/index.php?module=movies&action=view&id=34452
	"http://www.power-portal.to/html/power-portal/index.php"		=> 'download\.php\?id=',		# http://www.power-portal.to/html/power-portal/download.php?id=29346
	"http://saugstube-server.to/-saugstube.php"						=> '-saugstube\.php\?Module=Details\&ID=',						# http://saugstube-server.to/-saugstube.php?Module=Details&ID=19878
	"http://page.titanesel.nl/default.html"							=> 'id-',	
	#""	 => '',	

);

my %LinksToScanEN = (
	
	"http://ed2k.earthreactor.com/"											=> 'index\.php\?name=News\&file=article\&sid=',		# http://earthreactor.com/index.php?name=News&file=article&sid=2649
	"http://www.shareheaven.net/modules.php?name=News"						=> 'modules\.php\?name=News\&file=article\&sid=',	# http://www.shareheaven.net/modules.php?name=News&file=article&sid=11858
	"http://tvunderground.org.ru/index.php"									=> 'index\.php\?show=episodes\&sid=',			# http://tvunderground.org.ru/index.php?show=episodes&sid=14797
	"http://www.dead-donkey.com/modules/shares/index.php?section=movies"	=> '\&id=',							# http://www.dead-donkey.com/modules/shares/index.php?section=movies&amp;id=533
	"http://www.dead-donkey.com/modules/shares/index.php?page=2&section=movies&section=movies"	 => '\&id=',	
	"http://www.dead-donkey.com/modules/shares/index.php?page=3&section=movies&section=movies"	 => '\&id=',	
	"http://www.dead-donkey.com/modules/shares/index.php?page=4&section=movies&section=movies"	 => '\&id=',		
	""	 => '',	
	""	 => '',	
	""	 => '',	
	
	#""	 => '',	

);


my %LinksToScanFR = (

	"http://www.letoutedonkey.com/filmnew.php"		=> 'ed2k\:\/\/\|file\|',	# ed2k://|file|Infernal%20Affairs%203%20Limited%20French%20Dvdrip%20Xvid-Magical%20Cd1.avi|734453760|F8BD11F754242F7111FBBCFAE29F6EAB|/
	#""	 => '',	

);


Main();
&getNewsEselfilme();
&AddContentToSqlEMULE($torrentflatfile);

sub Main(){

#	while( my ($url,$regex) = each(%LinksToScanDE) ){
#		&doScanning( $url, $regex, "de" );
#	}; # while( my ($url,$goto) = each(%LinksToScan) ){

	while( my ($url,$regex) = each(%LinksToScanEN) ){
		&doScanning( $url, $regex, "en" );
	}; # while( my ($url,$goto) = each(%LinksToScan) ){

#	while( my ($url,$regex) = each(%LinksToScanFR) ){
#		&doScanning( $url, $regex, "fr" );
#	}; # 
	
	sub doScanning(){

		my $UrltoScan		= shift;
		my $Regex			= shift;
		my $lang			= shift;

		my $Links			= &ScanningPage($UrltoScan,,);
		#print Dumper $Links;

		open(WH,"+>>$torrentflatfile");
		foreach my $link ( @{$Links} ) {
			
			my ( $LinkText, $ParrentUrl, $UrlToScan1 ) = split(' ### ', $link );
			if ( $UrlToScan1 =~ /$Regex/i && $UrlToScan1 =~ /^http/i ) {

				$UrlToScan1 =~ s/\&hit=1//gi;
				
				# überspringe bereits gescannte linktexte
				next if ( exists $AlreadSeenLinks->{$LinkText} );
				$AlreadSeenLinks->{$LinkText} = 0;

				print WH "CATE=$lang#DESC=$LinkText#LINK=$UrlToScan1\n";
			#	print "CATE=$lang#DESC=$LinkText#LINK=$UrlToScan1\n";

			}; # if ( $UrlToScan1 =~ /$Regex/i  ) {

		}; # foreach my $link ( @{$ExtractedLinks} ) {
		close RH;

		%AlreadSeenLinks		= ();
		return 1;

	}; # sub doScanning(){

	return 1;

}; # sub Main(){

# gucken, ob morgen immer noch refused bei isohunt
# print &ScanningPage("http://www.zoozle.net/emule-bittorrent-download/gothic,torrent,,0.html",,);
# exit;


sub ScanningPage(){

	my $UrltoScan			= shift || return 0;
	my $ParrentUrl			= shift || "";
	my $LinkText			= shift || "";


	# bereits gescannte links nicht mehr scannen + keine forum links scannen
	return if ( exists $AlreadSeenLinks->{$UrltoScan} || $UrltoScan =~ /forum/ig );

	# markiere den link als bereits gescannt - tu das hierschon, weil wir später eine menge return anweisung haben
	# und wir ohne die anweisung hier, später eventuell zig links doppelt spidern würden
	$AlreadSeenLinks->{$UrltoScan} = 0;


	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
	@StatusCode				= split('', $HeadHashResultRef->{ 'COD' } );

#	# kehre zurück, wenn kein gültiger header code ausgegeben wurde, der statuscode fehlerhaft ist oder content lenght zu klein ist
#	if ( $StatusCode[0] != 2 ){
#		print "something is wrong status '$StatusCode[0]'\n";
#		return;
#	} elsif ( $HeadStatusCode != 1 ){
#		print "something is wrong head '$HeadStatusCode'\n";
#		return;
#	};

	$AlreadyScannedLinks++;
	my $GetHashResultRef	= $GET->Get( $UrltoScan );
	my $ResponseContent		= $GetHashResultRef->{ 'CNT' };
	my $ResponseObj			= $GetHashResultRef->{ 'OBJ' };

	my $ScanLinkConfig = {
		"OBJ"	=> $ResponseObj,
		"CON"	=> $ResponseContent,
		"CDP"	=> 0,
		"SUD"	=> 1,
		"SDT"	=> 0,	# 0=CurrentDept | 1=dirdept
		"URL"	=> $UrltoScan,
		"PURL"	=> $ParrentUrl,
		"TXT"	=> $LinkText,
	};

#	use Data::Dumper;
#	print Dumper 

	return $LINK->HtmlLinkExtractor($ScanLinkConfig)
	
}; # sub ScanningPage(){


sub getNewsEselfilme() {


	my $Links = &ScanningPage("http://eselfilme.com/parents/home.htm");
	open(WH,"+>>$torrentflatfile");

	foreach my $link ( @{$Links} ) {
	
			#	print "CATE=$lang#DESC=$LinkText#LINK=$UrlToScan1\n";
			# <a href="javascript: openInfo('../popups/74/74704.htm');">
			my ( $LinkText, $ParrentUrl, $UrlToScan1 ) = split(' ### ', $link );
			next if ( $LinkText =~ /100K/i || $LinkText =~ /100 K/i );
			my ( $LINK, undef ) = split('\'', $UrlToScan1);
			substr($LINK, 0,2 ) = "";
			print WH "CATE=de#DESC=$LinkText#LINK=http://www.eselfilme.com$LINK\n"; 
	};

	close WH;
	return 1;

	# rest is obsolate

	use LWP::Simple;
	my $page = get("http://eselfilme.com/parents/home.htm");
	my @content = split('"', $page);
	open(WH,"+>>$torrentflatfile");

	my $linecount;
	foreach my $line ( @content ) {
		my $linecount++;
		if ( $line =~ /javascript/i ) {
			my ($pre_link,undef) = split('>', $line);
			my (undef, $link) = split('\'', $pre_link);
			substr($link, 0,2 ) = "";

			my ( $desc, undef ) = split(/<\/a>/i, @content[$linecount]);
			$desc =~ s/&nbsp;//;
			$desc =~ s/<b>//ig; $desc =~ s/<\/b>//ig;
			$desc =~ s/>//;

			next if ( $desc =~ /bgcolor/i );
			next if ( $desc =~ /</ );
			next if ( $desc =~ />/ );
			next if ( $desc =~ /function/i );

			# print "DESC $desc\n";
			next if ( $desc =~ /100K/i || $desc =~ /100 K/i );
			print "CATE=de#DESC=$desc#LINK=http://www.eselfilme.com$link\n"; 
			
		}; # if ( $line =~ /javascript/i ) {
	}; # foreach $line ( @content ) { 

	close WH;
	return 1;

}; # sub