#!/usr/bin/perl -w
use DBI;
use IO::Socket::INET;

my    $dbh  = DBI->connect("dbi:Pg:dbname=web;host=localhost;port=5432","tfe","prout123") or die $dbh->errstr();

sub get_file(@)
{

    my ($file,$wr) = @_;
#    print "File : $file\n";
    chomp($file);
    my ($sock,$contenu);
    my ($addr,$path,$fichier) = $file =~ /^https?:\/\/(.+?)\/(.*\/)?(.+?)$/;
    $path = $path ? $path : "";
    
    if (!$addr or !$fichier) { warn("PB avec $file avec $addr et $fichier !\n"); next; }
#    print "Connection $addr \n";    
#    print "\tDownloading from $addr: " ;    
    $sock = IO::Socket::INET->new (
    	PeerAddr=>"$addr",
    	PeerPort=>'http(80)',
    	Proto=>'tcp'
    	) or next;
    $sock -> autoflush;    
    my $requete = "GET /$path$fichier HTTP/1.0\r\nHost: $addr\r\nUser-Agent: tfeserver Parser\r\n\r\n";
    print $sock $requete;

    
    my @temp = ();
    my $i=0;
    my $chunked=0;
    my $chunk_count=0;
    my $chunk_reprise=0;
    my $encoding = 0;
    my $started=0;
    my $check=1;
    while(defined($_ = <$sock>))
    {
    	$contenu = $_;

	if (!$started)
	{
#	print $contenu;	
	if ($contenu =~ /HTTP\/1.1 (\d+)/i) { 
	    my $num = $1;
	    if ($num != 200) { print "ERROR $num: $file\n";  
	    my $re = $dbh->prepare("DELETE FROM url where url='".$file."'");
	    $re->execute();	    
	    print "Del: \$bdd->prepare(\"DELETE FROM url where url='".$file."'\");\n";
	    }
	}
	if ($contenu =~ /Transfer-Encoding: chunked/) {  $chunked=1;  }
	if ($contenu =~ /Content-Length: (\d+)/i) { $chunk_count=$1; }	
	if ($contenu =~ /Connection: close/i) { $check=0; }		
	if ($contenu =~ /Charset=(.*)/i) { $encoding=$1; }	
	if ($contenu =~ /^\r?$/) 
	{ 
	    print "check de /home/tfe/web/dl/images/cache/$wr\n";
	    if (! -d "/home/tfe/web/dl/images/cache/$wr") { mkdir "/home/tfe/web/dl/images/cache/$wr"; }
	    open(FICHIER,">/home/tfe/web/dl/images/cache/$wr/$fichier") or warn "Impossible d ouvrir la sortie (cache/$wr/$fichier) en ecriture \n"; 
	    $started=1; 
	}
	}
	else	{
	if (!$chunked)	{
        $chunk_count-= length($contenu);	
#        $contenu =~ s/\r//g;
#        $contenu =~ s/\n//g;	
#	if ($encoding =~ /utf\-(\d+)/i)	{ $contenu = Unicode::String::utf8("$contenu")->latin1;	}	
	push(@temp,$contenu); 
	if ($chunk_count <= 0 and $check) { close $sock; last;}
	}
	else	{

	    if ($chunk_count<=0)	    {
		$contenu =~ s/[^a-f0-9]//g;	    
		$chunk_count = hex $contenu;
		if ($chunk_count <= 0 && length($contenu)>0) {  close $sock; last; }
		$chunk_reprise=1;
	    }
	    else   {
	    $chunk_count-= length($contenu);
	    if ($encoding =~ /utf\-(\d+)/i  && $contenu)	{ $contenu = Unicode::String::utf8("$contenu")->latin1; }
            $contenu =~ s/\r//g;
            $contenu =~ s/\n//g;	
	    if ($chunk_reprise) {  
		if( @temp) { $temp[$#temp].=$contenu; }
		else { push(@temp,$contenu); }
		$chunk_reprise=0; 
	    }
	    else   {  push(@temp,$contenu); }
	}
	}
	}
#	$i++;
    
    }
    print FICHIER join("",@temp);
    close FICHIER;    
    close $sock;
#    print "\tOK\n" ;
#    print "Fin Mise a jour\n\n" ;
    return 1;
}





my $sth  = $dbh->prepare("SELECT * FROM url");
$sth->execute();
while ($row = $sth->fetchrow_hashref)
{
    if (${$row}{url} =~ /\.(?:jpe?g)|(?:png)|(?:gif)$/)
    {
    ${$row}{channel} =~ s/\W//g;
    print "Getting : ".${$row}{url}."\n";
    get_file(${$row}{url},${$row}{channel});
    }
}


