#!/usr/bin/perl

use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request;
use Tie::File;


$UA			= "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;)";

############# Subroutine zum erstellen eines HTTP Requestes:Start
# Aufgabe: &http_get - erstelle und sende http request an übergebene url
# Rückgabe: content der angefragten uri/url

sub http_get() {
	
	my ($url, $timeout, $useragent, $proxy) = @_;
	my $UA = LWP::UserAgent->new( keep_alive => 1 );

	$UA->agent($useragent);
	$UA->timeout($timeout);
	$UA->max_size(950_000);
	#$UA->proxy(["http"], "http://$proxy") if ($proxy ne '');
  
	my $jar_jar = HTTP::Cookies->new
		(file => ".Cookies" || ".Cookies.txt",
			autosave => 1,
			max_cookie_size => 40960,
			max_cookies_per_domain => 10000, );
			$UA->cookie_jar($jar_jar );
		
	my $req = HTTP::Request->new(GET => $url);
	
	# Pass request to the user agent and get a response back
  	
	$req->referer($url);
	
	my $res = $UA->request($req);

	if ($res->is_success) {
		return $res->content;
  	} else {
      	#return "<html><body> Spider::Easyspider::http_get(): " . $res->status_line . "</body></html>";
		return 0;
  	};

}; # sub http_get {}
############# Subroutine zum erstellen eines HTTP Requestes:Ende


sub deleteSpecialChars() {

	my $del_badchar = shift;
	
		$del_badchar =~ s/&nbsp//g;
		$del_badchar =~ s/nbsp//g;
		$del_badchar =~ s/\`/ /g;
		$del_badchar =~ s/\'/ /g;
		$del_badchar =~ s/\?/+/g;
		$del_badchar =~ s/%//g;
		$del_badchar =~ s/$//g;
		$del_badchar =~ s/§//g;
		$del_badchar =~ s/!//g;
		$del_badchar =~ s/&//g;
		$del_badchar =~ s/\{//g;
		$del_badchar =~ s/\}//g;
		$del_badchar =~ s/\(//g;
		$del_badchar =~ s/\)//g;
		$del_badchar =~ s/\[//g;
		$del_badchar =~ s/\]//g;
		$del_badchar =~ s/\=//g;
		$del_badchar =~ s/\\//g;
		$del_badchar =~ s/\#//g;
		$del_badchar =~ s/\,//g;
		$del_badchar =~ s/\;//g;
		$del_badchar =~ s/\|//g;
		$del_badchar =~ s/\</ /g;
		$del_badchar =~ s/\>/ /g;
		$del_badchar =~ s/\///g;
		$del_badchar =~ s/°//g;
		$del_badchar =~ s/\^//g;
		$del_badchar =~ s/ß/ss/g;
		$del_badchar =~ s/\|//g;
		$del_badchar =~ s/=&szlig;=/ss/g;
		$del_badchar =~ s/\./ /g;
		$del_badchar =~ s/\-/ /g;
		$del_badchar =~ s/ g / /g;
		$del_badchar =~ s/nbsp/ /g;
		$del_badchar =~ s/\+/ /g;
		$del_badchar =~ s/\*/ /g;
		$del_badchar =~ s/\&+\#+(\d)+\;/ /g;

	return ($del_badchar);
	
} # sub deleteSpecialChars() {}


sub AddContentToSqlTORRENT() {

	my $filename	= shift;
	my $date		= &getDATE();

	use DBI();
	my $drh			= DBI->install_driver("mysql");

	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});
	my $count		= 0;

	open(RH1,"<$filename") or warn "$! - $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 AddContentToSql() {


sub AddContentToSqlEMULE() {

	my $filename	= shift;
	my $date		= &getDATE();

	use DBI();
	my $drh			= DBI->install_driver("mysql");

	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});
	my $count		= 0;

	open(RH1,"<$filename") or warn "$! - $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' );
				});

					
			$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 AddContentToSql() {



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;

}; # GetDate



1;