#!/usr/bin/perl

require "functions.pl";

use DBI();
use IO::Handle;

$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 "emule_news.txt";
open(WH,">emule_news.txt");
WH->autoflush(1);

	&getNewsTV();
	&getNewsSaugstube();
	&getNewsTitanesel();
	&getNewsEselfilme();
	&getNewsGoldesel();

	#######################
	#### english sites here
	#######################

	&getNewsShareheaven();
#	&getNewsFucktheinter(); # obsolate

close WH;

&add_content_toSQLDB("emule_news.txt", "ALL");
&add_content_toREALSQLDB("emule_news.txt", "ALL");

$dbh->disconnect;


sub getNewsTV(){
	
	my $page = &http_get("http://tvunderground.org.ru/index.php", $timeout, $ua, '');
	my @CONTENT = split('"', $page);
	my @CONTENT2 = split('\'', $page);
	
	if ( $#CONTENT < $#CONTENT2 ) {
		@CONTENT = @CONTENT2;
	};
	
	foreach $line (@CONTENT) {	
		$linecount++;
		if ($line =~ /english/i || $line =~ /german/i || $line =~ /french/i || $line =~ /italian/i || $line =~ /polish/i) {
			
			if ($line =~ /english/i ) { $lang = "en"; };
			if ($line =~ /german/i ) { $lang = "en"; }
			if ($line =~ /french/i ) { $lang = "fr"; }
			if ($line =~ /italian/i ) { $lang = "it"; }
			if ($line =~ /polish/i ) { $lang = "pl"; }

			my ( $desc_t ) = split(/<\/a>/i, @CONTENT[$linecount+2] );
			my ( undef, $desc ) = split('>', $desc_t );
			$desc =~ s/<a href=//i;
			chomp($desc);
			next if ( $desc !~ /\w/ ); #	&& (length(@CONTENT[$linecount+1]) < 3) ); 
			next if ( $desc =~ /&nbsp/i );
			$count++;
			print WH "CATE=$lang#DESC=$desc#LINK=http://tvunderground.org.ru/@CONTENT[$linecount+1]\n";
		};
	};
};



sub getNewsSaugstube(){
	
	my $page = &http_get("http://www.saugstube-server.to/-saugstube.php", $timeout, $ua, '');
	
	my @CONTENT = split(/<a href=/i, $page);

	foreach $line (@CONTENT) {
		if ($line =~ /javascript/i) {
		
			my ( $js_link, undef )			= split('</font>', $line);
			my ( $js_temp, undef, $desc )	= split('">', $js_link); 
			my ( undef, $pre_link )			= split('\'', $js_temp); 
			
			$desc =~ s/&\#(\d)+;//g;
			$desc =~ s/\#//g;
			next if ( $desc =~ /</ );
			
			chomp($pre_link, $desc);
			$count++; # http://www.saugstube-server.to/-saugstube.php?fRequest=Popup&fSection=divx&fID=4521
			print WH "CATE=de#DESC=$desc#LINK=http://www.saugstube-server.to/-saugstube.php/$pre_link\n";
		};
	};
};



sub getNewsTitanesel(){
	
	my $page = &http_get("http://page.titanesel.nl/default.html", $timeout, $ua, '');
	my @CONTENT = split(/<a href=/i, $page);

	foreach $line (@CONTENT) {
		if ($line =~ /id-(\d)/i) {
			my ($pre_link, undef, $desc) = split('>', $line);
			$desc =~ s/<\/font//g;
			chomp($pre_link, $desc);
			next if ( $pre_link =~ /title=\"/i );
			$count++;
			$link = "http://page.titanesel.nl/$pre_link";
			next if ( $link !~ /^http/i );
			print WH "CATE=de#DESC=$desc#LINK=$link\n";
		};
	}; # foreach
	
}; # sub 



sub getNewsEselfilme() {
	my $page = &http_get("http://eselfilme.com/parents/home.htm", $timeout, $ua, '');
	my @content = split('"', $page);

	my $linecount;
	foreach $line ( @content ) {
		$linecount++;
		if ( $line =~ /javascript/i ) {
			my (undef, $pre_link) = split('\'', $line);
			substr($pre_link, 0,2 ) = "";
			if ( @content[$linecount] =~ /(\>)+(\w{0,200})+(\<)/i ) {
				substr(@content[$linecount], 0,1 ) = "";
				my ($desc) = split('<', @content[$linecount]);
				next if (  ( $desc !~ /(\w)/i ) or ( $desc !~ /(\d)/i ) );
				print WH "CATE=de#DESC=$desc#LINK=http://www.eselfilme.com$pre_link\n"; 
				$count++;
			};
		};
	};		
};


sub getNewsGoldesel {
	
	my $page = &http_get("http://www.goldesel.to/index.php", $timeout, $ua, '');
	my @content = split(/href/i, $page);

	foreach $line ( @content ) {
		
		# für den einen part
		my ( undef, $link1, undef, $desc1 ) = split('"', $line);
		$link1 =~ s/^\///;
		$link1 =~ s/amp;//g;
		my ( undef, $desc ) = split('&quot;', $desc1);
		next if ($desc eq '');
		if ($link1 =~ /(\id)/ ) {
			print WH "CATE=de#DESC=$desc#LINK=http://goldesel.to/$link1\n";
			$count++;
		};
	};		
};




sub getNewsShareheaven(){

	my $page = &http_get("http://www.shareheaven.net/modules.php?name=News", $timeout, $ua);
		
	foreach $line ( split(/<a href=/i, $page ) ) {
		$line =~ s/amp;//g;
		if ( ($line =~ /modules.php\?name=News\&file=article\&sid=/i) && ($line =~ /read more/i) ) {
			my ( undef, $url ) = split('"', $line);
			my (undef, $desc) = split('>', $temp);
			my ( $desc, undef) = split('<', $desc);
			chomp($desc);
			my $temp = &http_get("http://www.shareheaven.net/$url", $timeout, $ua);
			my @title = $temp =~ /<title>(.*?)<\/title>/gis;
			my ( undef, $desc ) = split('Emule Links -', $title[0]);	# <title>ShareHeaven.net - eDonkey, Overnet and Emule Links - The Black Dahlia - (2006) *DVDSCR*</title>
			$desc =~ s/-//g;
			$desc =~ s/^ //g;
			chomp($desc);
			
			if ( ($desc =~ /(\w)/i) && ( $url =~ /(\w)/ ) ) {
				$count++;
				print WH "CATE=en#DESC=$desc#LINK=http://www.shareheaven.net/$url\n";
			};
		};
	};
};



##################
######### obsolate 
###################
#&getNewsEselpsychos();

sub getNewsEselpsychos(){
	
	my $page = &http_get("http://eselpsychos.to/okin.php", $timeout, $ua, '');
	my @CONTENT = split(/<a href=/i, $page);

	foreach $line (@CONTENT) {
		if ($line =~ /neuesfenster/i ) {
		
			my ( $js_link, undef )			= split('</a><br>', $line);
			my ( undef, $pre_link, $desc )	= split('\'', $js_link); 
			substr($desc, 0, 4) = "";
			$desc =~ s/\#//g;
			
			next if ( ($desc !~ /\w/) && (length($pre_link) < 3) ); 
			chomp($pre_link, $desc);
			$count++;
			print WH "CATE=de#DESC=$desc#LINK=$pre_link\n";
		};
	};
};



sub getNewsFucktheinter(){
	
	my $page = &http_get("http://www.fucktheinter.net/index.php", $timeout, $ua, '');
	
	my @CONTENT = split(/<a href=/i, $page);

	foreach $line (@CONTENT) {
		if ($line =~ /fucktheinter\.net\/index\.php\?module=dp_Elinks/i) {
			my (undef, $url, $desc) = split('"', $line);
			$url =~ s/amp;//g;
			my ($desc, undef) = split('<', $desc);
			$desc =~ s/>//g;
			chomp($desc);
			# print "$url AND $desc\n";
			if ( ($desc =~ /(\w)/i) && ( $url =~ /id_el=+[\d]/i || $url =~ /cid=+[\d]/i ) ) {
				$count++;
				print WH "CATE=en#DESC=$desc#LINK=$url\n";
			};
		};
	};
};


###### 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/);
		
		$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 `emule_news3` ( `UNID`, `DATE` , `CATG` , `DESC` , `LINK` )
					VALUES (
					'', '$date', '$cat', '$desc', '$link' );
			});

		$dbh->do( 
			qq {
				INSERT DELAYED INTO `emule_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 `emule7` (`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;
