#!/usr/bin/perl

use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request;
use Tie::File;

my $file = $ARGV[0];
AddContentToSqlTORRENT($file);
exit;


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);
					
			if ( length($desc) > 2 && $link =~ /^http/i ) {
				
				if ( $link =~ /bitreactor\.to/i ) {
					$link =~ s/download/torrent/ig;
				};
				
				$desc =~ s/(torrents|torrent|www|galaxy|to|more|ubb)//i;
				$desc =~ s/\[+(.*?)+\]//;
				$desc =~ s/\(+(.*?)+\)//;

				$dbh->do( 
					qq {
						INSERT DELAYED INTO `torrent7` ( `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' );
					});

			}; # if (length($desc) > 2) {

		};# 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);
			
			if ( length($desc) > 2 && $link =~ /^http/i ) {

				$dbh->do( 
					qq {
						INSERT DELAYED INTO `emule7` ( `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' );
					});
			
			}; # if (length($desc) > 2) {

		};# foreach(){}

		print "Done reading $filename with $count Entries\n";
	close RH1;

}; # sub AddContentToSql() {


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 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;