#!/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;
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 XML::Code; # perl -MCPAN -e 'force install "Curses"'
use File::Basename;
use Data::Dumper;
use String::Random; # perl -MCPAN -e 'force install "String::Random"'
use Digest::SHA qw(sha512_hex); # perl -MCPAN -e 'force install "Digest::SHA"'
use Encode qw(encode decode);
use HTML::Strip;
use MongoDB; # http://perlmaven.com/getting-started-with-mongodb-using-perl-insert-and-update

my $scriptname 		= basename($0);
my $count 			= 0;
my $pass 			= new String::Random;
my $hs 				= HTML::Strip->new();
my $client     		= MongoDB->connect('mongodb://localhost');
my $collection 		= $client->ns('OneTipp.B2Cv2'); # database foo, collection bar
	
my $SourceFolder 	= '/home/immo/www.immonet.de/angebot';
my $string1;
my $catg;
my $title;
my $string;
my $rcount = 0;
my $Hash;
my $dbh;
my %Hash = ();

##system("mysqldump -u root -p###########!########### -f --skip-skip-lock-tables buzzerstar > buzzerstarv3.sql");exit;
my $enc = 'latin1'; # in dieser Kodierung ist das Script gespeichert

open(L,">logfile_witze.txt");

# prepare files from SourceFolder
#eval {
find(\&SourceStructure, $SourceFolder);
#};
close L;

print "I found $count 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 =~ /\.html/ig && $SourceFilename !~ /trackback/ig){ #$SourceFilename =~ /\.(html|htm|shtm|shtml|php)/ig ){
	if ( $SourceFilename =~ /./ig){	
			print "DEBUG: Working on : $SourceFilename \n";	
			#print L "DEBUG: Working on : $SourceFilename \n";	
				{
				  local $/ = undef;
				  open FILE, "$SourceFilename" or return "Couldn't open file: $SourceFilename $!";
				  binmode FILE;
				  $string = <FILE>;
				  close FILE;
				}
				
				my (@obj_beschreibung) = ($string =~ m#headline-290">Objektbeschreibung<span class="hidden-print\s*(.*?)\s*panelLocationDescription#igs);
				#print Dumper @obj_beschreibung;
				
				my $cc1 = scalar(@obj_beschreibung);
				#print "Objektbeschreibung: $cc1\n";
							
				my (@obj_ausstattung) = ($string =~ m#headline-290">Ausstattung<span class="hidden-print\s*(.*?)\s*class="text-100">Verfügbarkeit prüfen#igs);
				#print Dumper @obj_beschreibung;
				
				my $cc2 = scalar(@obj_ausstattung);
				#print "ObjektAusstattung: $cc2\n";
			
				my (@obj_lage) = ($string =~ m#headline-290">Lage<span class="hidden-print\s*(.*?)\s*panelOther#igs);
				#print Dumper @obj_beschreibung;
				
				my $cc3 = scalar(@obj_lage);
				#print "ObjektLage: $cc3\n";
							
		#		my (@obj_sonstiges) = ($string =~ m# <strong>Sonstiges:</strong>\s*(.*?)\s*</p>#igs);
#
		#		#print Dumper @obj_beschreibung;
		#		my $cc4 = scalar(@obj_sonstiges);
		#		print "ObjektSonstiges: $cc4\n";
		#				
		#		my (@obj_infra) = ($string =~ m#<strong>Infrastruktur:</strong>\s*(.*?)\s*</p>#igs);
		#		
		#		my $cc5 = scalar(@obj_infra);
		#		print "ObjektInfastruktur: $cc5\n";
				
				my @array = sort { $a <=> $b } ($cc1, $cc2, $cc3);
				#print "min: $array[0]\n";
				#print "max: $array[-1]\n";
				my $max = $array[-1];
				
				for (my $i=0;$i<=$max;$i++){
					my $obj_beschreibung;
					my $obj_ausstattung;
					my $obj_lage;
					my $obj_sonstiges;
					my $obj_infra;
					
					my $b = &delLinefeed($obj_beschreibung[$i]);
					my $a = &delLinefeed($obj_ausstattung[$i]);
					my $l = &delLinefeed($obj_lage[$i]);
				#	my $s = &delLinefeed($obj_sonstiges[$i]);
				#	my $i = &delLinefeed($obj_infra[$i]);
										
				#	$b = $hs->parse($b);
				#	$hs->eof;
				#	$a = $hs->parse($a);
				#	$hs->eof;
				#	$l = $hs->parse($l);
				#	$hs->eof;
				#	$s = $hs->parse($s);
				#	$hs->eof;	
					
				#	print "Objektbeschreibung: $b\n";
				#	print "ObjektAusstattung: $a\n";
				#	print "ObjektLage: $l\n";
										
					my $result     = $collection->insert_one(
						{ time => time(), myid=>"SE1",objt => "www.immonet.de", desc => $b, deco => $a, loca => $l}
					);
				#	my $result     = $collection->insert_one(
				#		{ time => time(), myid=>"SE1",objt => "www.ohne-makler.net", desc => $b, deco => $a, loca => $l, othr => $s, infa => $i }
				#	);
				#	@obj_beschreibung
				#	@obj_ausstattung
				#	@obj_lage
				#	@obj_sonstiges
				}
								
				$count++;
				next;
				my $dbh = DBI->connect("dbi:mysql:dbname=","root","", {
					AutoCommit => 1,#0=$dbh->commit(); 
					RaiseError => 0,
				});
				
				my $query = sprintf("%s (%s, %s, %s, %s, %s)",
					"INSERT INTO witze (p_text,p_catg,p_shortcode,p_hash,id) VALUES",
					$dbh->quote(),
					$dbh->quote(),
					$dbh->quote($pass->randpattern("CncCncCncCnC")),
					$dbh->quote(sha512_hex()),		
					$dbh->quote(""),											
				);
			
				eval { $dbh->do($query) };
				$dbh->disconnect;
	}; # if ( $SourceFilename =~ /\.txt/ig ){#&& $SourceDirname =~ /Team/ig){
	
}; # sub SourceStructure(){
sub strip_tags(){
	my $content = shift;
	$content =~ s|<.+?>||ig;
	$content =~ s|content="||ig;
	$content =~ s|"||ig;
	return $content;
}
sub strip_tags_array(){
	my @arr = shift;
	my @retArray = ();
	foreach my $content (@arr){
	#my $content = shift;
	$content =~ s|<.+?>||ig;
	$content =~ s|.+?>||ig;
	$content =~ s|content="||ig;
	$content =~ s|"||ig;
	push(@retArray, $content);
	#$content =~ s#<([^">]+(?:"[^"]+")*[^>]+)>##ig;
	}
	return @retArray;
}
sub links(){
	my $content = shift;
	my @links = $content =~ /href\s*=\s*"?([^"\s>]+)/gis;
	return @links;
}

sub delLinefeed(){
	my $content = shift;
#	my (@obj) = ($content =~ m#>\s*(.*?)\s*<#igs);
#	my $final = "";
#	foreach my $e (@obj){
#		if ($e !~ /(ImmobilienScout24|Javascript|IS24\.message|class=|id=|jquery|function| Script|Java)/ig){
#			if ($e eq ''){
#				$final .= " ";
#			} else {
#				$final .= $e;
#			}
#		}
#	}
	
	$content =~ s|<.+?>||ig;
	$content =~ s|.+\">||ig;
	$content =~ s|<\"+.||ig;
	$content =~ s|\"+.||ig;
	
#	my ($content1,undef)=split(/</,$content);
#	my (undef,$content1)=split(/>/,$content);
#	
#	if (length($content1) > 120){
#		$content = $content1;
#	}
	
	$content =~ s/\r\n/ /ig;
	$content =~ s/\r/ /ig;
	$content =~ s/\n/\n/ig;
	$content =~ s/\t//ig;
	$content =~ s/(\s){2,}/ /ig;
	
	if ($content =~ /(ImmobilienScout24|Javascript|IS24\.message|class=|id=|jquery|function| Script|Java)/ig){
		return -1;
	}	
	return $content;
}
