#!/usr/bin/perl

#sleep 25000;

###########################
### Autor: Sebastian Enger / B.Sc. 
### Copyright: Sebastian Enger
### Licence: BSD
### Version: 1.1.b  - 20080716@19.Uhr
### Contact: sebastian.enger@gmail.com | icq: 135846444
### Latest Version: PENDRIVE:\Programming\Perl\IncrementalBackup
###########################

use strict;
no strict "subs";
use DBI;
use File::Find;	# perl -MCPAN -e 'force install "File::Find"'
use File::Copy;	# perl -MCPAN -e 'force install "File::Copy"'
use File::Path;	# perl -MCPAN -e 'force install "File::Path"'
# use File::Remove qw(remove); # perl -MCPAN -e 'force install "WordPress::XMLRPC"' 
use WordPress::API::Post; # perl -MCPAN -e 'force install "WordPress::API::Post"'
use Data::Random qw(:all); # perl -MCPAN -e 'force install "Data::Random"' # perl -MCPAN -e 'force install "Date::Calc"'
#use XML::Code; # perl -MCPAN -e 'force install "Curses"'
use HTML::StripTags qw(strip_tags); # perl -MCPAN -e 'force install "HTML::StripTags"'
use Data::Dumper; 
use POSIX qw(strftime);
use Regexp::Common qw(time);
use File::Basename;
use LWP::Simple;
use LWP::UserAgent;
use HTML::LinkExtor;
use HTML::LinkExtractor;
use URI::URL;
use LWP::UserAgent;
use HTTP::Status ();

my $count 		= 0; 
my $SourceFolder = 'E:\Working 2012\www.moviestreamz.com\mirror\www.movie2k.to';
my $SourceFolder1 = '';
my $SourceFolder2 = '';
my $CopySrcFolder = 'E:\Working 2012\www.moviestreamz.com\crawler\backup';

my $e2;
my $string1;
my $html;
my $title;
my $text;
my $catg;
my $string;
my $lastlength = "";
my $rcount = 0;
my $Hash;
my %Hash = ();
my %mon2num = (
	  "January" => 1,
	  "February" => 2,
	  "March" => 3,
	  "April" => 4,
	  "May" => 5,
	  "June" => 6,
	  "July" => 7,
	  "August" => 8,
	  "September" => 9,
	  "October" => 10,
	  "November" => 11,
	  "December" => 12
);

my $start			= 1;	# min rand time to wait between wp post requests
my $ende 			= 1;	# max rand time to wait between wp post requests
my $start_date		= '2013-08-01';
my $scriptname 			= basename($0);
my $logfile  		= 'E:\Working 2012\www.moviestreamz.com\crawler\logfile\\';
$logfile 			.= $scriptname."_log.txt";

my $sd 				= $SourceFolder =~ /(\w+)\.(com|net|org|eu|de|\.co\.nz|biz|to|tv|ws|in|me)/ig;
my $domain 			= "$1.$2"; 
my $CopyToFolder 	= "$CopySrcFolder\\$domain";
mkdir $CopyToFolder;
copy($0,$CopyToFolder) or die "Copy failed: $!";

#my $dbh = DBI->connect("dbi:mysql:dbname=nookiestar_pornrage","root","###########99", {
#   AutoCommit => 1,#0=$dbh->commit(); 
 #  RaiseError => 0,
 #  mysql_auto_reconnect => 1,
 #});
  
# prepare files from SourceFolder
#eval {
find(\&SourceStructure, $SourceFolder);
####TODO: TV Shows -> change title: find(\&SourceStructure, $SourceFolder1);
#find(\&SourceStructure, $SourceFolder2);
#};

print "I found $count movie entries\n";
#$dbh->disconnect();
exit;

sub SourceStructure(){

    # $_ is set to the current file name
    # $File::Find::dir is set to the current directory
    # $File::Find::name is set to "$File::Find::dir/$_"
    # you are chdir()'d to $File::Find::dir 

	my $SourceFilename		= $File::Find::name;
	my $SourceDirname		= $File::Find::dir;
	my $string = "";
	
	#if ( $SourceFilename =~ /index\.(shtml|htm|html|php)_p=/ig ){ #/\.html/ig ){#&& $SourceFilename =~ /posts/ig ){
	if ( $SourceFilename =~ /\.(shtml|htm|html|php)_p=/ig || $SourceFilename =~ /watch-movie/ig ){ 
	#	if ( $SourceFilename =~ /stream\.html/ig ) {
	
	#	return if ( $_ !~ /^(\w+)/ig );
	#	return if ( $_ !~ /(\.html)/ig );
		return if ( $SourceFilename =~ /index/ig );
		return if ( $SourceFilename =~ /feed/ig );
		return if ( $SourceFilename =~ /trackback/ig );
		return if ( $SourceFilename =~ /page/ig );
		return if ( $SourceFilename =~ /author/ig );
		return if ( $SourceFilename =~ /years/ig );
	
	#	print "Dumper: $SourceFilename\n";
	{
	  local $/ = undef;
	  open FILE, "$SourceFilename" or return "Couldn't open file: $SourceFilename $!";
	  binmode FILE;
	  $string = <FILE>;
	  close FILE;
	}
					
	#my @split = split("\n", $string);

	# Get Title from filename: Start
#	my $temp;
#	my @temp = split("\/",$SourceFilename);	
#	my $title = $temp[$#temp]; #$title = "$temp[$#temp]";
#	$title =~ s/^(\d{1,10})-//;
	
#	$title =~ s/(\.html|_|-|\.)/ /ig;
#	$title =~ s/[A-Z][a-z]*+(?!\W)\K/ /gx;
#	$title = ucfirst($title);
	#next if $title2 =~ /(feed|\&|\$|index)/ig;
	#print ">>>>>>>>>>>>>>> ".length($title).": $title <<<<<<<<<<<<< \n";
	# Get Title from filename: End
				
#	my ($title_temp) = ($string =~ m#<!-- \#BeginEditable \"title\" -->\s*(.*?)\s*<!-- \#EndEditable -->#igs);
	my ($title_temp) = ($string =~ m#<TITLE>\s*(.*?)\s*</TITLE>#igs);
#	my ($title,undef,undef,undef) = split('&raquo;', $title_temp);
	my ($title,undef,undef) = split('(\||-|:|&laquo;|&raquo;|\/)', $title_temp);
#	$title = "$title_temp";
#	my $org_title;
#	
#	my @path = split('serie', $SourceFilename);
#	my $org_title = @path[1];
#	$org_title =~ s/-/ /ig;
#	$org_title =~ s/^\\//ig;
#	$org_title =~ s/^\///ig;
#	$org_title =~ s/\// - /ig;
#	$title = $org_title;
	
#	print "Title: $org_title\n";
#	return;
	
	$title =~ s/(Movie|Download|Stream|Source|blog|org|&laquo;|&raquo;|online|gucken|watch)//ig;
	$title =~ s/(stream|online|anschauen)//ig;
	$title =~ s/(Free Downloads|Download)//ig;
	$title =~ s/(DOWNEU\.(\w)|DOWNEU)//ig;
	$title =~ s/(MovStream\.info|Hnmovies)//ig;
	$title =~ s/(TVsource\.in|tvsource)//ig;
	$title =~ s/HD-Area\.org\://ig;
	$title =~ s/HD-Area\.org://ig;
	$title =~ s/Moviekk\.net//ig;
		
	$title =~ s/\&//ig;
	$title =~ s/\./ /ig;
	$title =~ s/\'//ig;
	
	$title =~ s/\'//ig;
	#$title =~ s/\-/ /ig;
	#$title =~ s/-/ /g;
	$title =~ s/_/ /g;
	$title =~ s/([\w']+)/\u\L$1/g;
	
	#$title =~ s/[^[:ascii:]]/ /g;
	$title =~ s/&(\w+);//g;
	$title =~ s/#(\w+);//g;
	$title =~ s/\s+/ /ig;
		
	next if length $title <= 2;

	my $content 		= "";
	my @split 			= split("\n", $string);
	
	# den content block mit bild, text und download links entgegen nehmen
	my (@content) 		= ($string =~ m#<div class="moviedescription">\s*(.*?)\s*<\/div>#igs);
	#$content = strip_tags( $content, "" );
	my @split_content 	= split("\n", @content[$#content]);
#	print "File: $SourceFilename\n";
#	print Dumper @content;
	
#	JavaScript Cracking Support
#	my (@content1) 		= ($string =~ m#<script type="text/javascript">var subcats\s*(.*?)\s*</script>#igs);
#	my @split_content1 	= split("\n", @content1[$#content1]);
#	
#	# use this function if download/stream links are hidden in javascript code
#	my $html = parseContent(@split_content1); 
#	my @split_content 	= split("\n", $html);
		
	# bild code vorbereiten
	my $ImgCode 		= prepareImage($title, @content);
	# print "Bildcode: $ImgCode\n"; exit; 
	
	# datums code vorbereiten
	my ($content_extra) = ($string =~ m#<span class="author">\s*(.*?)\s*title="View all posts by this author"#igs);
	#print Dumper $content_extra; exit;
	
	my $date 			= createPostDate($content_extra, "random");
	#print "Date: $date\n"; exit; return; 
	
	my ($content_extra) = ($string =~ m#Genre:\s*(.*?)\s*Length#igs);
	# category code vorbereiten
	my $catg			= categorize($content_extra, $title);
	#print "Kategorie: $content_extra --->>> $catg\n"; return;#exit;
		
	my $header			= createHeader($title); 
	my $footer			= createFooter($title); 
			
	my $fullhtml = "";
	#$fullhtml .= "<div><div>";
	$fullhtml .= $header;
	$fullhtml .= $ImgCode;
	for ( my $i=0;$i<=$#split_content; $i++){
		my $output = @split_content[$i];
		#print "$i) $output\n";
		if ( $output !~ /(spoiler)/ig){
			my $content1 = strip_tags( $output, "" );
			#$fullhtml .= $output;
			$fullhtml .= $content1;
		}
	} # for ( my $i=0;$i<=$#split_content; $i++){

	$fullhtml .= "<br /><br />";
	
	## für kkiste.to
	my (@streamlink1) = ($string =~ m#<div id="emptydiv">\s*(.*?)\s*<div id="underplayer">#igs); # working
	my (@streamlink2) = ($string =~ m#Here you see hoster listed, which might be faster\s*(.*?)\s*<div id="underplayer">#igs);

	foreach my $streamlink (@streamlink1){
		my @cnt = split("(\"|\')", $streamlink);
		foreach my $e (@cnt){
			if ( $e =~ /^http/ig && $e !~ /(movie2k|affbuzzads|iframe)/ig ){
				print "Stream Link EINS: <a href=\"$e\" target=\"_blank\" rel=\"nofollow\" title=\"$title als Stream ansehen\">$title Kinofilm als Stream & Streaming anschauen</a><br />\n"; 
				$fullhtml .= "Stream Link: <a href=\"$e\" target=\"_blank\" rel=\"nofollow\" title=\"$title als Stream ansehen\">$title Kinofilm als Stream & Streaming anschauen</a><br />";	
			}
		}
	} 

	foreach my $streamlink (@streamlink2){
		my @cnt1 = split("(\"|\')", $streamlink);
		foreach my $e2 (@cnt1){
			if ( $e2 =~ /^http/ig && $e2 !~ /(movie2k|affbuzzads|iframe|get|adobat|mgid)/ig ){
				print "Stream Link ZWEI: $e2\n"; 
				$fullhtml .= "Stream Link: <a href=\"$e2\" target=\"_blank\" rel=\"nofollow\" title=\"$title als Stream ansehen\">$title Kinofilm als Stream & Streaming anschauen</a><br />";	
			}
		}
	}

	## für kkiste.to
		
	$fullhtml .= $footer;
	$fullhtml =~ s/href/rel="nofollow" href/ig;	# setze alle links in dem post auf rel nofollow
	$fullhtml = changeAffilateLinks($fullhtml);

#	if ($fullhtml !~ m/ecostream\.tv/ig ){
#		return;
#	}

	# Debug Test Output:Start 
###print $fullhtml;	exit;
	# Debug Test Output: ENd
	
	my $lenght 			= length($fullhtml);	
	
	if ( $Hash{$title} ) {
		#print "ERROR: Double Title - $title ---> Hash Count: ".keys(%Hash)." \n";
		#return;
	} else {	
		$Hash{$title} = $title;
	}
	
#	if ( $lenght>=100&&$lenght<=150000){
#	
#		print "File: $SourceFilename\n";
#		print "$count) Title: '$title' \n";
#		print "Date: $date\n";
#		print "Category: $catg\n";
#		print "Content Lenght: $lenght\n";
#		$content = strip_tags( $content, "" );
#		$content =~ s/\s+/ /ig;
##		$content =~ s/[^[:ascii:]]/ /ig;
##		$content =~ s/\[.*?\]/ /ig;
##		#$content = substr( $content, 0, -584,"");
#		print "Content: $fullhtml\n";		exit;
#		$count++;
#		
#		if ($count==836){
#			print "Content: $fullhtml\n";	
#			exit;
#		}
#	
#	}
#	return;
			
	if ( $lenght>150&&$count<=0){
		$count++;
		print "I am skipping: $count of 100K\n";
	};			
	
	if ( $lenght>100&&$lenght<190000&&$count>=0){
			
		# remove html from text 
		# $content = strip_tags( $content, "" );
		#$fullhtml =~ s/(\w+)+\.(com|net|org|eu|de|\.co\.nz|biz)/KinoDownloads.com/ig;
		#$fullhtml =~ s/\.(com|net|org|eu|de|\.co\.nz|biz)/KinoDownloads.com/ig;

		#$content =~ s/[^[:ascii:]]/ /ig;
		#$fullhtml =~ s/\[.*?\]/ /ig;
			
		my $p = WordPress::API::Post->new({
			  username => 'cereal',
			  password => '###########99',
			  proxy => 'http://www.moviestreamz.com/xmlrpc.php',
			# proxy => 'http://www.buzzerstar.com/devel/xmlrpc.php',
		}); 
		
		my $is_catg = "";
		my $SourceFilename = "";

		if ( $title =~ /(Animation|anime)/ig || $catg =~ /(Animation|anime)/ig){
			$is_catg = 'Animation'; #
		} elsif ( $title =~ /(Biografie|bio|Biography)/ig || $catg =~ /(Biografie|bio|Biography)/ig){
			$is_catg = 'Biography'; #
		} elsif ( $title =~ /(Comedy|Komödie)/ig || $catg =~ /(Comedy|Komödie)/ig){
			$is_catg = 'Comedy'; #
		} elsif ( $title =~ /(Dokumentation|Doku|Documentation|docu)/ig || $SourceFilename =~ /(documentaries)/ig || $catg =~ /(Dokumentation|Doku|Documentation|docu)/ig){
			$is_catg = 'Documentation'; #
		} elsif ( $title =~ /(Dramatik|Drama)/ig || $catg =~ /(Dramatik|Drama)/ig){
			$is_catg = 'Drama'; #
		} elsif ( $title =~ /(Zeichentrick|Comic)/ig || $catg =~ /(Zeichentrick|Comic)/ig){
			$is_catg = 'Animation'; #
		} elsif ( $title =~ /Western/ig || $catg =~ /Western/ig){
			$is_catg = 'Western'; #
		} elsif ( $SourceFilename =~ /(Tv Serien|Serien|Series|TV|show)/ig || $title =~ /(Tv Serien|Serien|Series|TV|show)/ig || $catg =~ /(Tv Serien|Serien|Series|TV|show)/ig || $title =~ m/S.(\d{1,3}).E(\d{1,3})/ig && $title =~ m/S.(\d{1,3})(\s)E(\d{1,3})/ig || $catg =~ m/S.(\d{1,3}).E(\d{1,3})/ig && $catg =~ m/S.(\d{1,3})(\s)E(\d{1,3})/ig){
			$is_catg = 'TV Series'; #
		} elsif ( $title =~ /(Thriller|Spannung)/ig || $catg =~ /(Thriller|Spannung)/ig){
			$is_catg = 'Thriller'; #
		} elsif ( $title =~ /(Sport|WWE|Wrestling|Wrestlemania|soccer|football|madden)/ig || $catg =~ /(Sport|WWE|Wrestling|Wrestlemania|soccer|football|madden)/ig){
			$is_catg = 'Sport'; #
		} elsif ( $title =~ /(Science Fiction|Sci Fi|SciFi)/ig || $catg =~ /(Science Fiction|Sci Fi|SciFi)/ig){
			$is_catg = 'Science Fiction'; #
		} elsif ( $title =~ /(Romanze|Romantik|Romance)/ig || $catg =~ /(Romanze|Romantik|Romance)/ig){
			$is_catg = 'Romance'; #
		} elsif ( $title =~ /(Kriminalfilm|Krimi|Crime|CSI)/ig || $catg =~ /(Kriminalfilm|Krimi|Crime|CSI)/ig){
			$is_catg = 'Crime'; #
		} elsif ( $title =~ /(Kriegsfilm|Krieg|War)/ig || $catg =~ /(Kriegsfilm|Krieg|War)/ig){
			$is_catg = 'War'; #
		} elsif ( $title =~ /(Kinderfilm|Kinder|Child)/ig || $catg =~ /(Kinderfilm|Kinder|Child)/ig){
			$is_catg = 'Animation'; #
		} elsif ( $title =~ /Horror/ig || $catg =~ /Horror/ig){
			$is_catg = 'Horror'; #
		} elsif ( $title =~ /(Fantasie|Fantasy)/ig || $catg =~ /(Fantasie|Fantasy)/ig){
			$is_catg = 'Fantasy'; #
		} elsif ( $title =~ /(Abenteuer|adventure)/ig || $catg =~ /(Abenteuer|adventure)/ig){
			$is_catg = 'Adventure'; #	
		} elsif ( $title =~/(sex|fick|schwanz|teen|muschi|bdsm|blasen|porn|geil|poppen|arsch|vibrator|versaut|porn|porno|fuck|xxx)/ig || $fullhtml=~/(sex|fick|schwanz|teen|muschi|bdsm|blasen|porn|geil|poppen|arsch|vibrator|versaut|porn|porno|fuck|xxx)/ig || $catg =~ /(sex|fick|schwanz|teen|muschi|bdsm|blasen|porn|geil|poppen|arsch|vibrator|versaut|porn|porno|fuck|xxx)/ig){
			$is_catg = 'Porn Movies Streams'; #	
		} elsif ( $title =~ /(dvd|movie|rip|bdrip|HDTV|High Quality|High Definition|720p|720|1080|1080p|blu|ray|x264|MP4|mkv)/ig || $catg =~ /(dvd|movie|rip|bdrip|HDTV|High Quality|High Definition|720p|720|1080|1080p|blu|ray|x264|MP4|mkv)/ig){
			$is_catg = 'High Definition Streams'; #
		} elsif ( $title =~ /(Action|Aktion)/ig || $catg =~ /(Action|Aktion)/ig){
			$is_catg = 'Action'; # todo: auf kd.com als category anlegen
		} elsif ( $title =~ /(cam|xvid|divx|screener|movie)/ig || $catg =~ /(cam|xvid|divx|screener|movie)/ig){
			$is_catg = 'Movie Streams'; #
		} else {
			$is_catg = 'Movie Streams'; #
		};
		
		$title = "Free $title $is_catg Streaming & Downloads"; 
		if ($title =~ m/Porn Movies Streams/ig){
			$title =~ s/Movies Streams//i;
		}
		if ($title =~ m/Movie Streams/ig){
			$title =~ s/Streams//i;
		}
		if ($title =~ m/High Definition Streams/ig){
			$title =~ s/Streams//i;
		}
		
		# post to wordpress
		my $tags = $title; $tags =~ s/\s+/, /ig;
		$p->title($title);
		$p->description($fullhtml);  
		$p->mt_keywords("$title, $tags, $catg");
		$p->dateCreated($date); # set the date for the post
		$p->categories([$is_catg]);
		
		eval {
			check();
			print "$is_catg -> ($count) -> added($date): $title --> Lenght: $lenght \n";
			$p->save; # save to wordpress
			
			open(W,">$logfile");
				print W $count if $count =~ /(\d)/ig;
			close W;
			
			undef $fullhtml;
			undef $p;
			$count++;
			
			#bei $start=200 und $ende=100; bekommt man eben zufallszahlen in dem bereich 100-200. 
			#my $rand = int(rand($ende-$start)+$start);
			#select(undef, undef, undef, $rand);
			select(undef, undef, undef, 0.35);
		}
	}
	
	}; # if ( $SourceFilename =~ /\.txt/ig ){#&& $SourceDirname =~ /Team/ig){
	
}; # sub SourceStructure(){

sub parseContent(){

	my @content = shift;
	my $return_string = "";
	my $return_string_streams = "";
	
	my $D;
	my %D = ();

	for (my $i=0;$i<=$#content;$i++){
		
		my $content = @content[$i];
		my @arr = split("\"",$content);
		my $store = "";
		
		foreach my $array (@arr){
			if ( $array =~ /^http/ig){
				$array =~ s/\\//ig;
				$store = $array;
			}
			if ($array =~ m/download/ig && !exists $D{$store}){
				$return_string .=<<EOT;
					Download: <a href="$store" title="$title Stream High Quality Download" target="_blank">$title (Download)</a><br />
EOT
				$D{$store} = $store;
			};
			if ($array =~ m/stream/ig && !exists $D{$store}){
				$return_string .=<<EOT;
					Stream: <a href="$store" title="Starte den $title Movie Stream" target="_blank">$title (Stream)</a><br />				
EOT
				$D{$store} = $store;
			};
			#<iframe src="$store" marginwidth="0" marginheight="0" scrolling="NO" frameborder="0" height="362" width="653"><br /><br />
		} # foreach my $array (@arr){
	} # for (my $i=0;$i<=$#content;$i++){
	
	return "$return_string $return_string_streams\n";
}

sub URLDecode {
my $theURL = shift;
$theURL =~ tr/+/ /;
$theURL =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
$theURL =~ s/<!--(.|\n)*-->//g;
return $theURL;
}

sub createFooter(){

	my $title = shift;
	my $date 	= strftime "%d.%m.%Y", localtime;
	
	my $s1 = 1000;
	my $e1 = 10000;
	
	my $s = 10000;
	my $e = 100000;
	my $rand = int(rand($e1-$s1)+$s1);
	my $rand_d = int(rand($e-$s)+$s);
		
	my $html =<<END;
<table width="100%">
<tr>
<td style="text-align: center; font-size: 16px; font-weight: bold;" bgcolor="#FFFFFF">
<a target="_blank" href="http://www.moviestreamz.com/downloads.php?file=$title" title="Downloade $title"><img src="http://www.moviestreamz.com/images/download.gif" alt="Downloade $title"></a>
<br /><br />
<a target="_blank" href="http://www.moviestreamz.com/highspeed.php" title="Highspeed Downloads">Tipp: Lade hier mit FULLSPEED deine Kinofilme herunter / Download with Full Speed</a>
</td>
</tr>
</table>
</div>
<br /><br />
<a target="_blank" href="http://www.moviestreamz.com/downloads.php?file=$title" title="Downloade $title"><img src="http://www.moviestreamz.com/pictures.php?picture=$title" alt="Downloade $title" /></a>
END
	
	return $html;
	
} # sub createFooter(){

sub createHeader(){

	my $title	= shift;
	my $date 	= strftime "%d.%m.%Y", localtime;
	#my $date = '<?php date("D M j G:i:s T Y"); ?>'; 
	
	my $s1 = 1000;
	my $e1 = 10000;
	
	my $s = 10000;
	my $e = 100000;
	my $rand = int(rand($e1-$s1)+$s1);
	my $rand_d = int(rand($e-$s)+$s);
		
	my $html =<<END;
<h1>Movie und Kinofilm $title hier im kostenlosen Stream & Streaming</h1>
<h2><a href="http://www.moviestreamz.com/downloads.php?file=$title" target="_blank" style="font-size: 18px; color:red" title="Download now $title"><b>Klicke hier zum downloaden: <br />$title</b></a></h2>
<br><br>
Bis zum $date wurden $rand Downloads mit ca. $rand_d Kb/s gemacht
<br><br>
END
	
	return $html;
	
} # sub createHeader(){

sub categorize(){
	
	my $cnt = shift;
	my $title = shift;
	my @c 	= split("\"",$cnt);
	
	my $string;
	
	foreach my $line (@c){
		#print "Dumper: $line\n";
		if ($line=~/(category|c=|tag|label|genre)/ig){
			# print "Dumper: $line\n";
			$string .= $line . " ";
		}
	}
	
	$string = $cnt;
	
	if ( $title =~ /(Animation|anime)/ig || $string =~ /(Animation|anime)/ig){
		return 'Animation';
	} elsif ( $title =~ /(Biografie|bio|biography)/ig || $string =~ /(Biografie|bio|biography)/ig){
		return 'Biography';
	} elsif ( $title =~ /(Comedy|Komödie)/ig || $string =~ /(Comedy|Komödie)/ig){
		return 'Comedy';
	} elsif ( $title =~ /(Dokumentation|Doku|Documentation|docu)/ig || $string =~ /(Dokumentation|Doku|Documentation|docu)/ig){
		return 'Documentation';
	} elsif ( $title =~ /(Dramatik|Drama|Dramatic)/ig || $string =~ /(Dramatik|Drama|Dramatic)/ig){
		return 'Drama';
	} elsif ( $title =~ /(Zeichentrick|Comic|Anime|Animation)/ig || $string =~ /(Zeichentrick|Comic|Anime|Animation)/ig){
		return 'Animation';
	} elsif ( $title =~ /Western/ig || $string =~ /Western/ig){
		return 'Western';
	} elsif ( $title =~ /(Tv Serien|Serien|Series|TV)/ig || $title =~ m/S.(\d{1,3}).E(\d{1,3})/ig && $title =~ m/S.(\d{1,3})(\s)E(\d{1,3})/ig || $string =~ /(Tv Serien|Serien|Series|TV)/ig || $string =~ m/S.(\d{1,3}).E(\d{1,3})/ig && $string =~ m/S.(\d{1,3})(\s)E(\d{1,3})/ig){
		return 'TV Series';
	} elsif ( $title =~ /(Thriller|Spannung)/ig || $string =~ /(Thriller|Spannung)/ig){
		return 'Thriller';
	} elsif ( $title =~ /(Sport|WWE|Wrestling|Wrestlemania|soccer|football|madden)/ig || $string =~ /(Sport|WWE|Wrestling|Wrestlemania|soccer|football|madden)/ig){
		return 'Sports';
	} elsif ( $title =~ /(Science Fiction|Sci Fi|SciFi)/ig || $string =~ /(Science Fiction|Sci Fi|SciFi)/ig){
		return 'Science Fiction';
	} elsif ( $title =~ /(Romanze|Romantik|Romance)/ig || $string =~ /(Romanze|Romantik|Romance)/ig){
		return 'Romance';
	} elsif ( $title =~ /(Kriminalfilm|Krimi|Crime|CSI)/ig || $string =~ /(Kriminalfilm|Krimi|Crime|CSI)/ig){
		return 'Crime';
	} elsif ( $title =~ /(Kriegsfilm|Krieg|War)/ig || $string =~ /(Kriegsfilm|Krieg|War)/ig){
		return 'War';
	} elsif ( $title =~ /(Kinderfilm|Kinder|Child)/ig || $string =~ /(Kinderfilm|Kinder|Child)/ig){
		return 'Animation';
	} elsif ( $title =~ /Horror/ig || $string =~ /Horror/ig){
		return 'Horror';
	} elsif ( $title =~ /(Fantasie|Fantasy)/ig || $string =~ /(Fantasie|Fantasy)/ig){
		return 'Fantasy';
	} elsif ( $title =~ /(Abenteuer|adventure)/ig || $string =~ /(Abenteuer|adventure)/ig){
		return 'Adventure'; #	
	} elsif ( $title =~/(sex|fick|schwanz|teen|muschi|bdsm|blasen|porn|geil|poppen|arsch|vibrator|versaut|porn|porno|fuck|xxx)/ig || $string =~ /(sex|fick|schwanz|teen|muschi|bdsm|blasen|porn|geil|poppen|arsch|vibrator|versaut|porn|porno|fuck|xxx)/ig){
		return 'Porn Movies'; #	
	} elsif ( $title =~ /(dvd|movie|rip|bdrip|HDTV|High Quality|High Definition|720p|720|1080|1080p|blu|ray|x264|MP4|mkv)/ig || $string =~ /(dvd|movie|rip|bdrip|HDTV|High Quality|High Definition|720p|720|1080|1080p|blu|ray|x264|MP4|mkv)/ig){
		return 'High Definition Movies'; #
	} elsif ( $title =~ /(Games|Spiele|PC|PS3|Playstation|Xbox|Wii|PSP|3DS|DS)/ig || $string =~ /(Games|Spiele|PC|PS3|Playstation|Xbox|Wii|PSP|3DS|DS)/ig){
		return 'Games'; # todo: auf kd.com als category anlegen
	} elsif ( $title =~ /(Action|Aktion)/ig || $string =~ /(Action|Aktion)/ig){
		return 'Action'; # todo: auf kd.com als category anlege
	} elsif ( $title =~ /(cam|xvid|divx|screener|movie)/ig || $string =~ /(cam|xvid|divx|screener|movie)/ig){
			return 'Movie Streams'; #
	} else {
	#default:	
		#return 'Movie Downloads'; #
		return 'Movie Streams';
	}

}; # sub categorize(){

sub createPostDate(){
		
	my $content 			= shift;
	my $random 				= shift;
	my ($day,$month,$year) 	= (undef,undef,undef);
	my $scriptname 			= basename($0);

	#print "Dumper: $content\n";
	if ( $content =~ /(\d{1,2})\.(\d{1,2})\.(\d{2,4})/ig ) { # deutsches datum
		#print "day: $1 , month: $2 , year: $3 \n ";
		($day,$month,$year) = ($1,$2,$3);
	} elsif ( $content =~ /(\d{1,2})-(\d{1,2})-(\d{2,4})/ig ) { # deutsches datum
		#print "day: $1 , month: $2 , year: $3 \n ";
		($day,$month,$year) = ($1,$2,$3);
	} elsif ( $scriptname =~ /movie-blog/ig ) {
		#if ( $content =~ /(\d{1,2})\.(\s)(\w{2,20})(\s)(\d{2,4})/ig ) { # deutsches datum
		my ($tmp_date) = $content =~ /$RE{time}{y4md}/ig;
		($year,$month,$day) = split("\/",$tmp_date);
	} elsif ( $scriptname =~ /moviebox/ig ) {
		if ( $content =~ /(\w{2,15})(\s)(\d{1,2}),(\s)(\d{2,4})/ig ) { # deutsches datum
			($month,$day,$year) = ($1,$3,$5);
		}
	} elsif ( $scriptname =~ /hd-world/ig ) { # 4. April 2012 
	#	print "I am in\n";
		if ( $content =~ /(\d{1,2})\.(\s)(\w{2,15})(\s)(\d{2,4})/ig ) { # deutsches datum
	#	if ( $content =~ /(\s)(\d{1,2})\.(\s)/ig ) { # deutsches datum
			($day,$month,$year) = ($1,$3,$5);
		}
	} elsif ( $scriptname =~ /tvsource/ig ) { # 4. April 2012 
	#	print "I am in\n";
		if ( $content =~ /(\w{3,20})(\s)(\d{1,15}),(\s)(\d{2,4})/ig ) { # deutsches datum
			($day,$month,$year) = ($3,$1,$5);
		}
	} elsif ( $scriptname =~ /hnmovies/ig ) { # 4. April 2012 
	#	print "I am in\n";
		if ( $content =~ /(\w{3,20})(\s)(\d{1,15}),(\s)(\d{2,4})/ig ) { # deutsches datum
			($day,$month,$year) = ($3,$1,$5);
		}
	} elsif ( $scriptname =~ /downeu/ig ) { # 4. April 2012 
	#	print "I am in\n";
		if ( $content =~ /(\d{1,2})(\s)(\w{1,25})(\s)(\d{2,4})/ig ) { # deutsches datum
			($day,$month,$year) = ($1,$2,$5);
		}
	} elsif ( $scriptname =~ /sharepirate/ig ) { # 4. April 2012 
	#	print "I am in\n";
		if ( $content =~ /(\w{3,20})(\s)(\d{1,15}),(\s)(\d{2,4})/ig ) { # deutsches datum
			($day,$month,$year) = ($3,$1,$5);
		}
	} elsif ( $scriptname =~ /downtr/ig ) { # 4. April 2012 
	#	print "I am in\n";
		if ( $content =~ /(\d{1,2})(\s)(\w{2,15})(\s)(\d{2,4})/ig ) { # deutsches datum
	#	if ( $content =~ /(\s)(\d{1,2})\.(\s)/ig ) { # deutsches datum
			($day,$month,$year) = ($1,$3,$5);
		}
	} elsif ( $scriptname =~ /movstreams/ig ) { # 4. April 2012 
	#	print "I am in\n";
		if ( $content =~ /(\w{3,30})(\s)(\w{2,15})(\s)(\d{2,4})/ig ) { # deutsches datum
	#	if ( $content =~ /(\s)(\d{1,2})\.(\s)/ig ) { # deutsches datum
	#		($month,$day,$year) = ($1,$3,$5);
	#		print "($month,$day,$year)\n";
		}
	} 
	
	#$month = postDate($month);
	#print "day: $day month: $month year: $year\n ";

	#print "day: $day\n";
	#print "month: $month\n";
	#print "year: $year\n";
	
	
	if ( length($day)==1){
		$day = "0$day";
	}
	if ( length($month)==1){
		$month = "0$month";
	}
	if ( length($year)==2){
		$year = "20$year";
	}
	
	if ( $day =~ /\d{2}/ && $month =~ /\d{2}/ && $year =~ /\d{4}/ ) {
		return "$year-$month-$day";
	} else {
	
		if ( $random eq "random"){
			return rand_date( min => '2013-07-01', max => 'now' );
		} else {
			return rand_date( min => $start_date, max => 'now' );
		}
	}
	
} # sub createPostDate(){

sub postDate(){

	my $month = shift;
	my $number = $mon2num{$month};
#	|| warn "Could not convert month abbreviation '$month' to a number.";
	
	if ( $number =~ /\d/ ){
		return $number;
	} else {
		return int(rand(12)+1);
	}
} # sub postDate(){

sub prepareImage(){

	my $title 		= shift;
	my @cnt 		= shift;
	my @link 		= ();
	my $pic 		= "";
	my $pos_w_fin	= 320;
	my $pos_h_fin 	= 436;
	
	my $string		= join('', @cnt);
	my $LX 			= new HTML::LinkExtractor();
    $LX->parse(\$string);

	my $html = "";

	for my $Link( @{ $LX->links } ) {
		if( $$Link{tag} eq "img" ){
			my $image = $$Link{src};
						
			## nur für kkiste.to
			$image =~ s/images/http:\/\/kkiste\.to\/images/ig; 
			## nur für kkiste.to
			
			#print "Image: $image\n";
			$html .=<<END;
<a target="_blank" href="http://www.moviestreamz.com/downloads.php?file=$title" title="Klicke hier um den Kinofilm Stream $title herunter zu laden"><img src="$image" xheight="$pos_h_fin" xwidth="$pos_w_fin" alt="Kinofilm $title downloaden" /></a>
<br />
END
		}		

	}
	
	if ( length $html < 100 ) {
		
	$html =<<END;
<a target="_blank" href="http://www.moviestreamz.com/downloads.php?file=$title" title="Klicke hier um den Kinofilm Stream $title herunter zu laden"><img src="http://www.moviestreamz.com/images/standart_banner.jpg" height="$pos_h_fin" width="$pos_w_fin" alt="Kinofilm $title" /></a>
END

	};
	
	return $html;
	
}; # sub prepareImage(){

sub check(){
	
	my $max_retry = 30;
	my $sleep = 30;
	
	my $length_of_randomstring=32;# the length of 
			 # the random string to generate

	my @chars=('a'..'z','A'..'Z','0'..'9','_');
	my $random_string;
	foreach (1..$length_of_randomstring) 
	{
		# rand @chars will generate a random 
		# number between 0 and scalar @chars
		$random_string.=$chars[rand @chars];
	}
	my $ua = LWP::UserAgent->new;
	$ua->timeout(10);
	
	
	while($max_retry>0){
		
		my $response = $ua->get("http://www.nookiestar.com/alive.php?id=$random_string");

		if ($response->is_error) {
			printf "Error: [%d] %s\n", $response->code, $response->message;

			# record the timeout
			if ($response->code == HTTP::Status::HTTP_INTERNAL_SERVER_ERROR) {
				# later: socket: phprestart
				$max_retry--;
				print "HTTP 500 -> HTTP_INTERNAL_SERVER_ERROR: waiting $sleep second\n";
				sleep $sleep;
			}
		}
		
		if ( $response->content =~ /online/ig ){
			return 1;
		}
		
		if ( $response->content =~ /offline/ig ){
			$max_retry--;
			print "Mysql Database has gone away: waiting $sleep second\n";
			sleep $sleep;
		}
		
	}

	return 0;
}


sub changeAffilateLinks(){

	my $content = shift;

	if ( $content =~ m/http:\/\/www\.firstload\.com\/affiliate\/log\.php\?log=(\d{1,20})/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/www\.firstload\.com\/affiliate\/log\.php\?log=(\d{1,20})/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/tinyurl.com\/cubgw9z/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/tinyurl.com\/cubgw9z/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/bit\.ly\/YJtMVc/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/bit\.ly\/YJtMVc/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/ul-movies.com\/out\.php/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/ul-movies.com\/out\.php/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}	
	if ( $content =~ m/http:\/\/www\.surl\.pf-control\.de\/(\w|\d)/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/www\.surl\.pf-control\.de\/(\w|\d)/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}	
	if ( $content =~ m/http:\/\/www\.movie-blog\.org\/2007/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/www\.movie-blog\.org\/2007/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/movie-blog\.org\/2007/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/movie-blog\.org\/2007/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/cloudzer.net\/ref\/(\w|\d)/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/cloudzer.net\/ref\/(\w|\d)/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/netload\.in\/index\.php\?refer_id=(\w|\d)/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/netload\.in\/index\.php\?refer_id=(\w|\d)/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/tinyurl\.com\/cineload/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/tinyurl\.com\/cineload/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/www\.graboid\.com/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/www\.graboid\.com\/affiliates\/scripts\/click\.php/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/www\.hnmovies\.com/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/www\.hnmovies\.com\/affiliates\/scripts\/click\.php/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/download-client\.com/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/download-client\.com\/TDS\//http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/www\.downeu\.net\/redirect\//ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/www\.downeu\.net\/redirect\//http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/www\.torrentdownload\.ws\/search\?q=/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/www\.torrentdownload\.ws\/search/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/bit\.ly\/SharePirateUploaded\//ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/bit\.ly\/SharePirateUploaded/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/www\.filefactory\.com\/member\/refer\//ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/www\.filefactory\.com\/member\/refer\//http:\/\/www\.moviestreamz\.com\/highspeed\.php\?q=/ig;
	}
	if ( $content =~ m/http:\/\/www\.mplaynetwork\.com\/signup/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/www\.mplaynetwork\.com\/signup/http:\/\/www\.moviestreamz\.com\/highspeed\.php/ig;
	}
	if ( $content =~ m/http:\/\/ads\.affbuzzads\.com\/movie_player\/index\?ref=/ig ){
		#print "Dumper: Content is Foreign Affilate Link poisend for $SourceFilename\n";
		$content =~ s/http:\/\/ads\.affbuzzads\.com\/movie_player\/index\?ref=/http:\/\/www\.moviestreamz\.com\/highspeed\.php\?/ig;
	}
	
	#http://ads.affbuzzads.com/movie_player/index?ref=72368
	#http://www.mplaynetwork.com/signup
	#http://www.filefactory.com/member/refer/X3OeeWw
	#http://bit.ly/SharePirateUploaded
	#http://www.downeu.net/redirect/
	#http://download-client.com/TDS/?wmid=98997&uid=346&q=Star Trek The Motion Picture 1979 Bluray 1080p TRUEHD
	#http://cloudzer.net/ref/7144364
	#http://netload.in/index.php?refer_id=vQzTtYI&id=5
	#http://movie-blog.org/2007
	#http://www.surl.pf-control.de/L7h203nUmM
	return $content;
	
}; # sub changeAffilateLinks(){
