#!/usr/bin/perl -w
# 
use strict;
use Web::Scraper;
use LWP::UserAgent;


my $p1 = scraper {
      process 'table[id="tableau2"] td', "list[]" => scraper {
              process ".corptableR", conf => 'TEXT';
              process_first ".corptable", date => 'TEXT';
              };
};

my $url = "http://www.memorialgenweb.org/memorial3/html/fr/resultcommune.php?act=view&insee=54395&pays=France&dpt=54&idsource=57519&table=bp&lettre=&fusxx=&debut=";
my $phpsid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; # XXX set your own PHPSESSID cookie

my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
$ua->cookie_jar({});

my %dates = ();

my $total = 0;

for (my $i = 0; $i<38; $i++) {
    my $page = $i*100;
    sleep(3);
    my $resget = $ua->get($url.$page, 'Cookie' => "PHPSESSID=$phpsid");
    if ($resget->is_error) {
        #print $resget->error_as_HTML;
        print "Error page $i\n";
        next;
    };

    #print $resget->content;
    my $res = $p1->scrape($resget);
        
    my $c = "";
    for my $p (@{$res->{list}}) {
        if ( $p->{conf}  && $p->{conf} eq "1914-1918") {
            $c = $p->{conf};
            #print "$c\n";
            next;
        };
        my $d = $p->{date} || "";
        if ($c ne "")  {
            #print "\t==$d==\n";
            if ( $d =~ m/^(\d\d)\/(\d\d)\/19(\d\d) / ) {
                my $date = "19$3-$2-$1";
                $dates{$date}++;
                $total++;
                #print "\t$date";
            }
            $c = "";
            next;
        }
    };

};

print "Date,Quantité\n";
foreach my $d (sort keys %dates) {
    print "$d,$dates{$d}\n";
}
print $total."\n";
