#!/usr/bin/perl

system("cls");
#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;
use LWP::UserAgent;
use HTTP::Request::Common qw(GET);
use HTTP::Cookies;
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::Basename;
# 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);
use Data::Dumper; 
use POSIX qw(strftime);
use HTML::Entities;
use HTML::LinkExtractor;
use Regexp::Common qw(time);
use LWP::Simple;
use HTTP::Status ();

my %AlreadyScanned;
my $AlreadyScanned;
my $crawling;
my $key;
my $Hash;
my %Hash = ();

my $D;
my %D = ();

#############################
# Posting Data
 my $username 	= "cereal";
 my $password 	= "###########99"; 
 #my $proxy 		= "http://www.buzzerstar.com/devel/xmlrpc.php";
 my $proxy 		= "http://www.kinodownloads.com/xmlrpc.php";
#############################

my $count 			= 0; 
my $start			= 1;	# min rand time to wait between wp post requests
my $ende 			= 3;	# max rand time to wait between wp post requests
my $start_date		= strftime("%Y-%m-%d", localtime(time));
my $scriptname 		= basename($0);
my $CopySrcFolder 	= '/server/backup';
my $alreadyscanned  = '/server/kinodownloads/already/';
my $logfile  		= '/server/kinodownloads/logfile/';
$logfile 			.= $scriptname."_log.txt";
$alreadyscanned 	.= $scriptname."_alreadyscanned.txt";

my $domain 			= "$1.$2"; 
my $CopyToFolder 	= "$CopySrcFolder\\$scriptname";
mkdir $CopyToFolder;
copy($0,$CopyToFolder) or die "Copy failed: $!";

my %crawling = (
	  "page" => "http://cineload.ws/page/",
	  "crawlto" => 4,
	  "forbidden" => "(category|feed|contact|rules|trackback|author|comment|page|wp-|xmlrpc|download-stream-date|s=|uploader|c=|premium|respond|tag|hoster-tipp)",
	  "base"	=> "http://cineload.ws/",
	  "min_link_lenght" => 10
);

my %mon2num = (
	  "Januar" => 1,
	  "Februar" => 2,
	  "März" => 3,
	  "April" => 4,
	  "Mai" => 5,
	  "Juni" => 6,
	  "Juli" => 7,
	  "August" => 8,
	  "September" => 9,
	  "Oktober" => 10,
	  "November" => 11,
	  "Dezember" => 12
);

main();
sub main(){
	
	open(R,"<$alreadyscanned");
	while(<R>) {
	   chomp;
	   $AlreadyScanned{$_} = $_;
	}
	close R;
	
	my $base 		= $crawling{base};
	my $starpage 	= $crawling{page};
	my $goto 		= $crawling{crawlto};
	my $forbidden	= $crawling{forbidden};
	my $min_link_lenght = $crawling{min_link_lenght};
	
	# startpage
	my $content = fetch($base);
	next if length($content)<100;
	my $links_arref = links($content, $forbidden,$base,$min_link_lenght);
	$D{$base} = $base;
	create($links_arref);
		
	# subpages
	for (my $i=1;$i<=$goto;$i++){
		my $tmp_url = $starpage.$i;
	#	print "$scriptname -> Crawling Mainpages: $tmp_url\n";
		my $content = fetch($tmp_url);
		$D{$tmp_url} = $tmp_url;
		next if length($content)<100;
		my $links_arref = links($content, $forbidden,$base,$min_link_lenght);
		create($links_arref);
	}
	
	return;
} # sub main(){


sub create(){

	my $links_arref = shift;
	
	foreach my $link (@{$links_arref}){
	
		print "$scriptname -> Crawling: $link\n";
		my $content = fetch($link);
		my $title 	= title($content,$link);
		
		if ( $Hash{$title} ) {
			#print "ERROR: Double Title - $title ---> Hash Count: ".keys(%Hash)." \n";
			next;
		} else {	
			$Hash{$title} = $title;
		}
		
		my ($content) 		= ($content =~ m#<div class="entry">\s*(.*?)\s*</div>#igs);
		my @split_content 	= split("\n", $content);
		
		# bild code vorbereiten
		my $ImgCode 		= prepareImage($title, $content);
		
		# datums code vorbereiten
		my ($content_extra) = ($content =~ m#<div class="postdate-single">\s*(.*?)\s*</div>#igs);
		my $date 			= createPostDate($content_extra);
		
		# category code vorbereiten
		my ($content_extra) 	= ($content =~ m#Posted in\s*(.*?)\s*</div>#igs);
		my $catg			= categorize($content_extra, $title);
			
		my $header			= createHeader($title); 
		my $footer			= createFooter($title); 
		
		my $fullhtml = "";
		$fullhtml .= $header;
		$fullhtml .= $ImgCode;
		for ( my $i=0;$i<=$#split_content; $i++){
			my $output = @split_content[$i];
			if ( $output !~ /(spoiler-tgl|spoiler-body)/ig ){ # ignore bad html
				$fullhtml .= $output;
			}
		} # for ( my $i=0;$i<=$#split_content; $i++){

		$fullhtml .= $footer;
		$fullhtml =~ s/href/rel="nofollow" href/ig;	# setze alle links in dem post auf rel nofollow
		$fullhtml = changeAffilateLinks($fullhtml);
				
		post($title, $catg, $fullhtml, $date);
		
		open(W,"+>>$alreadyscanned");
			print W "$link\n";
		close W;
	
	}
	
	return;
	
} # sub create(){

sub post(){
	
	my $title = shift;
	my $catg  = shift;
	my $fullhtml = shift;
	my $date	= shift;
	
	return if length $title < 3; 
	
	print "$scriptname -> title: $title\n";
	print "\t -> Date: $date\n";
	print "\t -> Kategorie: $catg\n";
		
	my $lenght = length($fullhtml);
	my $p = WordPress::API::Post->new({
		  username => $username,
		  password => $password,
		  proxy => $proxy,
		#### proxy => 'http://www.buzzerstar.com/devel/xmlrpc.php',
	}); 
	
	my $is_catg = "";
	if ( $catg =~ /(Animation|anime)/ig){
		$is_catg = 'Animation'; #
	} elsif ( $title =~ /(Biografie|bio)/ig || $catg =~ /(Biografie|bio)/ig){
		$is_catg = 'Biografie'; #
	} elsif ( $title =~ /(Comedy|Komödie)/ig || $catg =~ /(Comedy|Komödie)/ig){
		$is_catg = 'Comedy'; #
	} elsif ( $title =~ /(Dokumentation|Doku)/ig || $catg =~ /(Dokumentation|Doku)/ig){
		$is_catg = 'Dokumentation'; #
	} elsif ( $title =~ /(Dramatik|Drama)/ig || $catg =~ /(Dramatik|Drama)/ig){
		$is_catg = 'Dramatik'; #
	} elsif ( $title =~ /(Zeichentrick|Comic)/ig || $catg =~ /(Zeichentrick|Comic)/ig){
		$is_catg = 'Zeichentrick'; #
	} elsif ( $title =~ /Western/ig || $catg =~ /Western/ig){
		$is_catg = 'Western'; #
	} elsif ( $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 Serien'; #
	} elsif ( $title =~ /(Thriller|Spannung)/ig || $catg =~ /(Thriller|Spannung)/ig){
		$is_catg = 'Thriller'; #
	} elsif ( $title =~ /Sport/ig || $catg =~ /Sport/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)/ig || $catg =~ /(Romanze|Romantik)/ig){
		$is_catg = 'Romantik'; #
	} elsif ( $title =~ /(Kriminalfilm|Krimi|Crime)/ig || $catg =~ /(Kriminalfilm|Krimi|Crime)/ig){
		$is_catg = 'Kriminalfilm'; #
	} elsif ( $title =~ /(Kriegsfilm|Krieg|War)/ig || $catg =~ /(Kriegsfilm|Krieg|War)/ig){
		$is_catg = 'Kriminalfilm'; #
	} elsif ( $title =~ /(Kinderfilm|Kinder|Child)/ig || $catg =~ /(Kinderfilm|Kinder|Child)/ig){
		$is_catg = 'Kinderfilm'; #
	} elsif ( $title =~ /Horror/ig || $catg =~ /Horror/ig){
		$is_catg = 'Horror'; #
	} elsif ( $title =~ /(Fantasy|Fantasie)/ig || $catg =~ /(Fantasy|Fantasie)/ig){
		$is_catg = 'Fantasy'; #
	} elsif ( $title =~ /(Abenteuer|adventure)/ig || $catg =~ /(Abenteuer|adventure)/ig){
		$is_catg = 'Abenteuer'; #	
	} elsif ( $title=~/(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 = 'Pornos und Sexfilme'; #	
	} 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 Movies'; #
	} elsif ( $title =~ /(Games|Spiele|PC|PS3|Playstation|Xbox|Wii|PSP|3DS|DS)/ig || $catg =~ /(Games|Spiele|PC|PS3|Playstation|Xbox|Wii|PSP|3DS|DS)/ig){
		$is_catg = 'Games'; # todo: auf kd.com als category anlegen
	} elsif ( $title =~ /(Action|Aktion)/ig || $catg =~ /(Action|Aktion)/ig){
		$is_catg = 'Action'; # todo: auf kd.com als category anlegen
	} else {
		$is_catg = 'Kinofilme'; #
	}

	$title = "Kostenlose $title $is_catg Downloads"; 
		
	# post to wordpress
	my $tags = $title; $tags =~ s/\s+/, /ig;
	$p->title($title);
	$p->description($fullhtml);  
	$p->mt_keywords("$title, $tags, $is_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);
	}
	
} # sub post(){


sub title(){

	my $string = shift;
	my $link = shift;
	
	my ($title_temp) = ($string =~ m#<TITLE>\s*(.*?)\s*</TITLE>#igs);
	my ($title,undef,undef) = split('(\||-|:|&laquo;|&raquo;|Film kostenlos downloaden|HD-World.org)', $title_temp);
	
	$title =~ s/(Movie|Download|Stream|Source|blog|org|&laquo;|&raquo;)//ig;
	$title =~ s/cineload\.ws//ig;
	$title =~ s/Movie-Blog\.org//ig;
	$title =~ s/HD-World\.org//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/\s{2,}+/ /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/\.txt//ig; $title =~ s/\.php//ig; $title =~ s/\.html//ig;
	$title =~ s/[^[:ascii:]]/ /g;
	$title =~ s/&(\w+);/ /g;
	$title =~ s/&#(\w+);//g;
	
	if (length $title <= 10){
		my $temp;
		my @temp = split("\/",$link);	
		my $test = $temp[$#temp]; #$title = "$temp[$#temp]";
		$test =~ s/(\.|_|-)/ /ig;
		$title =~ s/[^[:ascii:]]/ /g;
		$test =~ s/[A-Z][a-z]*+(?!\W)\K/ /gx;
		$test = ucfirst($title);
		$title = $test;
	}
	
	if (length $title <= 10){
		
		$title = $title_temp;
		
		$title =~ s/(Movie|Download|Stream|Source|blog|org|&laquo;|&raquo;)//ig;
		$title =~ s/cineload\.ws//ig;
		$title =~ s/Movie-Blog\.org//ig;
		$title =~ s/HD-World\.org//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/\s{2,}+/ /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/\.txt//ig; $title =~ s/\.php//ig; $title =~ s/\.html//ig;
		$title =~ s/[^[:ascii:]]/ /g;
		$title =~ s/&(\w+);/ /g;
		$title =~ s/&#(\w+);//g;
	}
	
	return $title;
	
} # sub title(){


sub links(){
	
	my $html 		= shift;
	my $forbidden 	= shift;
	my $base 		= shift;
	my $min_link_lenght = shift;
	
	my $lenght 		= length $base;
	my @links 		= ();
	
	my $LX 			= new HTML::LinkExtractor();
    $LX->parse(\$html);
	
    for my $Link( @{ $LX->links } ) {
           if( $$Link{href} !~ m/$forbidden/ig 
			&& $$Link{href} =~ m/$base/ig 
		 #	&& $$Link{href} =~ m/http/ig  # funzt nicht
			&& length($$Link{href})>$lenght+$min_link_lenght ) {
			
          #  print "$scriptname ->Found Link: ". $$Link{href}."\n";
			push @links, $$Link{href};
        }
    }
	
	my @unique = uniq( @links );
	return \@unique;
	
} #sub links(){


sub fetch(){

	my $url = shift;
	return if $url !~ m/^http/ig || length $url < 6;
	
	my $max_retry = 30;
	my $sleep = 60;
	
	if (exists $AlreadyScanned{$url} || exists $D{$url} ){
	#	warn "$url wurde schon gescannt";
		return 0;
	}
	
	my $ua = LWP::UserAgent->new;
	$ua->timeout(180);
	$ua->max_redirect( 100 );
	
    # Define user agent type
    $ua->agent('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');
	
    # Cookies
    $ua->cookie_jar(
        HTTP::Cookies->new(
            file => 'mycookies.txt',
            autosave => 1
        )
    );

	while($max_retry>0){
		
		# Request object
		my $req = GET $url;

		# Make the request
		my $res = $ua->request($req);
	
		if ($res->is_error) {
			printf "Fetching $url -> Error Code: [%d] %s\n", $res->code, $res->message;
			$max_retry--;
			sleep $sleep;
		}
		
		if ( $res->is_success ){
			 return $res->content;
		}
	}
	
	return 0;
	
} # sub fetch(){

sub uniq() {
	my %seen;
	return grep { !$seen{$_}++ } @_;
} # sub uniq() {

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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.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\.cineloads\.com\/highspeed\.php/ig;
	}
	
	#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(){


sub createPostDate(){
		
	my $content 			= 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);
		}
	};
	
#	print "Date: $day,$month,$year\n ";
	$month = postDate($month);
#	print "postDate: $month\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 {
		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};
			$html .=<<END;
<a target="_blank" href="http://www.kinodownloads.com/downloads.php?file=$title" title="Klicke hier um den Kinofilm $title herunter zu laden"><img src="$image" height="$pos_h_fin" width="$pos_w_fin" alt="Kinofilm $title downloaden" /></a>
<br />
END
		}		

	}
	
	if ( length $html < 100 ) {
		
	$html =<<END;
<a target="_blank" href="http://www.kinodownloads.com/downloads.php?file=$title" title="Klicke hier um den Kinofilm $title herunter zu laden"><img src="http://www.kinodownloads.com/images/standart_banner.jpg" height="$pos_h_fin" width="$pos_w_fin" alt="Kinofilm $title" /></a>
END

	};
	
	return $html;
	
}; # sub prepareImage(){

sub prepareImageOLD(){

	my $title 		= shift;
	my $cnt 		= shift;
	my @link 		= ();
	my $pic 		= "";
	my $pos_w_fin	= 320;
	my $pos_h_fin 	= 436;
	my $html;
	
	my @cnt			= split("\n",$cnt);
	
	foreach my $cnt (@cnt){
		#print "Dumper: $cnt\n";
		@link = split("\"", $cnt);
		foreach $pic (@link){
			#print "Dumper: $pic\n";
			if ( $pic =~ m/\.(jpg|jpeg|gif|png)/ig ) {
				#print "Found $pic\n";
				$pic =~ s/\?AP//ig;
				$html =<<END;
<a target="_blank" href="http://www.kinodownloads.com/downloads.php?file=$title" title="Klicke hier um den Kinofilm $title herunter zu laden"><img src="$pic" height="$pos_h_fin" width="$pos_w_fin" alt="Kinofilm $title downloaden" /></a>
END
				
			};
		};
	}
	
	if ( length($pic) < 10 ){
		foreach my $cnt (@cnt){
			#print "Dumper: $cnt\n";
			@link = split("=", $cnt);
			foreach $pic (@link){
				#print "Dumper: $pic\n";
				if ( $pic =~ m/\.(jpg|jpeg|gif|png)/ig ) {
					if ($pic=~/\s/ig){
						($pic,undef)=split(" ",$pic);
						$pic=~s/\"//ig;
					}
					
					#print "Found $pic\n";
					$pic =~ s/\?AP//ig;
					$html =<<END;
<a target="_blank" href="http://www.kinodownloads.com/downloads.php?file=$title" title="Klicke hier um den Kinofilm $title herunter zu laden"><img src="$pic" height="$pos_h_fin" width="$pos_w_fin" alt="Kinofilm $title downloaden" /></a>
END
				};
			};
		}
	} # if ( length($pic) < 10 ){
	
	if (length($html)<10){
	
		$html =<<END;
<a target="_blank" href="http://www.kinodownloads.com/downloads.php?file=$title" title="Klicke hier um den Kinofilm $title herunter zu laden"><img src="http://www.kinodownloads.com/images/standart_banner.jpg" height="$pos_h_fin" width="$pos_w_fin" alt="Kinofilm $title" /></a>
END

	};

	return $html;
	
}; # sub prepareImage(){


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.kinodownloads.com/downloads.php?file=$title" title="Downloade $title"><img src="http://www.kinodownloads.com/images/download.gif" alt="Downloade $title"></a>
<br /><br />
<a target="_blank" href="http://www.kinodownloads.com/highspeed.php" title="Highspeed Downloads">Tipp: Lade hier mit FULLSPEED deine Kinofilme herunter</a>
</td>
</tr>
</table>
</div>
<br /><br />
<a target="_blank" href="http://www.kinodownloads.com/downloads.php?file=$title" title="Downloade $title"><img src="http://www.kinodownloads.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>Kinofilm $title hier kostenlos downloaden</h1>
<h2><a href="http://www.kinodownloads.com/downloads.php?file=$title" target="_blank" style="font-size: 18px; color:red" title="Downloade hier $title"><b>Klick hier: <br />$title</b></a></h2>
<br><br>
Bis zum $date wurden $rand Downloads bei durchschnittlich $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 $catg;
	
	foreach my $line (@c){
		#print "Dumper: $line\n";
		if ($line=~/(category|c=)/ig){
			# print "Dumper: $line\n";
			$catg .= $line . " ";
		}
	}
	
	if ( $catg =~ /(Animation|anime)/ig){
		return 'Animation';
	} elsif ( $title =~ /(Biografie|bio)/ig || $catg =~ /(Biografie|bio)/ig){
		return 'Biografie';
	} elsif ( $title =~ /(Comedy|Komödie)/ig || $catg =~ /(Comedy|Komödie)/ig){
		return 'Comedy';
	} elsif ( $title =~ /(Dokumentation|Doku)/ig || $catg =~ /(Dokumentation|Doku)/ig){
		return 'Dokumentation';
	} elsif ( $title =~ /(Dramatik|Drama)/ig || $catg =~ /(Dramatik|Drama)/ig){
		return 'Dramatik';
	} elsif ( $title =~ /(Zeichentrick|Comic)/ig || $catg =~ /(Zeichentrick|Comic)/ig){
		return 'Zeichentrick';
	} elsif ( $title =~ /Western/ig || $catg =~ /Western/ig){
		return 'Western';
	} elsif ( $title =~ /(Tv Serien|Serien|Series|TV)/ig || $catg =~ /(Tv Serien|Serien|Series|TV)/ig){
		return 'Tv Serien';
	} elsif ( $title =~ /(Thriller|Spannung)/ig || $catg =~ /(Thriller|Spannung)/ig){
		return 'Thriller';
	} elsif ( $title =~ /Sport/ig || $catg =~ /Sport/ig){
		return 'Sport';
	} elsif ( $title =~ /(Science Fiction|Sci Fi|SciFi)/ig || $catg =~ /(Science Fiction|Sci Fi|SciFi)/ig){
		return 'Science Fiction';
	} elsif ( $title =~ /(Romanze|Romantik)/ig || $catg =~ /(Romanze|Romantik)/ig){
		return 'Romantik';
	} elsif ( $title =~ /(Kriminalfilm|Krimi|Crime)/ig || $catg =~ /(Kriminalfilm|Krimi|Crime)/ig){
		return 'Kriminalfilm';
	} elsif ( $title =~ /(Kriegsfilm|Krieg|War)/ig || $catg =~ /(Kriegsfilm|Krieg|War)/ig){
		return 'Kriegsfilm';
	} elsif ( $title =~ /(Kinderfilm|Kinder|Child)/ig || $catg =~ /(Kinderfilm|Kinder|Child)/ig){
		return 'Kinderfilm';
	} elsif ( $title =~ /Horror/ig || $catg =~ /Horror/ig){
		return 'Horror';
	} elsif ( $title =~ /(Fantasy|Fantasie)/ig || $catg =~ /(Fantasy|Fantasie)/ig){
		return 'Fantasy';
	} elsif ( $title =~ /(Abenteuer|adventure)/ig || $catg =~ /(Abenteuer|adventure)/ig){
		return 'Abenteuer'; #	
	} elsif ( $title =~/(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){
		return 'Pornos und Sexfilme'; #	
	} elsif ( $title =~ /(High Quality|High Definition|720p|720|1080|1080p|blu|ray|x264|MP4)/ig || $catg =~ /(High Quality|High Definition|720p|720|1080|1080p|blu|ray|x264|MP4)/ig){
		return 'High Definition Movies'; #
	} elsif ( $title =~ /(Games|Spiele|PC|PS3|Playstation|Xbox|Wii|PSP|3DS|DS)/ig || $catg =~ /(Games|Spiele|PC|PS3|Playstation|Xbox|Wii|PSP|3DS|DS)/ig){
		return 'Games';
	} elsif ( $title =~ /(Action|Aktion)/ig || $catg =~ /(Action|Aktion)/ig){
		return 'Action';
	} else {
		return 'Kinofilme'; #
	}

}; # sub categorize(){

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;
}