#!/usr/bin/perl 
#use strict;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request;
use HTTP::Response;
use Encode; 

my $ua = LWP::UserAgent->new(agent => "Konqueror/3");
			   
$cookie_jar = HTTP::Cookies->new(file => 'lwp_cookies.dat',
				 autosave => 1,);

$ua->cookie_jar($cookie_jar );

my $base = 'http://elections.interieur.gouv.fr/PR2012/';

my $res  = $ua->request(HTTP::Request->new( GET => $base."index.html" ) );

my @rows = $res->content;
open (FICHIER, '>>data.txt');
#trouve les départements
@links = $res->content =~ /href\s*=\s*"?([^"\s>]+)/gis;
my @depLinks = grep(/\d+\/\d+\/\d+\.html/,@links);
#print join("\n",@depLinks);
my @villeLink;
foreach my $depLink (@depLinks)
{
	#choppe la liste des lettres des villes
	$res  = $ua->request(HTTP::Request->new( GET => $base.$depLink ) );
	@links = $res->content =~ /href\s*=\s*"?([^"\s>]+)/gis;
	my @lettresLinks = grep(/\d+\S.html/,@links);
	#print join("\n",@lettresLinks);

	my $depBase;
	if($depLink =~ /(\d+\/\d+\/)\d+\.html/)
	{
		$depBase = $1;
	}
	
	#print $depBase;
	foreach my $lettreLink (@lettresLinks)
	{
		$res  = $ua->request(HTTP::Request->new( GET => $base.$depBase.$lettreLink ) );
		@links = $res->content =~ /href\s*=\s*"?([^"\s>]+)/gis;
		my @villesLinks = grep(/\d\d\d\d+.html/,@links);
		#print join("\n",@villesLinks);
		#remove duplicates
		my @ltab;
		my @villesLinksNoDup ;		
		for $link (@villesLinks) 
		{
			if ($ltab{$link}) {}
			else				
			{
				#print $link."\n";
				push(@villesLinksNoDup,$link);
				$ltab{$link} = 1;
				
			}
        	}
		#print join("\n",@villesLinksNoDup);
		foreach my $villeLink (@villesLinksNoDup)
		{
			$res  = $ua->request(HTTP::Request->new( GET => $base.$depBase.$villeLink) );	
			#print $res->content;
			if($res->content =~ /<title>.*sidentielle 2012 : (.*)<\/title>/)
			{
				print FICHIER encode('UTF_8', decode('iso-8859-1', $1." ; "));
				print encode('UTF_8', decode('iso-8859-1', $1."\n"));
			}
			my $content = encode('UTF_8', decode('iso-8859-1',$res->content));
			#print $content;
			$content=~s/ / /g;
			@data = $content =~ /<td class="tdc[bf]*" align="left">.*?<\/td><td class="tdcd" align="right">([\d ]+)<\/td>/gis;
			print FICHIER join(" ; ",@data);
			print FICHIER "\n";
			#exit();		
		}	

	}
} 
close(FICHIER);




